AbstractFactory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace RabbitMqModule\Service;
4
5
use Interop\Container\ContainerInterface;
6
use Zend\ServiceManager\Factory\FactoryInterface;
7
use RuntimeException;
8
9
/**
10
 * Class AbstractFactory
11
 * @package RabbitMqModule\Service
12
 */
13
abstract class AbstractFactory implements FactoryInterface
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $name;
19
20
    /**
21
     * @var \Zend\Stdlib\AbstractOptions
22
     */
23
    protected $options;
24
25
    /**
26
     * @var string
27
     */
28
    protected $configKey = 'rabbitmq_module';
29
30
    /**
31
     * @param string $name
32
     */
33 11
    public function __construct($name)
34
    {
35 11
        $this->name = $name;
36 11
    }
37
38
    /**
39
     * @return string
40
     */
41 10
    public function getName()
42
    {
43 10
        return $this->name;
44
    }
45
46
    /**
47
     * Gets options from configuration based on name.
48
     *
49
     * @param ContainerInterface $container
50
     * @param string                  $key
51
     * @param null|string             $name
52
     *
53
     * @return \Zend\Stdlib\AbstractOptions
54
     *
55
     * @throws \RuntimeException
56
     */
57 11
    public function getOptions(ContainerInterface $container, $key, $name = null)
58
    {
59 11
        if ($name === null) {
60 10
            $name = $this->getName();
61 10
        }
62
63
        /** @var array $options */
64 11
        $options = $container->get('Configuration');
65 11
        $options = $options[$this->configKey];
66 11
        $options = isset($options[$key][$name]) ? $options[$key][$name] : null;
67
68 11
        if (null === $options) {
69 1
            throw new RuntimeException(
70 1
                sprintf('Options with name "%s" could not be found in "%s.%s"', $name, $this->configKey, $key)
71 1
            );
72
        }
73
74 10
        $optionsClass = $this->getOptionsClass();
75
76 10
        return new $optionsClass($options);
77
    }
78
79
    /**
80
     * Get the class name of the options associated with this factory.
81
     *
82
     * @abstract
83
     *
84
     * @return string
85
     */
86
    abstract public function getOptionsClass();
87
}
88