Rule   A
last analyzed

Complexity

Total Complexity 41

Size/Duplication

Total Lines 372
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 94.95%

Importance

Changes 0
Metric Value
wmc 41
lcom 1
cbo 6
dl 0
loc 372
ccs 94
cts 99
cp 0.9495
rs 9.1199
c 0
b 0
f 0

23 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setId() 0 9 2
A getId() 0 4 1
A setLabel() 0 9 2
A getLabel() 0 4 1
A setDescription() 0 9 2
A getDescription() 0 4 1
A setSlotId() 0 9 2
A getSlotId() 0 4 1
A setPriority() 0 9 2
A getPriority() 0 4 1
A setWeight() 0 4 1
A getWeight() 0 4 1
A setContentList() 0 10 2
A getContentList() 0 10 2
A setSegmentId() 0 9 2
A getSegmentId() 0 4 1
A setStatus() 0 12 5
A getStatus() 0 4 1
A getCreated() 0 10 1
A getUpdated() 0 10 1
A setTestConfig() 0 26 4
A getTestConfig() 0 20 4

How to fix   Complexity   

Complex Class

Complex classes like Rule often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Rule, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Acquia\LiftClient\Entity;
4
5
use Acquia\LiftClient\Exception\LiftSdkException;
6
use DateTime;
7
8
class Rule extends Entity
9
{
10
    /**
11
     * @param array $array
12
     */
13 84
    public function __construct(array $array = [])
14
    {
15 84
        parent::__construct($array);
16 84
    }
17
18
    /**
19
     * Sets the 'id' parameter.
20
     *
21
     * @param string $id
22
     *
23
     * @throws \Acquia\LiftClient\Exception\LiftSdkException
24
     *
25
     * @return \Acquia\LiftClient\Entity\Rule
26
     */
27 33
    public function setId($id)
28
    {
29 33
        if (!is_string($id)) {
30 3
            throw new LiftSdkException('Argument must be an instance of string.');
31
        }
32 30
        $this['id'] = $id;
33
34 30
        return $this;
35
    }
36
37
    /**
38
     * Gets the 'id' parameter.
39
     *
40
     * @return string The Identifier of the Rule
41
     */
42 12
    public function getId()
43
    {
44 12
        return $this->getEntityValue('id', '');
45
    }
46
47
    /**
48
     * Sets the 'label' parameter.
49
     *
50
     * @param string $label
51
     *
52
     * @throws \Acquia\LiftClient\Exception\LiftSdkException
53
     *
54
     * @return \Acquia\LiftClient\Entity\Rule
55
     */
56 33
    public function setLabel($label)
57
    {
58 33
        if (!is_string($label)) {
59 3
            throw new LiftSdkException('Argument must be an instance of string.');
60
        }
61 30
        $this['label'] = $label;
62
63 30
        return $this;
64
    }
65
66
    /**
67
     * Gets the 'label' parameter.
68
     *
69
     * @return string
70
     */
71 12
    public function getLabel()
72
    {
73 12
        return $this->getEntityValue('label', '');
74
    }
75
76
    /**
77
     * Sets the 'description' parameter.
78
     *
79
     * @param string $description
80
     *
81
     * @throws \Acquia\LiftClient\Exception\LiftSdkException
82
     *
83
     * @return \Acquia\LiftClient\Entity\Rule
84
     */
85 33
    public function setDescription($description)
86
    {
87 33
        if (!is_string($description)) {
88 3
            throw new LiftSdkException('Argument must be an instance of string.');
89
        }
90 30
        $this['description'] = $description;
91
92 30
        return $this;
93
    }
94
95
    /**
96
     * Gets the 'description' parameter.
97
     *
98
     * @return string The Description of the Rule
99
     */
100 12
    public function getDescription()
101
    {
102 12
        return $this->getEntityValue('description', '');
103
    }
104
105
    /**
106
     * Sets the 'slot_id' parameter.
107
     *
108
     * @param string $slotId
109
     *
110
     * @throws \Acquia\LiftClient\Exception\LiftSdkException
111
     *
112
     * @return \Acquia\LiftClient\Entity\Rule
113
     */
114 33
    public function setSlotId($slotId)
115
    {
116 33
        if (!is_string($slotId)) {
117 3
            throw new LiftSdkException('Argument must be an instance of string.');
118
        }
119 30
        $this['slot_id'] = $slotId;
120
121 30
        return $this;
122
    }
123
124
    /**
125
     * Gets the 'slot_id' parameter.
126
     *
127
     * @return string The Description of the Rule
128
     */
129 12
    public function getSlotId()
130
    {
131 12
        return $this->getEntityValue('slot_id', '');
132
    }
133
134
    /**
135
     * Sets the 'priority' parameter.
136
     *
137
     * Higher number means the rule display / apply first.
138
     *
139
     * @param int $priority
140
     *
141
     * @throws \Acquia\LiftClient\Exception\LiftSdkException
142
     *
143
     * @return \Acquia\LiftClient\Entity\Rule
144
     */
145 33
    public function setPriority($priority)
146
    {
147 33
        if (!is_integer($priority)) {
148 3
            throw new LiftSdkException('Argument must be an instance of integer.');
149
        }
150 30
        $this['priority'] = $priority;
151
152 30
        return $this;
153
    }
154
155
    /**
156
     * Gets the 'priority' parameter.
157
     *
158
     * @return int The priority of the Rule
159
     */
160 12
    public function getPriority()
161
    {
162 12
        return $this->getEntityValue('priority', 0);
163
    }
164
165
    /**
166
     * Sets the 'weight' parameter.
167
     *
168
     * @deprecated use the priority parameter instead.
169
     * @param int $weight
170
     *
171
     * @throws \Acquia\LiftClient\Exception\LiftSdkException
172
     *
173
     * @return \Acquia\LiftClient\Entity\Rule
174
     */
175
    public function setWeight($weight)
176
    {
177
        return $this->setPriority($weight);
178
    }
179
180
    /**
181
     * Gets the 'weight' parameter.
182
     *
183
     * @deprecated use the priority parameter instead.
184
     * @return int The weight of the Rule
185
     */
186
    public function getWeight()
187
    {
188
        return $this->getPriority();
189
    }
190
191
    /**
192
     * Sets the 'content' parameter.
193
     *
194
     * @param Content[] $contentList
195
     *
196
     * @throws \Acquia\LiftClient\Exception\LiftSdkException
197
     *
198
     * @return \Acquia\LiftClient\Entity\Rule
199
     */
200 30
    public function setContentList(array $contentList)
201
    {
202 30
        $this['content'] = [];
203 30
        foreach ($contentList as $content) {
204
            // We need to 'normalize' the data.
205 30
            $this['content'][] = $content->getArrayCopy();
206 20
        }
207
208 30
        return $this;
209
    }
210
211
    /**
212
     * Gets the 'content' parameter.
213
     *
214
     * @return Content[] The list of content this rule applies to
215
     */
216 12
    public function getContentList()
217
    {
218 12
        $contentList = $this->getEntityValue('content', '');
219 12
        $ret = [];
220 12
        foreach ($contentList as $content) {
221 12
            $ret[] = new Content($content);
222 8
        }
223
224 12
        return $ret;
225
    }
226
227
    /**
228
     * Sets the 'segment' parameter.
229
     *
230
     * @param string $segment
231
     *
232
     * @throws \Acquia\LiftClient\Exception\LiftSdkException
233
     *
234
     * @return \Acquia\LiftClient\Entity\Rule
235
     */
236 33
    public function setSegmentId($segment)
237
    {
238 33
        if (!is_string($segment)) {
239 3
            throw new LiftSdkException('Argument must be an instance of string.');
240
        }
241 30
        $this['segment'] = $segment;
242
243 30
        return $this;
244
    }
245
246
    /**
247
     * Gets the 'segment' parameter.
248
     *
249
     * @return string
250
     */
251 12
    public function getSegmentId()
252
    {
253 12
        return $this->getEntityValue('segment', '');
254
    }
255
256
    /**
257
     * Sets the 'status' parameter.
258
     *
259
     * @param string $status
260
     *
261
     * @throws \Acquia\LiftClient\Exception\LiftSdkException
262
     *
263
     * @return \Acquia\LiftClient\Entity\Rule
264
     */
265 36
    public function setStatus($status)
266
    {
267 36
        if (!is_string($status)) {
268 3
            throw new LiftSdkException('Argument must be an instance of string.');
269
        }
270 33
        if ($status !== 'published' && $status !== 'unpublished' && $status !== 'archived') {
271 3
            throw new LiftSdkException('Status much be either published, unpublished or archived.');
272
        }
273 30
        $this['status'] = $status;
274
275 30
        return $this;
276
    }
277
278
    /**
279
     * Gets the 'status' parameter.
280
     *
281
     * @return string
282
     */
283 12
    public function getStatus()
284
    {
285 12
        return $this->getEntityValue('status', '');
286
    }
287
288
    /**
289
     * Gets the 'created' parameter.
290
     *
291
     * @return DateTime|false
292
     */
293 12
    public function getCreated()
294
    {
295 12
        $date = $this->getEntityValue('created', 0);
296
        // The ISO8601 DateTime format is not compatible with ISO-8601, but is left this way for backward compatibility
297
        // reasons. Use DateTime::ATOM or DATE_ATOM for compatibility with ISO-8601 instead.
298
        // See http://php.net/manual/en/class.datetime.php
299 12
        $datetime = DateTime::createFromFormat(DateTime::ATOM, $date);
300
301 12
        return $datetime;
302
    }
303
304
    /**
305
     * Gets the 'updated' parameter.
306
     *
307
     * @return DateTime|false
308
     */
309 12
    public function getUpdated()
310
    {
311 12
        $date = $this->getEntityValue('updated', 0);
312
        // The ISO8601 DateTime format is not compatible with ISO-8601, but is left this way for backward compatibility
313
        // reasons. Use DateTime::ATOM or DATE_ATOM for compatibility with ISO-8601 instead.
314
        // See http://php.net/manual/en/class.datetime.php
315 12
        $datetime = DateTime::createFromFormat(DateTime::ATOM, $date);
316
317 12
        return $datetime;
318
    }
319
320
    /**
321
     * Sets the Rule test_config property.
322
     *
323
     * @param \Acquia\LiftClient\Entity\TestConfigInterface $testConfig
324
     *
325
     * @return \Acquia\LiftClient\Entity\Rule
326
     */
327 30
    public function setTestConfig(TestConfigInterface $testConfig)
328
    {
329
        // To facilitate TypeHinting in PHPStorm we redefine what $testConfig is
330
        // here. We know it inherits from the TestConfigInterface and is a child
331
        // of TestConfigBase.
332
        /** @var \Acquia\LiftClient\Entity\TestConfigBase $testConfig */
333
334
        // Get class of the testConfig object.
335 30
        $type = get_class($testConfig);
336
337
        // Only allow one test type at a time.
338 30
        $this['testconfig'] = [];
339
        switch ($type) {
340 30
            case 'Acquia\LiftClient\Entity\TestConfigAb':
341 30
                $this['testconfig']['ab'] = $testConfig->getArrayCopy();
342 30
                break;
343 3
            case 'Acquia\LiftClient\Entity\TestConfigMab':
344 3
                $this['testconfig']['mab'] = $testConfig->getArrayCopy();
345 3
                break;
346 3
            case 'Acquia\LiftClient\Entity\TestConfigTarget':
347 3
                $this['testconfig']['target'] = $testConfig->getArrayCopy();
348 3
                break;
349
        }
350
351 30
        return $this;
352
    }
353
354
    /**
355
     * Gets the 'test_config' parameter.
356
     *
357
     * @return \Acquia\LiftClient\Entity\TestConfigInterface|null $testConfig
358
     */
359 12
    public function getTestConfig()
360
    {
361 12
        $testConfig = $this->getEntityValue('testconfig', []);
362
        // We know the array only has one possible set of options.
363
        // Get its key and value.
364 12
        reset($testConfig);
365 12
        $key = key($testConfig);
366
367
        // Based on the config, we load the different objects.
368
        switch ($key) {
369 12
            case 'ab':
370 12
                return new TestConfigAb($testConfig[$key]);
371
            case 'mab':
372
                return new TestConfigMab($testConfig[$key]);
373
            case 'target':
374
                return new TestConfigTarget($testConfig[$key]);
375
        }
376
377
        return null;
378
    }
379
}
380