Clock   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 48
ccs 0
cts 22
cp 0
rs 10
c 3
b 1
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A mock() 0 7 2
A now() 0 12 3
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