Clock::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Majora\Framework\Date;
4
5
/**
6
 * Class which provide current date used to allow mocked dates
7
 * into domain layers instead of using current one
8
 */
9
class Clock
10
{
11
    /**
12
     * @var \DateTime
13
     */
14
    private $currentDate;
15
16
    /**
17
     * Construct
18
     */
19
    public function __construct()
20
    {
21
        $this->currentDate = new \DateTime();
22
    }
23
24
    /**
25
     * use given date as mock
26
     *
27
     * @param string|\DateTime $date
28
     */
29
    protected function mock($date)
30
    {
31
        $this->currentDate = $date instanceof \DateTime ?
32
            $date :
33
            date_create($date)
34
        ;
35
    }
36
37
    /**
38
     * return current date
39
     *
40
     * @param string $format optionnal date format
41
     *
42
     * @return \DateTime
43
     */
44
    public function now($format = null)
45
    {
46
        $date = $this->currentDate
47
            ? clone $this->currentDate
48
            : ($this->currentDate = new \DateTime())
49
        ;
50
51
        return empty($format) ?
0 ignored issues
show
Bug Compatibility introduced by
The expression empty($format) ? $date : $date->format($format); of type DateTime|string adds the type string to the return on line 51 which is incompatible with the return type documented by Majora\Framework\Date\Clock::now of type DateTime.
Loading history...
52
            $date :
53
            $date->format($format)
54
        ;
55
    }
56
}
57