Completed
Pull Request — master (#16)
by Samuel
02:58
created

RecurrenceProvider::create()   D

Complexity

Conditions 9
Paths 13

Size

Total Lines 29
Code Lines 14

Duplication

Lines 6
Ratio 20.69 %

Code Coverage

Tests 15
CRAP Score 9

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 6
loc 29
ccs 15
cts 15
cp 1
rs 4.909
cc 9
eloc 14
nc 13
nop 1
crap 9
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