Test Failed
Push — master ( 0864dc...48ec37 )
by Petr
12:45
created

DependenciesFactory   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 35
eloc 48
c 1
b 0
f 0
dl 0
loc 115
rs 9.6

9 Methods

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