Source of file AbstractWorkerSingleton.php

Size: 4,017 Bytes - Last Modified: 2021-01-12T22:04:13+00:00

C:/Users/MAKS/Code/_PROJECTS/amqp-agent/src/Worker/AbstractWorkerSingleton.php

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
Covered by 7 test(s):
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingleton
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonGetInstanceInstantiatesClassWithTheSuppliedParameters
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceGetPropertyViaPublicAccessNotation
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceGetPropertyViaPublicAssignmentNotation
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceWhenCallingStaticMethod
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceRetrievingStaticOrConstProperty
  • MAKS\AmqpAgent\Tests\Worker\ConsumerSingletonTest::testSingleton
47
Covered by 7 test(s):
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingleton
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonGetInstanceInstantiatesClassWithTheSuppliedParameters
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceGetPropertyViaPublicAccessNotation
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceGetPropertyViaPublicAssignmentNotation
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceWhenCallingStaticMethod
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceRetrievingStaticOrConstProperty
  • MAKS\AmqpAgent\Tests\Worker\ConsumerSingletonTest::testSingleton
4849
Covered by 7 test(s):
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingleton
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonGetInstanceInstantiatesClassWithTheSuppliedParameters
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceGetPropertyViaPublicAccessNotation
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceGetPropertyViaPublicAssignmentNotation
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceWhenCallingStaticMethod
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceRetrievingStaticOrConstProperty
  • MAKS\AmqpAgent\Tests\Worker\ConsumerSingletonTest::testSingleton
5051
Covered by 7 test(s):
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingleton
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonGetInstanceInstantiatesClassWithTheSuppliedParameters
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceGetPropertyViaPublicAccessNotation
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceGetPropertyViaPublicAssignmentNotation
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceWhenCallingStaticMethod
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceRetrievingStaticOrConstProperty
  • MAKS\AmqpAgent\Tests\Worker\ConsumerSingletonTest::testSingleton
52
Covered by 7 test(s):
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingleton
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonGetInstanceInstantiatesClassWithTheSuppliedParameters
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceGetPropertyViaPublicAccessNotation
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceGetPropertyViaPublicAssignmentNotation
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceWhenCallingStaticMethod
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceRetrievingStaticOrConstProperty
  • MAKS\AmqpAgent\Tests\Worker\ConsumerSingletonTest::testSingleton
5354
Covered by 7 test(s):
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingleton
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonGetInstanceInstantiatesClassWithTheSuppliedParameters
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceGetPropertyViaPublicAccessNotation
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceGetPropertyViaPublicAssignmentNotation
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceWhenCallingStaticMethod
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceRetrievingStaticOrConstProperty
  • MAKS\AmqpAgent\Tests\Worker\ConsumerSingletonTest::testSingleton
55
Covered by 1 test(s):
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonGetInstanceInstantiatesClassWithTheSuppliedParameters
56
Covered by 1 test(s):
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonGetInstanceInstantiatesClassWithTheSuppliedParameters
5758
Covered by 1 test(s):
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonGetInstanceInstantiatesClassWithTheSuppliedParameters
59
Covered by 1 test(s):
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonGetInstanceInstantiatesClassWithTheSuppliedParameters
60
Covered by 1 test(s):
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonGetInstanceInstantiatesClassWithTheSuppliedParameters
61
Covered by 1 test(s):
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonGetInstanceInstantiatesClassWithTheSuppliedParameters
62
Covered by 1 test(s):
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonGetInstanceInstantiatesClassWithTheSuppliedParameters
63
Covered by 1 test(s):
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonGetInstanceInstantiatesClassWithTheSuppliedParameters
64
Covered by 1 test(s):
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonGetInstanceInstantiatesClassWithTheSuppliedParameters
6566676869
Covered by 7 test(s):
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingleton
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonGetInstanceInstantiatesClassWithTheSuppliedParameters
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceGetPropertyViaPublicAccessNotation
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceGetPropertyViaPublicAssignmentNotation
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceWhenCallingStaticMethod
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceRetrievingStaticOrConstProperty
  • MAKS\AmqpAgent\Tests\Worker\ConsumerSingletonTest::testSingleton
