Completed
Push — master ( 5096ba...da0857 )
by Peter
07:17
created

WeekIntervalPoint   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 55
ccs 14
cts 16
cp 0.875
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A value() 0 4 1
A __toString() 0 4 1
A getMondayThisWeek() 0 14 3
1
<?php
2
3
/**
4
 * GpsLab component.
5
 *
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2016, Peter Gribanov
8
 * @license   http://opensource.org/licenses/MIT
9
 */
10
11
namespace GpsLab\Component\Interval\Week;
12
13
use GpsLab\Component\Interval\BaseIntervalPoint;
14
15
class WeekIntervalPoint extends BaseIntervalPoint
16
{
17
    /**
18
     * @var \DateTime
19
     */
20
    private $week;
21
22
    /**
23
     * @param \DateTime $date
24
     */
25 4
    public function __construct(\DateTime $date)
26
    {
27 4
        $this->week = $this->getMondayThisWeek($date)->setTime(0, 0, 0);
28 4
    }
29
30
    /**
31
     * Bugfix for get monday of this week.
32
     *
33
     * @see https://bugs.php.net/bug.php?id=63740
34
     *
35
     * @param \DateTime $date
36
     *
37
     * @return \DateTime
38
     */
39 4
    private function getMondayThisWeek(\DateTime $date)
40
    {
41 4
        $monday = clone $date;
42
43 4
        if ($monday->format('N') != 1) {
44 2
            $monday->modify('Monday this week');
45
46 2
            if ($date->format('Y W') != $monday->format('Y W')) {
47
                $monday->modify('-7 day');
48
            }
49 2
        }
50
51 4
        return $monday;
52
    }
53
54
    /**
55
     * @return \DateTime
56
     */
57 4
    public function value()
58
    {
59 4
        return clone $this->week;
60
    }
61
62
    /**
63
     * @return string
64
     */
65 1
    public function __toString()
66
    {
67 1
        return $this->value()->format('Y-m-d');
68
    }
69
}
70