Passed
Push — feature/switch-to-configuratio... ( 74ae66 )
by Tomas Norre
07:20
created

Configuration::fromArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 10
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 17
ccs 11
cts 11
cp 1
crap 3
rs 9.9332
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AOE\Crawler\Domain\Model;
6
7
/*
8
 * (c) 2020 AOE GmbH <[email protected]>
9
 *
10
 * This file is part of the TYPO3 Crawler Extension.
11
 *
12
 * It is free software; you can redistribute it and/or modify it under
13
 * the terms of the GNU General Public License, either version 2
14
 * of the License, or any later version.
15
 *
16
 * For the full copyright and license information, please read the
17
 * LICENSE.txt file that was distributed with this source code.
18
 *
19
 * The TYPO3 project - inspiring people to share!
20
 */
21
22
use Assert\Assert;
23
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
24
25
/**
26
 * Class Configuration
27
 */
28
class Configuration extends AbstractEntity
29
{
30
    /**
31
     * @var string
32
     */
33
    protected $name = '';
34
35
    /**
36
     * @var int
37
     */
38
    protected $forceSsl = 1;
39
40
    /**
41
     * @var string
42
     */
43
    protected $processingInstructionFilter = '';
44
45
    /**
46
     * @var string
47
     */
48
    protected $processingInstructionParameters = '';
49
50
    /**
51
     * @var string
52
     */
53
    protected $configuration = '';
54
55
    /**
56
     * @var string
57
     */
58
    protected $baseUrl = '';
59
60
    /**
61
     * @var string
62
     */
63
    protected $pidsonly = '';
64
65
    /**
66
     * @var string
67
     */
68
    protected $begroups = '';
69
70
    /**
71
     * @var string
72
     */
73
    protected $fegroups = '';
74
75
    /**
76
     * @var string
77
     */
78
    protected $exclude = '';
79
80 4
    public function __construct(string $name)
81
    {
82 4
        $this->name = $name;
83 4
    }
84
85 3
    public static function fromArray($array): self
86
    {
87 3
        Assert::that($array)
88 3
            ->keyIsset('name','Name needs to be set to create a new Object from Array');
89
90 3
        $configuration = new self($array['name']);
91
92 3
        foreach($array as $key => $value) {
93 3
            $property = self::getPropertyMap($key);
0 ignored issues
show
Bug Best Practice introduced by
The method AOE\Crawler\Domain\Model...ation::getPropertyMap() is not static, but was called statically. ( Ignorable by Annotation )

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

93
            /** @scrutinizer ignore-call */ 
94
            $property = self::getPropertyMap($key);
Loading history...
94 3
            if (empty($property)) {
95 3
                continue;
96
            }
97 3
            $setProperty = "set$property";
98 3
            $configuration->${setProperty}($value);
0 ignored issues
show
Bug introduced by
The constant AOE\Crawler\Domain\Model\setProperty was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
99
        }
100
101 3
        return $configuration;
102
    }
103
104 3
    private function getPropertyMap($key): string
105
    {
106
        $propertyMap = [
107 3
            'procInstrFilter' => 'ProcessingInstructionFilter',
108
            'baseUrl' => 'BaseUrl',
109
            'procInstrParams.'  => 'ProcessingInstructionParameters',
110
            'force_ssl' => 'ForceSsl',
111
            // Not sure if it be or fegroups
112
            'userGroups' => 'FeGroups',
113
            'exclude' => 'Exclude',
114
            //'key'  => '',
115
            'name'  => 'Name',
116
        ];
117
118 3
        return $propertyMap[$key] ?: '';
119
    }
120
121
    /**
122
     * @return string
123
     */
124 1
    public function getName()
125
    {
126 1
        return $this->name;
127
    }
128
129
    /**
130
     * @param string $name
131
     */
132 4
    public function setName($name): void
133
    {
134 4
        $this->name = $name;
135 4
    }
136
137
    /**
138
     * @return int
139
     */
140 1
    public function isForceSsl()
141
    {
142 1
        return $this->forceSsl;
143
    }
144
145
    /**
146
     * @param int $forceSsl
147
     */
148 1
    public function setForceSsl($forceSsl): void
149
    {
150 1
        $this->forceSsl = $forceSsl;
151 1
    }
152
153
    /**
154
     * @return string
155
     */
156 8
    public function getProcessingInstructionFilter()
157
    {
158 8
        return $this->processingInstructionFilter;
159
    }
160
161
    /**
162
     * @param string $processingInstructionFilter
163
     */
164 4
    public function setProcessingInstructionFilter($processingInstructionFilter): void
165
    {
166 4
        $this->processingInstructionFilter = $processingInstructionFilter;
167 4
    }
168
169
    /**
170
     * @return string|array
171
     */
172 8
    public function getProcessingInstructionParameters()
173
    {
174 8
        return $this->processingInstructionParameters;
175
    }
176
177
    /**
178
     * @param string|array $processingInstructionParameters
179
     */
180 4
    public function setProcessingInstructionParameters($processingInstructionParameters): void
181
    {
182 4
        $this->processingInstructionParameters = $processingInstructionParameters;
0 ignored issues
show
Documentation Bug introduced by
It seems like $processingInstructionParameters can also be of type array. However, the property $processingInstructionParameters is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
183 4
    }
184
185
    /**
186
     * @return string
187
     */
188 1
    public function getConfiguration()
189
    {
190 1
        return $this->configuration;
191
    }
192
193
    /**
194
     * @param string $configuration
195
     */
196 1
    public function setConfiguration($configuration): void
197
    {
198 1
        $this->configuration = $configuration;
199 1
    }
200
201
    /**
202
     * @return string
203
     */
204 1
    public function getBaseUrl()
205
    {
206 1
        return $this->baseUrl;
207
    }
208
209
    /**
210
     * @param string $baseUrl
211
     */
212 4
    public function setBaseUrl($baseUrl): void
213
    {
214 4
        $this->baseUrl = $baseUrl;
215 4
    }
216
217 1
    public function getPidsOnly()
218
    {
219 1
        return $this->pidsonly;
220
    }
221
222 1
    public function setPidsOnly($pidsOnly): void
223
    {
224 1
        $this->pidsonly = $pidsOnly;
225 1
    }
226
227
    /**
228
     * @return string
229
     */
230 1
    public function getBeGroups()
231
    {
232 1
        return $this->begroups;
233
    }
234
235
    /**
236
     * @param string $beGroups
237
     */
238 1
    public function setBeGroups($beGroups): void
239
    {
240 1
        $this->begroups = $beGroups;
241 1
    }
242
243
    /**
244
     * @return string
245
     */
246 8
    public function getFeGroups()
247
    {
248 8
        return $this->fegroups;
249
    }
250
251
    /**
252
     * @param string $feGroups
253
     */
254 1
    public function setFeGroups($feGroups): void
255
    {
256 1
        $this->fegroups = $feGroups;
257 1
    }
258
259
    /**
260
     * @return string
261
     */
262 1
    public function getExclude()
263
    {
264 1
        return $this->exclude;
265
    }
266
267
    /**
268
     * @param string $exclude
269
     */
270 1
    public function setExclude($exclude): void
271
    {
272 1
        $this->exclude = $exclude;
273 1
    }
274
}
275