Passed
Pull Request — master (#16)
by
unknown
13:19 queued 10:34
created

AbstractDeclarationBasedFactory   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Test Coverage

Coverage 71.74%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 45
c 1
b 0
f 0
dl 0
loc 114
ccs 33
cts 46
cp 0.7174
rs 10
wmc 18

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setFallbackKey() 0 3 1
A injectConfigurationProvider() 0 3 1
A setClassImplements() 0 3 1
A getDeclarationByKey() 0 8 3
A addDeclaration() 0 13 4
A setDeclarations() 0 5 2
A buildObjectFromKey() 0 29 6
1
<?php
2
3
namespace Aoe\Asdis\System\Factory;
4
5
use Aoe\Asdis\System\Configuration\Provider;
6
use Aoe\Asdis\System\Factory\Exception\DeclarationNotFound;
7
use Aoe\Asdis\System\Factory\Exception\InvalidDeclaration;
8
use Aoe\Asdis\System\Factory\Exception\MissingImplementation;
9
use TYPO3\CMS\Core\Utility\GeneralUtility;
10
11
/**
12
 * Abstract factory class for factories that create their products from array based declarations.
13
 */
14
abstract class AbstractDeclarationBasedFactory
15
{
16
    /**
17
     * @var string
18
     */
19
    public const DECLARATION_KEY = 'key';
20
21
    /**
22
     * @var string
23
     */
24
    public const DECLARATION_CLASS = 'class';
25
26
    /**
27
     * @var string
28
     */
29
    public const DECLARATION_FILE = 'file';
30
31
    protected ?Provider $configurationProvider = null;
32
33
    private ?string $fallbackKey = null;
34
35
    private array $declarations = [];
36
37
    private array $classImplements = [];
38
39 2
    public function injectConfigurationProvider(Provider $configurationProvider): void
40
    {
41 2
        $this->configurationProvider = $configurationProvider;
42 2
    }
43
44 3
    protected function setClassImplements(array $classImplements): void
45
    {
46 3
        $this->classImplements = $classImplements;
47 3
    }
48
49 3
    protected function setDeclarations(array $declarations): void
50
    {
51 3
        $this->declarations = [];
52 3
        foreach ($declarations as $declaration) {
53 2
            $this->addDeclaration($declaration);
54
        }
55 3
    }
56
57 1
    protected function setFallbackKey(string $fallbackKey): void
58
    {
59 1
        $this->fallbackKey = $fallbackKey;
60 1
    }
61
62
    /**
63
     * @return object
64
     * @throws DeclarationNotFound
65
     * @throws MissingImplementation
66
     */
67 1
    protected function buildObjectFromKey(string $key)
68
    {
69 1
        $declaration = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $declaration is dead and can be removed.
Loading history...
70
        try {
71 1
            $declaration = $this->getDeclarationByKey($key);
72
        } catch (DeclarationNotFound $declarationNotFound) {
73
            if (!isset($this->fallbackKey)) {
74
                throw $declarationNotFound;
75
            }
76
            $declaration = $this->getDeclarationByKey($this->fallbackKey);
0 ignored issues
show
Bug introduced by
It seems like $this->fallbackKey can also be of type null; however, parameter $key of Aoe\Asdis\System\Factory...::getDeclarationByKey() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
            $declaration = $this->getDeclarationByKey(/** @scrutinizer ignore-type */ $this->fallbackKey);
Loading history...
77
        }
78
79 1
        if (!class_exists($declaration[self::DECLARATION_CLASS])) {
80
            require_once $declaration[self::DECLARATION_FILE];
81
        }
82
83 1
        $object = GeneralUtility::makeInstance($declaration[self::DECLARATION_CLASS]);
84 1
        $implemented = class_implements($object);
85 1
        foreach ($this->classImplements as $classImplement) {
86 1
            if (!in_array($classImplement, $implemented)) {
87
                throw new MissingImplementation(
88
                    $declaration[self::DECLARATION_CLASS],
89
                    $classImplement,
90
                    static::class,
91
                    1_372_770_673_456
0 ignored issues
show
Bug introduced by
The constant Aoe\Asdis\System\Factory\1_372_770_673_456 was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
92
                );
93
            }
94
        }
95 1
        return $object;
96
    }
97
98
    /**
99
     * @throws InvalidDeclaration
100
     */
101 2
    private function addDeclaration(array $declaration): void
102
    {
103
        if (
104 2
            !array_key_exists(self::DECLARATION_KEY, $declaration) ||
105 2
            !array_key_exists(self::DECLARATION_CLASS, $declaration) ||
106 2
            !array_key_exists(self::DECLARATION_FILE, $declaration)
107
        ) {
108
            throw new InvalidDeclaration(
109
                'Missing declaration element.',
110
                1_372_422_185_108
0 ignored issues
show
Bug introduced by
The constant Aoe\Asdis\System\Factory\1_372_422_185_108 was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
111
            );
112
        }
113 2
        $this->declarations[] = $declaration;
114 2
    }
115
116
    /**
117
     * @return mixed
118
     * @throws DeclarationNotFound
119
     */
120 1
    private function getDeclarationByKey(string $key)
121
    {
122 1
        foreach ($this->declarations as $declaration) {
123 1
            if (strcmp($declaration[self::DECLARATION_KEY], $key) === 0) {
124 1
                return $declaration;
125
            }
126
        }
127
        throw new DeclarationNotFound($key, 1_372_422_430_920);
0 ignored issues
show
Bug introduced by
The constant Aoe\Asdis\System\Factory\1_372_422_430_920 was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
128
    }
129
}
130