Test Failed
Pull Request — master (#14)
by Samuel
01:57
created

DtStartTransformer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 55
rs 10
ccs 13
cts 13
cp 1

1 Method

Rating   Name   Duplication   Size   Complexity  
B transform() 0 25 6
1
<?php
2
3
namespace Recurrence\RruleTransformer;
4
5
/**
6
 * Class DtStartTransformer
7
 * @package Recurrence\RruleTransformer
8
 */
9
class DtStartTransformer extends AbstractRruleTransformer implements TransformerInterface
10
{
11
    const RRULE_PARAMETER = 'DTSTART';
12
13
    /**
14
     * @var array
15
     */
16
    private $datePatterns = [
17
        [
18
            'pattern'     => '[0-9]{8}T[0-9]{6}Z',
19 1
            'date_format' => 'Ymd\THisZ',
20
            'timezone'    => 'UTC',
21 1
        ],
22 1
        [
23 1
            'pattern'     => '[0-9]{8}T[0-9]{6}',
24
            'date_format' => 'Ymd\THis',
25
            'timezone'    => null,
26
        ],
27 1
        [
28 1
            'pattern'     => '[0-9]{8}',
29
            'date_format' => 'Ymd',
30
            'timezone'    => null,
31 1
        ],
32 1
    ];
33
34
    /**
35 1
     * @param string $rRule
36 1
     * @return \DateTime
37
     */
38
    public function transform($rRule)
39
    {
40 1
        if (preg_match('/'.$this::RRULE_PARAMETER.';TZID=([a-zA-Z_-]+[\/[a-zA-Z_+\-0-9]+]?):([0-9]{8}T[0-9]{6})/', $rRule, $matches)) {
41 1
            try {
42
                return \DateTime::createFromFormat('Ymd\THis', $matches[2], new \DateTimeZone($matches[1]));
43
            } catch (\Exception $e) {
44 1
                throw new \InvalidArgumentException(sprintf('Invalid RRULE [%s] option : [%s] with timezone [%s]', $this::RRULE_PARAMETER, $matches[2], $matches[1]));
45
            }
46
        }
47
48
        // Process each supported date patterns and try to create \Datetime
49
        foreach ($this->datePatterns as $datePattern) {
50
            if (preg_match(sprintf('/%s=(%s)/', $this::RRULE_PARAMETER, $datePattern['pattern']), $rRule, $matches)) {
51
                return \DateTime::createFromFormat(
52
                    $datePattern['date_format'],
53
                    $matches[1],
54
                    (($datePattern['timezone'])? new \DateTimeZone($datePattern['timezone']) : null)
55
                );
56
            }
57
        }
58
59
        $this->throwExceptionOnInvalidParameter($rRule, $this::RRULE_PARAMETER);
60
61
        return null;
62
    }
63
}
64