Completed
Push — master ( 474cbc...4e1719 )
by Olivier
03:46
created

RecurrenceRule::getModifier()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 9
cp 0
rs 9.7
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 20
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shapin\Calendar\Model;
6
7
class RecurrenceRule
8
{
9
    private $parts;
10
11 8
    private function __construct()
12
    {
13 8
    }
14
15 8
    public static function createFromArray(array $parts): self
16
    {
17 8
        if (array_key_exists('UNTIL', $parts) && is_string($parts['UNTIL'])) {
18 2
            $parts['UNTIL'] = new \DateTimeImmutable($parts['UNTIL']);
19
        }
20
21 8
        $rule = new self();
22 8
        $rule->parts = $parts;
23
24 8
        return $rule;
25
    }
26
27
    public function getModifier(int $times = 1): string
28
    {
29
        if (!isset($this->parts['FREQ'])) {
30
            throw new \BadMethodCallException("RecurrenceRule doesn't contains a FREQ. Not implemented yet.");
31
        }
32
33
        $freq = $this->parts['FREQ'];
34
35
        if ('WEEKLY' === $freq) {
36
            return "+$times week";
37
        }
38
        if ('MONTHLY' === $freq) {
39
            return "+$times month";
40
        }
41
42
        throw new \BadMethodCallException("Unknown freq $freq");
43
    }
44
45 8
    public function getParts(): array
46
    {
47 8
        return $this->parts;
48
    }
49
}
50