TimeContext   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
eloc 25
c 0
b 0
f 0
dl 0
loc 41
ccs 22
cts 22
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B getValueForTag() 0 29 7
1
<?php
2
3
namespace LSB\NumberingBundle\Model;
4
5
/**
6
 * Class TimeContext
7
 * @package LSB\NumberingBundle\Model
8
 */
9
class TimeContext
10
{
11
12
    /**
13
     * Returns integer representation of time context for given date.
14
     * For example: 42nd day of the year, 2nd quater of the year etc.
15
     *
16
     * @param string $timeContext
17
     * @param \DateTime|null $date
18
     * @return int
19
     * @throws \Exception
20
     */
21 2
    public static function getValueForTag(string $timeContext, ?\DateTime $date = null): int
22
    {
23 2
        $date = $date ?? new \DateTime();
24
25
        switch ($timeContext) {
26 2
            case Tag::YEAR:
27 1
                $res = $date->format('Y');
28 1
                break;
29 2
            case Tag::SEMESTER:
30 1
                $res = ceil($date->format('n') / 6);
31 1
                break;
32 2
            case Tag::QUARTER:
33 1
                $res = ceil($date->format('n') / 3);
34 1
                break;
35 2
            case Tag::MONTH:
36 1
                $res = $date->format('m');
37 1
                break;
38 2
            case Tag::WEEK:
39 1
                $res = $date->format('W');
40 1
                break;
41 2
            case Tag::DAY:
42 1
                $res = $date->format('z');
43 1
                break;
44
            default:
45 1
                throw new \InvalidArgumentException('Time context tag not allowed');
46
                break;
47
        }
48
49 1
        return (int)$res;
50
    }
51
52
53
}
54