RecurrenceProvider   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 26
dl 0
loc 60
ccs 16
cts 16
cp 1
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setAttribute() 0 9 3
A create() 0 27 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\Model\Exception\InvalidRruleException;
9
use Recurrence\Validator\RecurrenceValidator;
10
11
class RecurrenceProvider
12
{
13
    /**
14
     * Map extractor/transformer names with Recurrence attributes
15
     */
16
    private array $options = [
17
        'Count'            => 'count',
18
        'Interval'         => 'interval',
19
        'Freq'             => 'frequency',
20
        'DtStartTimezoned' => 'periodStartAt',
21
        'DtStart'          => 'periodStartAt',
22
        'UntilTimezoned'   => 'periodEndAt',
23
        'Until'            => 'periodEndAt',
24
    ];
25
26
    /**
27
     * @return Recurrence
28
     * @throws InvalidRruleException
29
     */
30
    public function create(string $rRule): Recurrence
31
    {
32 1
        $recurrence = new Recurrence();
33
34
        // Process all options supported
35 1
        foreach ($this->options as $option => $attribute) {
36
            // Create extractor
37 1
            $className = 'Recurrence\Rrule\Extractor\\'.$option.'Extractor';
38 1
            $extractor = new $className();
39
40 1
            if ($values = $extractor->extract($rRule)) {
41
                // Create corresponding transformer
42 1
                $className = 'Recurrence\Rrule\Transformer\\'.$option.'Transformer';
43 1
                $transformer = new $className();
44
45
                // Set Recurrence attribute
46 1
                $recurrence = $this->setAttribute($recurrence, $attribute, $transformer->transform($values));
47
            }
48
        }
49
50
        try {
51 1
            RecurrenceValidator::validate($recurrence);
52 1
        } catch (InvalidRecurrenceException $e) {
53 1
            throw new InvalidRruleExpressionException($e->getMessage());
54
        }
55
56 1
        return $recurrence;
57
    }
58
59
    /**
60
     * @param mixed $value
61
     */
62
    private function setAttribute(Recurrence $recurrence, string $attribute, $value): Recurrence
63
    {
64 1
        $method = sprintf('set%s', ucfirst($attribute));
65
66 1
        if ($value && method_exists($recurrence, $method)) {
67 1
            $recurrence->$method($value);
68
        }
69
70 1
        return $recurrence;
71
    }
72
}
73