DependenciesFactory::getInstances()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 16
c 0
b 0
f 0
ccs 0
cts 9
cp 0
rs 10
cc 4
nc 4
nop 1
crap 20
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 kalanis\PohodaException;
11
use Psr\Container\ContainerInterface;
12
13
class DependenciesFactory
14
{
15 210
    public function __construct(
16
        protected ?Common\NamespacesPaths $namespacesPaths = null,
17
        protected ?ValueTransformer\SanitizeEncoding $sanitizeEncoding = 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))) {
25
            throw new PohodaException('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))) {
36
            throw new PohodaException('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))) {
47
            throw new PohodaException('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 PohodaException('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 PohodaException('Container does not return SanitizeEncoding class!');
78
        }
79
80
        return $this->sanitizeEncoding = $instance;
81
    }
82
83 7
    public function getParameterInstances(): ParameterInstances
84
    {
85 7
        if (!empty($this->parameterInstances)) {
86 7
            return $this->parameterInstances;
87
        }
88
89
        $instance = $this->getInstances(ParameterInstances::class);
90
        if (!$instance instanceof ParameterInstances) {
91
            throw new PohodaException('Container does not return ParameterInstances class!');
92
        }
93
94
        return $this->parameterInstances = $instance;
95
    }
96
97
    protected function getInstances(string $className): object
98
    {
99
        if (empty($this->container)) {
100
            throw new PohodaException('No DI available, you must set the ' . $className . ' class first!');
101
        }
102
103
        if (!$this->container->has($className)) {
104
            throw new PohodaException('Container does not have ' . $className . ' class!');
105
        }
106
107
        $instance = $this->container->get($className);
108
        if (!is_object($instance)) {
109
            throw new PohodaException('Container does not return ' . $className . ' class!');
110
        }
111
112
        return $instance;
113
    }
114
}
115