Completed
Pull Request — master (#12)
by Samuel
04:45
created

RecurrenceProvider::parse()   C

Complexity

Conditions 8
Paths 33

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 8

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 32
ccs 19
cts 19
cp 1
rs 5.3846
cc 8
eloc 16
nc 33
nop 1
crap 8
1
<?php
2
3
namespace Recurrence;
4
5
use Recurrence\RruleTransformer\CountTransformer;
6
use Recurrence\RruleTransformer\DtStartTransformer;
7
use Recurrence\RruleTransformer\FreqTransformer;
8
use Recurrence\RruleTransformer\IntervalTransformer;
9
use Recurrence\RruleTransformer\UntilTransformer;
10
11
/**
12
 * Class RecurrenceProvider
13
 * @package Recurrence
14
 */
15
class RecurrenceProvider
16
{
17
18
    /**
19
     * @var FreqTransformer
20
     */
21
    private $freqTransformer;
22
23
    /**
24
     * @var DtStartTransformer
25
     */
26
    private $dtStartTransformer;
27
28
    /**
29
     * @var UntilTransformer
30
     */
31
    private $untilTransformer;
32
33
    /**
34
     * @var IntervalTransformer
35
     */
36
    private $intervalTransformer;
37
38
    /**
39
     * RecurrenceProvider constructor.
40
     */
41
    public function __construct()
42
    {
43 1
        $this->freqTransformer     = new FreqTransformer();
44 1
        $this->dtStartTransformer  = new DtStartTransformer();
45 1
        $this->untilTransformer    = new UntilTransformer();
46 1
        $this->intervalTransformer = new IntervalTransformer();
47 1
        $this->countTransformer    = new CountTransformer();
0 ignored issues
show
Bug introduced by
The property countTransformer does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
48 1
    }
49
50
    /**
51
     * @param string $rRule
52
     * @return Recurrence
53
     * @throws \InvalidArgumentException
54
     */
55
    public function parse($rRule)
56
    {
57 1
        if (empty($rRule)) {
58 1
            throw new \InvalidArgumentException('Empty RRULE');
59
        }
60
61 1
        $recurrence = new Recurrence();
62
63 1
        $recurrence->setFrequency($this->freqTransformer->transform($rRule));
64
65 1
        if ($periodStartAt = $this->dtStartTransformer->transform($rRule)) {
66 1
            $recurrence->setPeriodStartAt($periodStartAt);
67 1
        }
68
69 1
        if ($periodStartAt = $this->untilTransformer->transform($rRule)) {
70 1
            $recurrence->setPeriodEndAt($periodStartAt);
71 1
        }
72
73 1
        if ($interval = $this->intervalTransformer->transform($rRule)) {
74 1
            $recurrence->setInterval($interval);
75 1
        }
76
77 1
        if ($interval = $this->countTransformer->transform($rRule)) {
78 1
            $recurrence->setCount($interval);
79 1
        }
80
81 1
        if ($recurrence->hasCount() && $recurrence->getPeriodEndAt()) {
82 1
            throw new \InvalidArgumentException('Recurrence cannot have [UNTIL] and [COUNT] option at the same time');
83
        }
84
85 1
        return $recurrence;
86
    }
87
}
88