Passed
Branch factorize (4cfa74)
by Samuel
01:51
created

RecurrenceProvider::create()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 29
ccs 12
cts 12
cp 1
rs 8.5806
cc 4
eloc 14
nc 6
nop 1
crap 4
1
<?php
2
3
namespace Recurrence\Rrule;
4
5
use Recurrence\Model\Exception\InvalidRecurrenceException;
6
use Recurrence\Model\Exception\InvalidRruleExpressionException;
7
use Recurrence\Model\Recurrence;
8
use Recurrence\Rrule\Extractor\CountExtractor;
9
use Recurrence\Rrule\Extractor\FreqExtractor;
10
use Recurrence\Rrule\Extractor\UntilExtractor;
11
use Recurrence\Model\Exception\InvalidRruleException;
12
use Recurrence\Validator\RecurrenceValidator;
13
14
/**
15
 * Class RecurrenceProvider
16
 * @package Recurrence\Rrule
17
 */
18
class RecurrenceProvider
19
{
20
21
    /**
22
     * Map extractor/transformer names with Recurrence attributes
23
     * @var array
24
     */
25
    private $options = [
26
        'Count'            => 'count',
27
        'Interval'         => 'interval',
28
        'Freq'             => 'frequency',
29
        'DtStartTimezoned' => 'periodStartAt',
30
        'DtStart'          => 'periodStartAt',
31
        'UntilTimezoned'   => 'periodEndAt',
32
        'Until'            => 'periodEndAt',
33
    ];
34
35
    /**
36
     * @param string $rRule
37
     * @return Recurrence
38
     * @throws InvalidRruleException
39
     */
40
    public function create($rRule)
41
    {
42 1
        $recurrence = new Recurrence();
43
44
        // Process all options supported
45 1
        foreach ($this->options as $option => $attribute) {
46
            // Create extractor
47 1
            $className = 'Recurrence\Rrule\Extractor\\'.$option.'Extractor';
48 1
            $extractor = new $className();
49
50 1
            if ($values = $extractor->extract($rRule)) {
51
52
                // Create corresponding transformer
53 1
                $className = 'Recurrence\Rrule\Transformer\\'.$option.'Transformer';
54 1
                $transformer = new $className();
55
56
                // Set Recurrence attribute
57 1
                $recurrence = $this->setAttribute($recurrence, $attribute, $transformer->transform($values));
58
            }
59
        }
60
61
        try {
62 1
            RecurrenceValidator::validate($recurrence);
63 1
        } catch (InvalidRecurrenceException $e)  {
64 1
            throw new InvalidRruleExpressionException($e->getMessage());
65
        }
66
67 1
        return $recurrence;
68
    }
69
70
    /**
71
     * @param Recurrence $recurrence
72
     * @param string     $attribute
73
     * @param mixed      $value
74
     * @return Recurrence
75
     */
76
    private function setAttribute(Recurrence $recurrence, $attribute, $value)
77
    {
78 1
        $method = sprintf('set%s', ucfirst($attribute));
79
80 1
        if ($value && method_exists($recurrence, $method)) {
81 1
            $recurrence->$method($value);
82
        }
83
84 1
        return $recurrence;
85
    }
86
}
87