Completed
Push — master ( 6a4de9...ffda25 )
by Olivier
01:58
created

RecurrenceRule   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 86.96%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 52
ccs 20
cts 23
cp 0.8696
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A createFromArray() 0 7 1
A getModifier() 0 17 4
A getLastEvent() 0 12 3
A getParts() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shapin\Calendar\Model;
6
7
class RecurrenceRule
8
{
9
    private $parts;
10
11 4
    private function __construct()
12
    {
13 4
    }
14
15 4
    public static function createFromArray(array $parts): self
16
    {
17 4
        $rule = new self();
18 4
        $rule->parts = $parts;
19
20 4
        return $rule;
21
    }
22
23 3
    public function getModifier(int $times = 1): string
24
    {
25 3
        if (!isset($this->parts['FREQ'])) {
26
            throw new \BadMethodCallException("RecurrenceRule doesn't contains a FREQ. Not implemented yet.");
27
        }
28
29 3
        $freq = $this->parts['FREQ'];
30
31 3
        if ('WEEKLY' === $freq) {
32 2
            return "+$times week";
33
        }
34 1
        if ('MONTHLY' === $freq) {
35 1
            return "+$times month";
36
        }
37
38
        throw new \BadMethodCallException("Unknown freq $freq");
39
    }
40
41 3
    public function getLastEvent(\DateTimeImmutable $firstEventStartAt): \DateTimeImmutable
42
    {
43 3
        if (isset($this->parts['UNTIL'])) {
44 1
            return new \DateTimeImmutable($this->parts['UNTIL']);
45
        }
46
47 3
        if (isset($this->parts['COUNT'])) {
48 3
            return $firstEventStartAt->modify($this->getModifier((int) $this->parts['COUNT']));
49
        }
50
51
        throw new \BadMethodCallException('Not implemented recurring rule.');
52
    }
53
54 1
    public function getParts(): array
55
    {
56 1
        return $this->parts;
57
    }
58
}
59