Passed
Push — master ( ed2711...5bfb66 )
by Samuel
02:22
created

DtStartTransformer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 13
c 1
b 0
f 0
dl 0
loc 34
ccs 11
cts 11
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A transform() 0 17 5
A validate() 0 4 2
1
<?php
2
3
namespace Recurrence\Rrule\Transformer;
4
5
use Recurrence\Rrule\Extractor\DtStartExtractor;
6
use Recurrence\Model\Exception\InvalidRruleException;
7
8
/**
9
 * Class DtStartTransformer
10
 * @package Recurrence\Rrule\Transformer
11
 */
12
class DtStartTransformer implements RruleTransformerInterface
13
{
14
    /**
15
     * @param array $values
16
     * @return \DateTime
17
     * @throws InvalidRruleException
18
     */
19
    public function transform(array $values)
20
    {
21 1
        $this->validate($values);
22
23 1
        if (count($values) == 2 && substr($values[1], -1) == 'Z') {
24 1
            $dtStart = \DateTime::createFromFormat('Ymd\THis\Z', $values[0], new \DateTimeZone('UTC'));
25 1
        } else if (count($values) == 2) {
26 1
            $dtStart = \DateTime::createFromFormat('Ymd\THis', $values[0]);
27
        } else {
28 1
            $dtStart = \DateTime::createFromFormat('Ymd', $values[0]);
29
        }
30
31 1
        if ($dtStart === false) {
32 1
            throw new InvalidRruleException(DtStartExtractor::RRULE_PARAMETER, (string) $values[0]);
33
        }
34
35 1
        return $dtStart;
36
    }
37
38
    /**
39
     * @param array $values
40
     * @throws InvalidRruleException
41
     */
42
    protected function validate(array $values)
43
    {
44 1
        if (!isset($values[0])) {
45 1
            throw new InvalidRruleException(DtStartExtractor::RRULE_PARAMETER);
46
        }
47 1
    }
48
}
49