Completed
Pull Request — master (#16)
by Samuel
02:58
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

1 Method

Rating   Name   Duplication   Size   Complexity  
A RecurrenceProvider::setAttribute() 0 10 2

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 array
20
     */
21
    private $transformers;
22
23
    /**
24
     * RecurrenceProvider constructor.
25
     */
26
    public function __construct()
27
    {
28 1
        $this->transformers = [
29 1
            'frequency'     => new FreqTransformer(),
30 1
            'periodStartAt' => new DtStartTransformer(),
31 1
            'periodEndAt'   => new UntilTransformer(),
32 1
            'interval'      => new IntervalTransformer(),
33 1
            'count'         => new CountTransformer(),
34
        ];
35 1
    }
36
37
    /**
38
     * @param string $rRule
39
     * @return Recurrence
40
     * @throws \InvalidArgumentException
41
     */
42
    public function create($rRule)
43
    {
44 1
        if (empty($rRule)) {
45 1
            throw new \InvalidArgumentException('Empty RRULE');
46
        }
47
48 1
        $recurrence = new Recurrence();
49
50
        // Process each transformer and set value if available in RRULE expression
51 1
        foreach ($this->transformers as $attribute => $transformer) {
52 1
            if ($value = $transformer->transform($rRule)) {
53 1
                $recurrence = $this->setAttribute($recurrence, $attribute, $value);
54 1
            }
55 1
        }
56
57 1
        if (!$recurrence->getFrequency()) {
58 1
            throw new \InvalidArgumentException(sprintf('Recurrence [%s] option is required', FreqTransformer::RRULE_PARAMETER));
59
        }
60
61 1 View Code Duplication
        if ($recurrence->hasCount() && $recurrence->getPeriodEndAt()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
62 1
            throw new \InvalidArgumentException(sprintf('Recurrence cannot have [%s] and [%s] option at the same time', UntilTransformer::RRULE_PARAMETER, CountTransformer::RRULE_PARAMETER));
63
        }
64
65 1 View Code Duplication
        if (!$recurrence->hasCount() && !$recurrence->getPeriodEndAt()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
66 1
            throw new \InvalidArgumentException(sprintf('Recurrence required an [%s] or [%s] option', UntilTransformer::RRULE_PARAMETER, CountTransformer::RRULE_PARAMETER));
67
        }
68
69 1
        return $recurrence;
70
    }
71
72
    /**
73
     * @param Recurrence $recurrence
74
     * @param string     $attribute
75
     * @param mixed      $value
76
     * @return Recurrence
77
     */
78
    private function setAttribute(Recurrence $recurrence, $attribute, $value)
79
    {
80 1
        $method = sprintf('set%s', ucfirst($attribute));
81
82 1
        if (method_exists($recurrence, $method)) {
83 1
            $recurrence->$method($value);
84 1
        }
85
86 1
        return $recurrence;
87
    }
88
}
89