DtStartTimezonedTransformer   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 12
dl 0
loc 31
ccs 10
cts 10
cp 1
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A transform() 0 11 2
A validate() 0 10 4
1
<?php
2
3
namespace Recurrence\Rrule\Transformer;
4
5
use Recurrence\Rrule\Extractor\DtStartTimezonedExtractor;
6
use Recurrence\Model\Exception\InvalidRruleException;
7
8
/**
9
 * Class DtStartTimezonedTransformer
10
 * @package Recurrence\Rrule\Transformer
11
 */
12
class DtStartTimezonedTransformer implements RruleTransformerInterface
13
{
14
    /**
15
     * @throws InvalidRruleException
16
     */
17
    public function transform(array $values): \DateTime
18
    {
19 1
        $this->validate($values);
20
21 1
        $dtStart = \DateTime::createFromFormat('Ymd\THis', $values[1], new \DateTimeZone($values[0]));
22
23 1
        if (!$dtStart) {
0 ignored issues
show
introduced by
$dtStart is of type DateTime, thus it always evaluated to true.
Loading history...
24 1
            throw new InvalidRruleException(DtStartTimezonedExtractor::RRULE_PARAMETER, implode(', ', $values));
25
        }
26
27 1
        return $dtStart;
28
    }
29
30
    /**
31
     * @throws InvalidRruleException
32
     */
33
    protected function validate(array $values): void
34
    {
35 1
        if (!isset($values[0]) || !isset($values[1])) {
36 1
            throw new InvalidRruleException(DtStartTimezonedExtractor::RRULE_PARAMETER);
37
        }
38
39
        try {
40 1
            new \DateTimeZone($values[0]);
41 1
        } catch (\Exception $e) {
42 1
            throw new InvalidRruleException(DtStartTimezonedExtractor::RRULE_PARAMETER, (string) $values[0]);
43
        }
44 1
    }
45
}
46