ProducerFactory::createProducer()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 2
crap 2
1
<?php
2
3
namespace RabbitMqModule\Service;
4
5
use Interop\Container\ContainerInterface;
6
use Interop\Container\Exception\ContainerException;
7
use PhpAmqpLib\Connection\AbstractConnection;
8
use RabbitMqModule\Producer;
9
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
10
use Zend\ServiceManager\Exception\ServiceNotFoundException;
11
use RabbitMqModule\Options\Producer as Options;
12
use InvalidArgumentException;
13
14
/**
15
 * Class ProducerFactory
16
 * @package RabbitMqModule\Service
17
 */
18
class ProducerFactory 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 Options::class;
28
    }
29
30
    /**
31
     * @param ContainerInterface $serviceLocator
32
     * @param Options                 $options
33
     *
34
     * @return Producer
35
     *
36
     * @throws InvalidArgumentException
37
     */
38 1
    protected function createProducer(ContainerInterface $serviceLocator, Options $options)
39
    {
40
        /** @var AbstractConnection $connection */
41 1
        $connection = $serviceLocator->get(sprintf('%s.connection.%s', $this->configKey, $options->getConnection()));
42 1
        $producer = new Producer($connection);
43 1
        $producer->setExchangeOptions($options->getExchange());
44 1
        if ($options->getQueue()) {
45 1
            $producer->setQueueOptions($options->getQueue());
46 1
        }
47 1
        $producer->setAutoSetupFabricEnabled($options->isAutoSetupFabricEnabled());
48
49 1
        return $producer;
50
    }
51
52
    /**
53
     * Create an object
54
     *
55
     * @param  ContainerInterface $container
56
     * @param  string $requestedName
57
     * @param  null|array $options
58
     * @return Producer
59
     * @throws ServiceNotFoundException if unable to resolve the service.
60
     * @throws ServiceNotCreatedException if an exception is raised when
61
     *     creating a service.
62
     * @throws ContainerException if any other error occurs
63
     */
64 1
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
65
    {
66
        /* @var $options Options */
67 1
        $options = $this->getOptions($container, 'producer');
68
69 1
        return $this->createProducer($container, $options);
70
    }
71
}
72