Passed
Branch factorize (4cfa74)
by Samuel
01:51
created

DtStartTimezonedTransformer::transform()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
crap 2
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
     * @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
        $dtStart = \DateTime::createFromFormat('Ymd\THis', $values[1], new \DateTimeZone($values[0]));
24
25 1
        if (!$dtStart) {
26 1
            throw new InvalidRruleException(DtStartTimezonedExtractor::RRULE_PARAMETER, implode(', ', $values));
27
        }
28
29 1
        return $dtStart;
30
    }
31
32
    /**
33
     * @param array $values
34
     * @throws InvalidRruleException
35
     */
36 View Code Duplication
    protected function validate(array $values)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
    {
38 1
        if (!isset($values[0]) || !isset($values[1])) {
39 1
            throw new InvalidRruleException(DtStartTimezonedExtractor::RRULE_PARAMETER);
40
        }
41
42
        try {
43 1
            new \DateTimeZone($values[0]);
44 1
        } catch (\Exception $e) {
45 1
            throw new InvalidRruleException(DtStartTimezonedExtractor::RRULE_PARAMETER, (string) $values[0]);
46
        }
47 1
    }
48
}
49