Passed
Push — master ( c2e69f...e5bfaf )
by Samuel
43s
created

RecurrenceProvider::parse()   D

Complexity

Conditions 10
Paths 49

Size

Total Lines 36
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 10

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 21
cts 21
cp 1
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 18
nc 49
nop 1
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
        if (!$recurrence->hasCount() && !$recurrence->getPeriodEndAt()) {
86 1
            throw new \InvalidArgumentException('Recurrence required an [UNTIL] or [COUNT] option');
87
        }
88
89 1
        return $recurrence;
90
    }
91
}
92