Completed
Pull Request — master (#18)
by Samuel
02:01
created

DtStartTransformer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 0
cbo 1
dl 0
loc 37
ccs 12
cts 12
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B transform() 0 18 5
A validate() 0 6 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