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

RecurrenceRule::getParts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 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