7071727374757677787980
Covered by 3 test(s):
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonGetInstanceInstantiatesClassWithTheSuppliedParameters
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceGetPropertyViaPublicAccessNotation
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceRetrievingStaticOrConstProperty
81
Covered by 1 test(s):
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceRetrievingStaticOrConstProperty
82
Covered by 3 test(s):
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonGetInstanceInstantiatesClassWithTheSuppliedParameters
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceGetPropertyViaPublicAccessNotation
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceRetrievingStaticOrConstProperty
83
Covered by 1 test(s):
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceRetrievingStaticOrConstProperty
848586
Covered by 2 test(s):
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonGetInstanceInstantiatesClassWithTheSuppliedParameters
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceGetPropertyViaPublicAccessNotation
8788899091929394959697
Covered by 2 test(s):
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceGetPropertyViaPublicAssignmentNotation
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceRetrievingStaticOrConstProperty
98
Covered by 1 test(s):
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceRetrievingStaticOrConstProperty
99
Covered by 1 test(s):
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceRetrievingStaticOrConstProperty
100101102
Covered by 1 test(s):
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceGetPropertyViaPublicAssignmentNotation
103
Covered by 1 test(s):
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceGetPropertyViaPublicAssignmentNotation
104105106107108109110111112113
Covered by 3 test(s):
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceGetPropertyViaPublicAccessNotation
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceGetPropertyViaPublicAssignmentNotation
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceWhenCallingStaticMethod
114
Covered by 3 test(s):
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceGetPropertyViaPublicAccessNotation
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceGetPropertyViaPublicAssignmentNotation
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceWhenCallingStaticMethod
115116117
Covered by 3 test(s):
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceGetPropertyViaPublicAccessNotation
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceGetPropertyViaPublicAssignmentNotation
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceWhenCallingStaticMethod
118
Covered by 3 test(s):
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceGetPropertyViaPublicAccessNotation
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceGetPropertyViaPublicAssignmentNotation
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceWhenCallingStaticMethod
119120121
Covered by 3 test(s):
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceGetPropertyViaPublicAccessNotation
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceGetPropertyViaPublicAssignmentNotation
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceWhenCallingStaticMethod
122123124125126127128129130131132
Covered by 1 test(s):
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceWhenCallingStaticMethod
133
Covered by 1 test(s):
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceWhenCallingStaticMethod
134135
Covered by 1 test(s):
  • MAKS\AmqpAgent\Tests\Worker\PublisherSingletonTest::testSingletonInstanceWhenCallingStaticMethod
136137138
<?php

/**
 * @author Marwan Al-Soltany <MarwanAlsoltany@gmail.com>
 * @copyright Marwan Al-Soltany 2020
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 */

declare(strict_types=1);

namespace MAKS\AmqpAgent\Worker;

use ReflectionClass;
use MAKS\AmqpAgent\Helper\Singleton;
use MAKS\AmqpAgent\Worker\AbstractWorker;

/**
 * An abstract decorator class implementing mapping functions (proxy functions) to turn a normal worker into a singleton.
 * @since 1.0.0
 */
abstract class AbstractWorkerSingleton extends Singleton
{
    /**
     * The full qualified name of the instantiated class.
     * @var string
     */
    protected static $class;

    /**
     * The instance of the worker class (a class that extends AbstractWorker).
     * Sub-classes of this class should instantiate a worker and set it
     * to the protected $worker property in their __construct() method.
     * @var AbstractWorker
     */
    protected $worker;


    /**
     * Returns an instance of the class this method was called on.
     * @param array ...$arguments The same arguments of the normal worker.
     * @return self
     */
    public static function getInstance()
    {
        $worker = parent::getInstance();
        $workerReference = $worker->worker;

        static::$class = get_class($workerReference);

        $arguments = func_get_args();
        $argsCount = func_num_args();

        if ($argsCount > 0) {
            $reflection = new ReflectionClass($workerReference);
            $properties = $reflection->getConstructor()->getParameters();

            $index = 0;
            foreach ($properties as $property) {
                $member = $property->getName();
                $workerReference->{$member} = $arguments[$index];
                $index++;
                if ($index === $argsCount) {
                    break;
                }
            }
        }

        return $worker;
    }


    /**
     * Gets a class member via public property access notation.
     * @param string $member Property name.
     * @return mixed
     */
    public function __get(string $member)
    {
        if (defined(static::$class . '::' . $member)) {
            return constant(static::$class . '::' . $member);
        } elseif (isset(static::$class::$$member)) {
            return static::$class::$$member;
        }

        return $this->worker->$member;
    }

    /**
     * Sets a class member via public property assignment notation.
     * @param string $member Property name.
     * @param mixed $value Override for object property or a static property.
     * @return void
     */
    public function __set(string $member, $value)
    {
        if (isset(static::$class::$$member)) {
            static::$class::$$member = $value;
            return;
        }

        $this->worker->{$member} = $value;
    }

    /**
     * Calls a method on a class that extend AbstractWorker and throws an exception for calls to undefined methods.
     * @param string $method Function name.
     * @param array $arguments Function arguments.
     * @return mixed
     */
    public function __call(string $method, array $arguments)
    {
        $function = [$this->worker, $method];
        $return = call_user_func_array($function, $arguments);

        // check to return the right object to allow for trouble-free chaining.
        if ($return instanceof $this->worker) {
            return $this;
        }

        return $return;
    }

    /**
     * Calls a method on a class that extend AbstractWorker and throws an exception for calls to undefined static methods.
     * @param string $method Function name.
     * @param array $arguments Function arguments.
     * @return mixed
     */
    public static function __callStatic(string $method, array $arguments)
    {
        $function = [static::$class, $method];
        $return = forward_static_call_array($function, $arguments);

        return $return;
    }
}