ClientFactory   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 9

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 9
dl 0
loc 111
ccs 30
cts 35
cp 0.8571
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getOptionsClass() 0 4 1
B __invoke() 0 30 4
A createEventManager() 0 18 4
A acceptsSharedManagerToConstructor() 0 6 1
A createService() 0 4 1
1
<?php
2
3
namespace PamiModule\Service;
4
5
use Interop\Container\ContainerInterface;
6
use Interop\Container\Exception\ContainerException;
7
use PAMI\Client\Impl\ClientImpl;
8
use PamiModule\Event\EventForwarder;
9
use PamiModule\Options\Client as ClientOptions;
10
use PamiModule\Options\Connection;
11
use Zend\EventManager\EventManager;
12
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
13
use Zend\ServiceManager\Exception\ServiceNotFoundException;
14
use Zend\ServiceManager\ServiceLocatorInterface;
15
16
/**
17
 * Class ClientFactory.
18
 */
19
class ClientFactory extends AbstractFactory
20
{
21
    /**
22
     * Get the class name of the options associated with this factory.
23
     *
24
     * @return string
25
     */
26 1
    public function getOptionsClass()
27
    {
28 1
        return \PamiModule\Options\Client::class;
29
    }
30
31
    /**
32
     * Create an object.
33
     *
34
     * @param ContainerInterface $container
35
     * @param string             $requestedName
36
     * @param null|array         $options
37
     *
38
     * @throws \RuntimeException
39
     * @throws \Interop\Container\Exception\NotFoundException
40
     * @throws ServiceNotFoundException                       if unable to resolve the service
41
     * @throws ServiceNotCreatedException                     if an exception is raised when
42
     *                                                        creating a service
43
     * @throws ContainerException                             if any other error occurs
44
     *
45
     * @return Client
46
     */
47 1
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
48
    {
49 1
        $options = null;
50 1
        $connectionName = $this->getName();
51
52 1
        if ($this->hasOptions($container, 'client', $this->getName())) {
53
            /** @var ClientOptions $options */
54 1
            $options = $this->getOptions($container, 'client');
55 1
            $connectionName = $options->getConnection() ?: $this->getName();
56 1
        }
57
58 1
        $connectionFactory = new ConnectionFactory($connectionName);
59
        /** @var Connection $connectionOptions */
60 1
        $connectionOptions = $connectionFactory->getOptions($container, 'connection');
61
62
        /** @var ClientImpl $connection */
63 1
        $connection = $container->get(sprintf('pami.connection.%s', $connectionName));
64
65 1
        $client = new Client($connectionOptions->getHost(), $connection);
66 1
        $client->setEventManager($this->createEventManager($container));
67
68 1
        if ($options) {
69 1
            $client->setParams($options->getParams());
70 1
        }
71
72 1
        $eventForwarder = new EventForwarder($client);
73 1
        $client->getConnection()->registerEventListener($eventForwarder);
74
75 1
        return $client;
76
    }
77
78
    /**
79
     * @param ContainerInterface $container
80
     *
81
     * @return EventManager
82
     */
83 1
    protected function createEventManager(ContainerInterface $container)
84
    {
85 1
        if ($this->acceptsSharedManagerToConstructor()) {
86
            // zend-eventmanager v3
87 1
            return new EventManager(
88 1
                $container->has('SharedEventManager') ? $container->get('SharedEventManager') : null
89 1
            );
90
        }
91
92
        // zend-eventmanager v2
93
        $events = new EventManager();
94
95
        if ($container->has('SharedEventManager')) {
96
            $events->setSharedManager($container->get('SharedEventManager'));
0 ignored issues
show
Bug introduced by
The method setSharedManager() does not seem to exist on object<Zend\EventManager\EventManager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
97
        }
98
99
        return $events;
100
    }
101
102
    /**
103
     * Does the EventManager accept the shared manager to the constructor?
104
     *
105
     * In zend-eventmanager v3, the EventManager accepts the shared manager
106
     * instance to the constructor *only*, while in v2, it must be injected
107
     * via the setSharedManager() method.
108
     *
109
     * @return bool
110
     */
111 1
    private function acceptsSharedManagerToConstructor()
112
    {
113 1
        $r = new \ReflectionClass(EventManager::class);
114
115 1
        return !$r->hasMethod('setSharedManager');
116
    }
117
118
    /**
119
     * Create service.
120
     *
121
     * @param ServiceLocatorInterface $serviceLocator Service locator
122
     *
123
     * @return Client
124
     */
125 1
    public function createService(ServiceLocatorInterface $serviceLocator)
126
    {
127 1
        return $this($serviceLocator, Client::class);
128
    }
129
}
130