Passed
Pull Request — master (#16)
by
unknown
14:00
created

Provider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Aoe\Asdis\System\Configuration;
4
5
use Aoe\Asdis\System\Configuration\Exception\InvalidStructure;
6
7
/**
8
 * Provides all configuration settings.
9
 */
10
class Provider
11
{
12
    private ?TypoScriptConfiguration $typoScriptConfiguration = null;
13
14
    public function __construct(TypoScriptConfiguration $typoScriptConfiguration)
15
    {
16
        $this->typoScriptConfiguration = $typoScriptConfiguration;
17
    }
18
19
    /*
20
    public function injectTypoScriptConfiguration(TypoScriptConfiguration $typoScriptConfiguration): void
21
    {
22
        $this->typoScriptConfiguration = $typoScriptConfiguration;
23
    }
24
    */
25
26
    /**
27
     * Tells if the assets on the current page should be replaced.
28
     */
29
    public function isReplacementEnabled(): bool
30
    {
31
        return (bool) ((int) $this->typoScriptConfiguration->getSetting('enabled'));
0 ignored issues
show
Bug introduced by
The method getSetting() does not exist on null. ( Ignorable by Annotation )

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

31
        return (bool) ((int) $this->typoScriptConfiguration->/** @scrutinizer ignore-call */ getSetting('enabled'));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
32
    }
33
34
    /**
35
     * This disables any processing in hook handlers of the asdis extension.
36
     * You can use this, if you have to implement your own hook processing in another extension.
37
     */
38
    public function isDefaultHookHandlingDisabled(): bool
39
    {
40
        return (bool) ((int) $this->typoScriptConfiguration->getSetting('disableDefaultHookHandling'));
41
    }
42
43
    public function getDistributionAlgorithmKey(): string
44
    {
45
        return (string) $this->typoScriptConfiguration->getSetting('distributionAlgorithm');
46
    }
47
48
    /**
49
     * Returns the scraper keys for the current page.
50
     *
51
     * @return array
52
     */
53
    public function getScraperKeys()
54
    {
55
        $keyList = $this->typoScriptConfiguration->getSetting('scrapers', 'string');
56
        $keys = explode(',', $keyList);
57
58
        if (!is_array($keys)) {
0 ignored issues
show
introduced by
The condition is_array($keys) is always true.
Loading history...
59
            return [];
60
        }
61
        if (count($keys) < 1) {
62
            return [];
63
        }
64
        $scraperKeys = [];
65
        foreach ($keys as $key) {
66
            $scraperKeys[] = trim($key);
67
        }
68
        return $scraperKeys;
69
    }
70
71
    /**
72
     * Returns the filter keys for the current page.
73
     *
74
     * @return array
75
     */
76
    public function getFilterKeys()
77
    {
78
        $keyList = $this->typoScriptConfiguration->getSetting('filters', 'string');
79
        $keys = explode(',', $keyList);
80
81
        if (!is_array($keys)) {
0 ignored issues
show
introduced by
The condition is_array($keys) is always true.
Loading history...
82
            return [];
83
        }
84
        if (count($keys) < 1) {
85
            return [];
86
        }
87
        $filterKeys = [];
88
        foreach ($keys as $key) {
89
            $filterKeys[] = trim($key);
90
        }
91
        return $filterKeys;
92
    }
93
94
    /**
95
     * Returns an array like this:
96
     * array(
97
     *     array(
98
     *         'identifier' => 'media1',
99
     *         'domain'     => 'm1.mydomain.com',
100
     *         'protocol'   => 'dynamic'
101
     *     ),
102
     *     array(
103
     *         'identifier' => 'media2',
104
     *         'domain'     => 'm2.mydomain.com',
105
     *         'protocol'   => 'http'
106
     *     )
107
     * )
108
     *
109
     * @return array
110
     * @throws InvalidStructure
111
     */
112
    public function getServerDefinitions()
113
    {
114
        $definitions = [];
115
        $serverDefinitions = $this->typoScriptConfiguration->getSetting('servers', 'array', true);
116
        foreach ($serverDefinitions as $identifier => $serverDefinition) {
117
            if (!is_array($serverDefinition) || !isset($serverDefinition['domain'])) {
118
                throw new InvalidStructure(
119
                    'Configured server definition for "' . $serverDefinition . '" is invalid.',
120
                    1_372_159_113_552
0 ignored issues
show
Bug introduced by
The constant Aoe\Asdis\System\Configuration\1_372_159_113_552 was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
121
                );
122
            }
123
            if (!isset($serverDefinition['protocol'])) {
124
                $serverDefinition['protocol'] = 'marker';
125
            }
126
            $definitions[] = [
127
                'identifier' => $identifier,
128
                'domain' => $serverDefinition['domain'],
129
                'protocol' => $serverDefinition['protocol'],
130
            ];
131
        }
132
        return $definitions;
133
    }
134
135
    public function getServerProtocolMarker(): string
136
    {
137
        return $this->typoScriptConfiguration->getSetting('serverProtocolMarker', 'string');
138
    }
139
}
140