DiAbstractFactory::initializeExtractor()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.2559

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 5
cp 0.6
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 2.2559
1
<?php
2
namespace mxdiModule\ServiceManager;
3
4
use mxdiModule\Service\ExtractorInterface;
5
use mxdiModule\Service\InstantiatorInterface;
6
use mxdiModule\Traits\ServiceTrait;
7
use Zend\Cache\Storage\Adapter\AbstractAdapter;
8
use Zend\Cache\Storage\StorageInterface;
9
use Zend\ServiceManager\AbstractFactoryInterface;
10
use Zend\ServiceManager\ServiceLocatorInterface;
11
use mxdiModule\Service\ChangeSet;
12
use mxdiModule\Service\Instantiator;
13
14
class DiAbstractFactory implements AbstractFactoryInterface
15
{
16
    use ServiceTrait;
17
18
    /** @var array */
19
    protected $config;
20
21
    /** @var ChangeSet|mixed */
22
    protected $changeSet;
23
24
    /** @var ExtractorInterface */
25
    protected $extractor;
26
27
    /** @var InstantiatorInterface */
28
    protected $instantiator;
29
30
    /** @var StorageInterface|AbstractAdapter */
31
    protected $cache;
32
33 11
    public function __construct(InstantiatorInterface $instantiator = null)
34
    {
35 11
        $this->instantiator = $instantiator ?: new Instantiator();
36 11
    }
37
38
    /**
39
     * Determine if we can create a service with name
40
     *
41
     * @param ServiceLocatorInterface $serviceLocator
42
     * @param string $name
43
     * @param string $requestedName
44
     * @return bool
45
     */
46 4
    public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
47
    {
48 4
        if ($this->shouldBeAvoided($serviceLocator, $name)) {
49 1
            return false;
50
        }
51
52 3
        $this->initializeCache($serviceLocator);
53 3
        $this->setChangeSet($this->cache->getItem($this->getHash($name)));
54
55 3
        if ($this->getChangeSet() instanceof ChangeSet) {
56
            // Positive result available via cache
57
            // Because we don't allow not annotated results to be set there
58 1
            return true;
59
        }
60
61
        // Result is not available via cache
62
        // Calculate the result first
63 2
        $this->initializeExtractor($serviceLocator);
64 2
        $this->setChangeSet($this->extractor->getChangeSet($requestedName));
65
66 2
        if ($this->getChangeSet()->isAnnotated()) {
67
            // Service is annotated to cache results
68 1
            $this->cache->setItem($this->getHash($name), $this->getChangeSet());
69 1
            return true;
70
        }
71
72
        // Service is not annotated
73
        // Cache false for it
74 1
        $this->cache->setItem($this->getHash($name), false);
75 1
        return false;
76
    }
77
78
    /**
79
     * Create service with name
80
     *
81
     * @param ServiceLocatorInterface $serviceLocator
82
     * @param string $name
83
     * @param string $requestedName
84
     * @return object
85
     */
86 1
    public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
87
    {
88 1
        return $this->instantiator->create($serviceLocator, $this->getChangeSet());
89
    }
90
91
    /**
92
     * @param ServiceLocatorInterface $serviceLocator
93
     * @param string $name
94
     * @return bool
95
     */
96 4
    protected function shouldBeAvoided(ServiceLocatorInterface $serviceLocator, $name)
97
    {
98 4
        if (! $this->config) {
99 4
            $this->config = $serviceLocator->get('config')['mxdimodule']['avoid_service'];
100 4
        }
101
102 4
        return isset($this->config[$name]) && $this->config[$name];
103
    }
104
105
    /**
106
     * @param ServiceLocatorInterface $serviceLocator
107
     */
108 2
    protected function initializeExtractor(ServiceLocatorInterface $serviceLocator)
109
    {
110 2
        if (!$this->extractor) {
111
            $this->extractor = $serviceLocator->get('mxdiModule\Extractor');
112
        }
113 2
    }
114
115
    /**
116
     * @param ServiceLocatorInterface $serviceLocator
117
     */
118 3
    protected function initializeCache(ServiceLocatorInterface $serviceLocator)
119
    {
120 3
        if (!$this->cache) {
121 3
            $this->cache = $serviceLocator->get('mxdiModule\Cache');
122 3
        }
123 3
    }
124
125
    /**
126
     * @return mixed|ChangeSet
127
     */
128 4
    public function getChangeSet()
129
    {
130 4
        return $this->changeSet;
131
    }
132
133
    /**
134
     * @param mixed|ChangeSet $changeSet
135
     */
136 4
    public function setChangeSet($changeSet)
137
    {
138 4
        $this->changeSet = $changeSet;
139 4
    }
140
141
    /**
142
     * @param ExtractorInterface $extractor
143
     */
144 5
    public function setExtractor(ExtractorInterface $extractor)
145
    {
146 5
        $this->extractor = $extractor;
147 5
    }
148
}
149