Passed
Pull Request — master (#21)
by Samuel
02:09
created

RecurrenceProvider::validate()   B

Complexity

Conditions 11
Paths 6

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 11.968

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 20
ccs 8
cts 10
cp 0.8
rs 7.3166
cc 11
nc 6
nop 1
crap 11.968

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
        $this->validate($parameters);
49
50 1
        return new Recurrence(
51 1
            $parameters['Freq'],
52 1
            $parameters['Interval'],
53 1
            $parameters['DtStartTimezoned'] ?? $parameters['DtStart'],
54 1
            $parameters['UntilTimezoned'] ?? $parameters['Until'],
55 1
            $parameters['Count'],
56
        );
57
    }
58
59
    private function validate(array $parameters): void
60
    {
61 1
        if (empty($parameters['Freq'])) {
62 1
            throw new InvalidRruleExpressionException(sprintf('Missing [Freq] option.'));
63
        }
64
65 1
        if (empty($parameters['Interval'])) {
66
            throw new InvalidRruleExpressionException(sprintf('Missing [Interval] option.'));
67
        }
68
69 1
        if (empty($parameters['DtStart']) && empty($parameters['DtStartTimezoned'])) {
70
            throw new InvalidRruleExpressionException(sprintf('Missing [DtStart] or [DtStartTimezoned] option.'));
71
        }
72
73 1
        if (empty($parameters['UntilTimezoned']) && empty($parameters['Until']) && empty($parameters['Count'])) {
74 1
            throw new InvalidRruleExpressionException(sprintf('Recurrence required [COUNT] or [UNTIL] option.'));
75
        }
76
77 1
        if ((!empty($parameters['UntilTimezoned']) || !empty($parameters['Until'])) && !empty($parameters['Count'])) {
78 1
            throw new InvalidRruleExpressionException(sprintf('Recurrence cannot have [COUNT] and [UNTIL] option at the same time.'));
79
        }
80 1
    }
81
}
82