Completed
Push — master ( 5a0e22...3dcaa1 )
by Thomas Mauro
02:54
created

ClientFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 8
dl 0
loc 70
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getOptionsClass() 0 4 1
B __invoke() 0 29 4
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\ServiceManager\Exception\ServiceNotCreatedException;
12
use Zend\ServiceManager\Exception\ServiceNotFoundException;
13
use Zend\ServiceManager\ServiceLocatorInterface;
14
15
/**
16
 * Class ClientFactory.
17
 */
18
class ClientFactory extends AbstractFactory
19
{
20
    /**
21
     * Get the class name of the options associated with this factory.
22
     *
23
     * @return string
24
     */
25 1
    public function getOptionsClass()
26
    {
27 1
        return \PamiModule\Options\Client::class;
28
    }
29
30
    /**
31
     * Create an object.
32
     *
33
     * @param ContainerInterface $container
34
     * @param string             $requestedName
35
     * @param null|array         $options
36
     *
37
     * @throws \RuntimeException
38
     * @throws \Interop\Container\Exception\NotFoundException
39
     * @throws ServiceNotFoundException                       if unable to resolve the service
40
     * @throws ServiceNotCreatedException                     if an exception is raised when
41
     *                                                        creating a service
42
     * @throws ContainerException                             if any other error occurs
43
     *
44
     * @return Client
45
     */
46 1
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
47
    {
48 1
        $options = null;
49 1
        $connectionName = $this->getName();
50
51 1
        if ($this->hasOptions($container, 'client', $this->getName())) {
52
            /** @var ClientOptions $options */
53 1
            $options = $this->getOptions($container, 'client');
54 1
            $connectionName = $options->getConnection() ?: $this->getName();
55 1
        }
56
57 1
        $connectionFactory = new ConnectionFactory($connectionName);
58
        /** @var Connection $connectionOptions */
59 1
        $connectionOptions = $connectionFactory->getOptions($container, 'connection');
60
61
        /** @var ClientImpl $connection */
62 1
        $connection = $container->get(sprintf('pami.connection.%s', $connectionName));
63
64 1
        $client = new Client($connectionOptions->getHost(), $connection);
65
66 1
        if ($options) {
67 1
            $client->setParams($options->getParams());
68 1
        }
69
70 1
        $eventForwarder = new EventForwarder($client);
71 1
        $client->getConnection()->registerEventListener($eventForwarder);
72
73 1
        return $client;
74
    }
75
76
    /**
77
     * Create service.
78
     *
79
     * @param ServiceLocatorInterface $serviceLocator Service locator
80
     *
81
     * @return Client
82
     */
83 1
    public function createService(ServiceLocatorInterface $serviceLocator)
84
    {
85 1
        return $this($serviceLocator, Client::class);
86
    }
87
}
88