Completed
Pull Request — master (#20)
by
unknown
06:10
created

Rule::getCreated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 1
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
     * @param int $priority
138
     *
139
     * @throws \Acquia\LiftClient\Exception\LiftSdkException
140
     *
141
     * @return \Acquia\LiftClient\Entity\Rule
142
     */
143 33
    public function setPriority($priority)
144
    {
145 33
        if (!is_integer($priority)) {
146 3
            throw new LiftSdkException('Argument must be an instance of integer.');
147
        }
148 30
        $this['priority'] = $priority;
149
150 30
        return $this;
151
    }
152
153
    /**
154
     * Gets the 'priority' parameter.
155
     *
156
     * @return int The priority of the Rule
157
     */
158 12
    public function getPriority()
159
    {
160 12
        return $this->getEntityValue('priority', 0);
161
    }
162
163
    /**
164
     * Sets the 'weight' parameter.
165
     *
166
     * @deprecated use the priority parameter instead.
167
     * @param int $weight
168
     *
169
     * @throws \Acquia\LiftClient\Exception\LiftSdkException
170
     *
171
     * @return \Acquia\LiftClient\Entity\Rule
172 30
     */
173
    public function setWeight($weight)
174 30
    {
175 30
        return $this->setPriority($weight)
176
    }
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected '}', expecting ';'
Loading history...
177 30
178 20
    /**
179
     * Gets the 'weight' parameter.
180 30
     *
181
     * @deprecated use the priority parameter instead.
182
     * @return int The weight of the Rule
183
     */
184
    public function getWeight()
185
    {
186
        return $this->getPriority();
187
    }
188 12
189
    /**
190 12
     * Sets the 'content' parameter.
191 12
     *
192 12
     * @param Content[] $contentList
193 12
     *
194 8
     * @throws \Acquia\LiftClient\Exception\LiftSdkException
195
     *
196 12
     * @return \Acquia\LiftClient\Entity\Rule
197
     */
198
    public function setContentList(array $contentList)
199
    {
200
        $this['content'] = [];
201
        foreach ($contentList as $content) {
202
            // We need to 'normalize' the data.
203
            $this['content'][] = $content->getArrayCopy();
204
        }
205
206
        return $this;
207
    }
208 33
209
    /**
210 33
     * Gets the 'content' parameter.
211 3
     *
212
     * @return Content[] The list of content this rule applies to
213 30
     */
214
    public function getContentList()
215 30
    {
216
        $contentList = $this->getEntityValue('content', '');
217
        $ret = [];
218
        foreach ($contentList as $content) {
219
            $ret[] = new Content($content);
220
        }
221
222
        return $ret;
223 12
    }
224
225 12
    /**
226
     * Sets the 'segment' parameter.
227
     *
228
     * @param string $segment
229
     *
230
     * @throws \Acquia\LiftClient\Exception\LiftSdkException
231
     *
232
     * @return \Acquia\LiftClient\Entity\Rule
233
     */
234
    public function setSegmentId($segment)
235
    {
236
        if (!is_string($segment)) {
237 36
            throw new LiftSdkException('Argument must be an instance of string.');
238
        }
239 36
        $this['segment'] = $segment;
240 3
241
        return $this;
242 33
    }
243 3
244
    /**
245 30
     * Gets the 'segment' parameter.
246
     *
247 30
     * @return string
248
     */
249
    public function getSegmentId()
250
    {
251
        return $this->getEntityValue('segment', '');
252
    }
253
254
    /**
255 12
     * Sets the 'status' parameter.
256
     *
257 12
     * @param string $status
258
     *
259
     * @throws \Acquia\LiftClient\Exception\LiftSdkException
260
     *
261
     * @return \Acquia\LiftClient\Entity\Rule
262
     */
263
    public function setStatus($status)
264
    {
265 12
        if (!is_string($status)) {
266
            throw new LiftSdkException('Argument must be an instance of string.');
267 12
        }
268
        if ($status !== 'published' && $status !== 'unpublished' && $status !== 'archived') {
269
            throw new LiftSdkException('Status much be either published, unpublished or archived.');
270
        }
271 12
        $this['status'] = $status;
272
273 12
        return $this;
274
    }
275
276
    /**
277
     * Gets the 'status' parameter.
278
     *
279
     * @return string
280
     */
281 12
    public function getStatus()
282
    {
283 12
        return $this->getEntityValue('status', '');
284
    }
285
286
    /**
287 12
     * Gets the 'created' parameter.
288
     *
289 12
     * @return DateTime|false
290
     */
291
    public function getCreated()
292
    {
293
        $date = $this->getEntityValue('created', 0);
294
        // The ISO8601 DateTime format is not compatible with ISO-8601, but is left this way for backward compatibility
295
        // reasons. Use DateTime::ATOM or DATE_ATOM for compatibility with ISO-8601 instead.
296
        // See http://php.net/manual/en/class.datetime.php
297
        $datetime = DateTime::createFromFormat(DateTime::ATOM, $date);
298
299 30
        return $datetime;
300
    }
301
302
    /**
303
     * Gets the 'updated' parameter.
304
     *
305
     * @return DateTime|false
306
     */
307 30
    public function getUpdated()
308
    {
309
        $date = $this->getEntityValue('updated', 0);
310 30
        // The ISO8601 DateTime format is not compatible with ISO-8601, but is left this way for backward compatibility
311
        // reasons. Use DateTime::ATOM or DATE_ATOM for compatibility with ISO-8601 instead.
312 30
        // See http://php.net/manual/en/class.datetime.php
313 30
        $datetime = DateTime::createFromFormat(DateTime::ATOM, $date);
314 30
315 3
        return $datetime;
316 3
    }
317 3
318 3
    /**
319 3
     * Sets the Rule test_config property.
320 3
     *
321
     * @param \Acquia\LiftClient\Entity\TestConfigInterface $testConfig
322
     *
323 30
     * @return \Acquia\LiftClient\Entity\Rule
324
     */
325
    public function setTestConfig(TestConfigInterface $testConfig)
326
    {
327
        // To facilitate TypeHinting in PHPStorm we redefine what $testConfig is
328
        // here. We know it inherits from the TestConfigInterface and is a child
329
        // of TestConfigBase.
330
        /** @var \Acquia\LiftClient\Entity\TestConfigBase $testConfig */
331 12
332
        // Get class of the testConfig object.
333 12
        $type = get_class($testConfig);
334
335
        // Only allow one test type at a time.
336 12
        $this['testconfig'] = [];
337 12
        switch ($type) {
338
            case 'Acquia\LiftClient\Entity\TestConfigAb':
339
                $this['testconfig']['ab'] = $testConfig->getArrayCopy();
340
                break;
341 12
            case 'Acquia\LiftClient\Entity\TestConfigMab':
342 12
                $this['testconfig']['mab'] = $testConfig->getArrayCopy();
343
                break;
344
            case 'Acquia\LiftClient\Entity\TestConfigTarget':
345
                $this['testconfig']['target'] = $testConfig->getArrayCopy();
346
                break;
347
        }
348
349
        return $this;
350
    }
351
352
    /**
353
     * Gets the 'test_config' parameter.
354
     *
355
     * @return \Acquia\LiftClient\Entity\TestConfigInterface|null $testConfig
356
     */
357
    public function getTestConfig()
358
    {
359
        $testConfig = $this->getEntityValue('testconfig', []);
360
        // We know the array only has one possible set of options.
361
        // Get its key and value.
362
        reset($testConfig);
363
        $key = key($testConfig);
364
365
        // Based on the config, we load the different objects.
366
        switch ($key) {
367
            case 'ab':
368
                return new TestConfigAb($testConfig[$key]);
369
            case 'mab':
370
                return new TestConfigMab($testConfig[$key]);
371
            case 'target':
372
                return new TestConfigTarget($testConfig[$key]);
373
        }
374
375
        return null;
376
    }
377
}
378