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

RecurrenceRule   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 52.63%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 0
loc 43
ccs 10
cts 19
cp 0.5263
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A createFromArray() 0 11 3
A getModifier() 0 17 4
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 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