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

RecurrenceProvider   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 74
Duplicated Lines 8.11 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 1
cbo 6
dl 6
loc 74
ccs 27
cts 27
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
D create() 6 29 9
A setAttribute() 0 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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