Completed
Pull Request — develop (#110)
by Daan van
10:17 queued 07:23
created

DateTime   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 142
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A now() 0 4 2
A fromString() 0 14 3
A __construct() 0 4 2
A add() 0 7 1
A sub() 0 7 1
A comesBefore() 0 4 1
A comesBeforeOrIsEqual() 0 4 1
A comesAfter() 0 4 1
A comesAfterOrIsEqual() 0 4 1
A format() 0 15 2
A __toString() 0 4 1
1
<?php
2
3
/**
4
 * Copyright 2016 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupSelfService\SelfServiceBundle\Value;
20
21
use DateInterval;
22
use DateTime as CoreDateTime;
23
use Surfnet\StepupSelfService\SelfServiceBundle\Exception\InvalidArgumentException;
24
25
/**
26
 * @SuppressWarnings(PHPMD.TooManyPublicMethods) due to comparison methods
27
 */
28
class DateTime
29
{
30
    /**
31
     * This string can also be used with `DateTime::createFromString()`.
32
     */
33
    const FORMAT = DATE_ATOM;
34
35
    /**
36
     * Allows for mocking of time.
37
     *
38
     * @var self|null
39
     */
40
    private static $now;
41
42
    /**
43
     * @var CoreDateTime
44
     */
45
    private $dateTime;
46
47
    /**
48
     * @return self
49
     */
50
    public static function now()
51
    {
52
        return self::$now ?: new self(new CoreDateTime);
53
    }
54
55
    /**
56
     * @param string $string A date-time string formatted using `self::FORMAT` (eg. '2014-11-26T15:20:43+01:00').
57
     * @return DateTime
58
     */
59
    public static function fromString($string)
60
    {
61
        if (!is_string($string)) {
62
            InvalidArgumentException::invalidType('string', 'string', $string);
63
        }
64
65
        $dateTime = CoreDateTime::createFromFormat(self::FORMAT, $string);
66
67
        if ($dateTime === false) {
68
            throw new InvalidArgumentException('Date-time string could not be parsed: is it formatted correctly?');
69
        }
70
71
        return new self($dateTime);
72
    }
73
74
    /**
75
     * @param CoreDateTime|null $dateTime
76
     */
77
    public function __construct(CoreDateTime $dateTime = null)
78
    {
79
        $this->dateTime = $dateTime ?: new CoreDateTime();
80
    }
81
82
    /**
83
     * @param DateInterval $interval
84
     * @return DateTime
85
     */
86
    public function add(DateInterval $interval)
87
    {
88
        $dateTime = clone $this->dateTime;
89
        $dateTime->add($interval);
90
91
        return new self($dateTime);
92
    }
93
94
    /**
95
     * @param DateInterval $interval
96
     * @return DateTime
97
     */
98
    public function sub(DateInterval $interval)
99
    {
100
        $dateTime = clone $this->dateTime;
101
        $dateTime->sub($interval);
102
103
        return new self($dateTime);
104
    }
105
106
    /**
107
     * @param DateTime $dateTime
108
     * @return boolean
109
     */
110
    public function comesBefore(DateTime $dateTime)
111
    {
112
        return $this->dateTime < $dateTime->dateTime;
113
    }
114
115
    /**
116
     * @param DateTime $dateTime
117
     * @return boolean
118
     */
119
    public function comesBeforeOrIsEqual(DateTime $dateTime)
120
    {
121
        return $this->dateTime <= $dateTime->dateTime;
122
    }
123
124
    /**
125
     * @param DateTime $dateTime
126
     * @return boolean
127
     */
128
    public function comesAfter(DateTime $dateTime)
129
    {
130
        return $this->dateTime > $dateTime->dateTime;
131
    }
132
133
    /**
134
     * @param DateTime $dateTime
135
     * @return boolean
136
     */
137
    public function comesAfterOrIsEqual(DateTime $dateTime)
138
    {
139
        return $this->dateTime >= $dateTime->dateTime;
140
    }
141
142
    /**
143
     * @param $format
144
     * @return string
145
     */
146
    public function format($format)
147
    {
148
        $formatted = $this->dateTime->format($format);
149
150
        if ($formatted === false) {
151
            throw new InvalidArgumentException(
152
                sprintf(
153
                    'Given format "%s" is not a valid format for DateTime',
154
                    $format
155
                )
156
            );
157
        }
158
159
        return $formatted;
160
    }
161
162
    /**
163
     * @return string An ISO 8601 representation of this DateTime.
164
     */
165
    public function __toString()
166
    {
167
        return $this->format(self::FORMAT);
168
    }
169
}
170