Passed
Push — master ( 221c35...fdb910 )
by Petr
02:59
created

DependenciesFactory::getNormalizerFactory()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 12
c 0
b 0
f 0
ccs 0
cts 7
cp 0
rs 10
cc 3
nc 3
nop 0
crap 12
1
<?php
2
3
declare(strict_types=1);
4
5
namespace kalanis\Pohoda\DI;
6
7
use kalanis\Pohoda\Common;
8
use kalanis\Pohoda\PrintRequest\ParameterInstances;
9
use kalanis\Pohoda\ValueTransformer;
10
use Psr\Container\ContainerInterface;
11
12
class DependenciesFactory
13
{
14 210
    public function __construct(
15
        protected ?Common\NamespacesPaths $namespacesPaths = null,
16
        protected ?ValueTransformer\SanitizeEncoding $sanitizeEncoding = null,
17
        protected ?Common\OptionsResolver\Normalizers\NormalizerFactory $normalizerFactory = null,
18
        protected readonly ?ContainerInterface $container = null,
19
        protected ?ParameterInstances $parameterInstances = null,
20 210
    ) {}
21
22 6
    public function getAgendaFactory(): AgendaFactoryInterface
23
    {
24 6
        if (empty($this->container) && (empty($this->namespacesPaths) || empty($this->sanitizeEncoding) || empty($this->normalizerFactory))) {
25
            throw new \LogicException('You must have at least one full set of dependencies to get Agenda factory');
26
        }
27 6
        return $this->container
28
            ? new AgendaDIFactory($this->container)
29 6
            : new AgendaReflectFactory($this)
30 6
        ;
31
    }
32
33 54
    public function getDocumentPartFactory(): DocumentPartFactoryInterface
34
    {
35 54
        if (empty($this->container) && (empty($this->namespacesPaths) || empty($this->sanitizeEncoding) || empty($this->normalizerFactory))) {
36
            throw new \LogicException('You must have at least one full set of dependencies to get Agenda factory');
37
        }
38 54
        return $this->container
39
            ? new DocumentPartDIFactory($this->container)
40 54
            : new DocumentPartReflectFactory($this)
41 54
        ;
42
    }
43
44 7
    public function getParametersFactory(): ParameterFactoryInterface
45
    {
46 7
        if (empty($this->container) && (empty($this->namespacesPaths) || empty($this->sanitizeEncoding) || empty($this->normalizerFactory))) {
47
            throw new \LogicException('You must have at least one full set of dependencies to get Parameter factory');
48
        }
49 7
        return $this->container
50
            ? new ParameterDIFactory($this->container)
51 7
            : new ParameterReflectFactory($this)
52 7
        ;
53
    }
54
55 128
    public function getNamespacePaths(): Common\NamespacesPaths
56
    {
57 128
        if (!empty($this->namespacesPaths)) {
58 128
            return $this->namespacesPaths;
59
        }
60
61
        $instance = $this->getInstances(Common\NamespacesPaths::class);
62
        if (!$instance instanceof Common\NamespacesPaths) {
63
            throw new \LogicException('Container does not return NamespacesPaths class!');
64
        }
65
66
        return $this->namespacesPaths = $instance;
67
    }
68
69 128
    public function getSanitizeEncoding(): ValueTransformer\SanitizeEncoding
70
    {
71 128
        if (!empty($this->sanitizeEncoding)) {
72 128
            return $this->sanitizeEncoding;
73
        }
74
75
        $instance = $this->getInstances(ValueTransformer\SanitizeEncoding::class);
76
        if (!$instance instanceof ValueTransformer\SanitizeEncoding) {
77
            throw new \LogicException('Container does not return SanitizeEncoding class!');
78
        }
79
80
        return $this->sanitizeEncoding = $instance;
81
    }
82
83
    public function getNormalizerFactory(): Common\OptionsResolver\Normalizers\NormalizerFactory
84
    {
85
        if (!empty($this->normalizerFactory)) {
86
            return $this->normalizerFactory;
87
        }
88
89
        $instance = $this->getInstances(Common\OptionsResolver\Normalizers\NormalizerFactory::class);
90
        if (!$instance instanceof Common\OptionsResolver\Normalizers\NormalizerFactory) {
91
            throw new \LogicException('Container does not return NormalizerFactory class!');
92
        }
93
94
        return $this->normalizerFactory = $instance;
95
    }
96
97 7
    public function getParameterInstances(): ParameterInstances
98
    {
99 7
        if (!empty($this->parameterInstances)) {
100 7
            return $this->parameterInstances;
101
        }
102
103
        $instance = $this->getInstances(ParameterInstances::class);
104
        if (!$instance instanceof ParameterInstances) {
105
            throw new \LogicException('Container does not return ParameterInstances class!');
106
        }
107
108
        return $this->parameterInstances = $instance;
109
    }
110
111
    protected function getInstances(string $className): object
112
    {
113
        if (empty($this->container)) {
114
            throw new \LogicException('No DI available, you must set the ' . $className . ' class first!');
115
        }
116
117
        if (!$this->container->has($className)) {
118
            throw new \LogicException('Container does not have ' . $className . ' class!');
119
        }
120
121
        $instance = $this->container->get($className);
122
        if (!is_object($instance)) {
123
            throw new \LogicException('Container does not return ' . $className . ' class!');
124
        }
125
126
        return $instance;
127
    }
128
}
129