DtStartTransformer::transform()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 9.6111
cc 5
nc 6
nop 1
crap 5
1
<?php
2
3
namespace Recurrence\Rrule\Transformer;
4
5
use Recurrence\Rrule\Extractor\DtStartExtractor;
6
use Recurrence\Model\Exception\InvalidRruleException;
7
8
class DtStartTransformer implements RruleTransformerInterface
9
{
10
    /**
11
     * @throws InvalidRruleException
12
     */
13
    public function transform(array $values): \DateTime
14
    {
15 1
        $this->validate($values);
16
17 1
        if (count($values) == 2 && substr($values[1], -1) == 'Z') {
18 1
            $dtStart = \DateTime::createFromFormat('Ymd\THis\Z', $values[0], new \DateTimeZone('UTC'));
19 1
        } elseif (count($values) == 2) {
20 1
            $dtStart = \DateTime::createFromFormat('Ymd\THis', $values[0]);
21
        } else {
22 1
            $dtStart = \DateTime::createFromFormat('Ymd', $values[0]);
23
        }
24
25 1
        if ($dtStart === false) {
26 1
            throw new InvalidRruleException(DtStartExtractor::RRULE_PARAMETER, (string) $values[0]);
27
        }
28
29 1
        return $dtStart;
30
    }
31
32
    /**
33
     * @throws InvalidRruleException
34
     */
35
    protected function validate(array $values): void
36
    {
37 1
        if (!isset($values[0])) {
38 1
            throw new InvalidRruleException(DtStartExtractor::RRULE_PARAMETER);
39
        }
40 1
    }
41
}
42