Passed
Pull Request — master (#21)
by Samuel
10:54
created

RecurrenceProvider::create()   C

Complexity

Conditions 13
Paths 18

Size

Total Lines 47
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 13.0864

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 47
ccs 23
cts 25
cp 0.92
rs 6.6166
cc 13
nc 18
nop 1
crap 13.0864

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\Rrule;
4
5
use Recurrence\Model\Exception\InvalidRruleException;
6
use Recurrence\Model\Exception\InvalidRruleExpressionException;
7
use Recurrence\Model\Recurrence;
8
9
class RecurrenceProvider
10
{
11
    /**
12
     * Map extractor/transformer names with Recurrence parameters.
13
     */
14
    private array $options = [
15
        'Count',
16
        'Interval',
17
        'Freq',
18
        'DtStartTimezoned',
19
        'DtStart',
20
        'UntilTimezoned',
21
        'Until',
22
    ];
23
24
    /**
25
     * @throws InvalidRruleException
26
     */
27
    public function create(string $rRule): Recurrence
28
    {
29 1
        $parameters = [];
30
31
        // Process all options supported
32 1
        foreach ($this->options as $option) {
33 1
            $parameters[$option] = null;
34
35
            // Create extractor
36 1
            $className = 'Recurrence\Rrule\Extractor\\'.$option.'Extractor';
37 1
            $extractor = new $className();
38
39 1
            if ($values = $extractor->extract($rRule)) {
40
                // Create corresponding transformer
41 1
                $className = 'Recurrence\Rrule\Transformer\\'.$option.'Transformer';
42 1
                $transformer = new $className();
43
44 1
                $parameters[$option] = $transformer->transform($values);
45
            }
46
        }
47
48 1
        if (empty($parameters['Freq'])) {
49 1
            throw new InvalidRruleExpressionException(sprintf('Missing [Freq] option.'));
50
        }
51
52 1
        if (empty($parameters['Interval'])) {
53
            throw new InvalidRruleExpressionException(sprintf('Missing [Interval] option.'));
54
        }
55
56 1
        if (empty($parameters['DtStart']) && empty($parameters['DtStartTimezoned'])) {
57
            throw new InvalidRruleExpressionException(sprintf('Missing [DtStart] or [DtStartTimezoned] option.'));
58
        }
59
60 1
        if (empty($parameters['UntilTimezoned']) && empty($parameters['Until']) && empty($parameters['Count'])) {
61 1
            throw new InvalidRruleExpressionException(sprintf('Recurrence required [COUNT] or [UNTIL] option.'));
62
        }
63
64 1
        if ((!empty($parameters['UntilTimezoned']) || !empty($parameters['Until'])) && !empty($parameters['Count'])) {
65 1
            throw new InvalidRruleExpressionException(sprintf('Recurrence cannot have [COUNT] and [UNTIL] option at the same time.'));
66
        }
67
68 1
        return new Recurrence(
69 1
            $parameters['Freq'],
70 1
            $parameters['Interval'],
71 1
            $parameters['DtStartTimezoned'] ?? $parameters['DtStart'],
72 1
            $parameters['UntilTimezoned'] ?? $parameters['Until'],
73 1
            $parameters['Count'],
74
        );
75
    }
76
}
77