Completed
Push — feature/registration-expiratio... ( 70a084...5dace9 )
by
unknown
03:14
created

DateTime   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 1
dl 0
loc 152
rs 10
c 0
b 0
f 0

12 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 endOfDay() 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 13 2
A __toString() 0 4 1
1
<?php
2
3
/**
4
 * Copyright 2014 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\Stepup\DateTime;
20
21
use DateInterval;
22
use DateTime as CoreDateTime;
23
use Surfnet\Stepup\Exception\InvalidArgumentException;
24
25
/**
26
 * @SuppressWarnings(PHPMD.TooManyMethods)
27
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
28
 */
29
class DateTime
30
{
31
    /**
32
     * The 'c' format, expanded in separate format characters. This string can also be used with
33
     * `DateTime::createFromString()`.
34
     */
35
    const FORMAT = 'Y-m-d\\TH:i:sP';
36
37
    /**
38
     * Allows for mocking of time.
39
     *
40
     * @var self|null
41
     */
42
    private static $now;
43
44
    /**
45
     * @var CoreDateTime
46
     */
47
    private $dateTime;
48
49
    /**
50
     * @return self
51
     */
52
    public static function now()
53
    {
54
        return self::$now ?: new self(new CoreDateTime);
55
    }
56
57
    /**
58
     * @param string $string A date-time string formatted using `self::FORMAT` (eg. '2014-11-26T15:20:43+01:00').
59
     * @return self
60
     */
61
    public static function fromString($string)
62
    {
63
        if (!is_string($string)) {
64
            InvalidArgumentException::invalidType('string', 'dateTime', $string);
65
        }
66
67
        $dateTime = CoreDateTime::createFromFormat(self::FORMAT, $string);
68
69
        if ($dateTime === false) {
70
            throw new InvalidArgumentException('Date-time string could not be parsed: is it formatted correctly?');
71
        }
72
73
        return new self($dateTime);
74
    }
75
76
    /**
77
     * @param CoreDateTime|null $dateTime
78
     */
79
    public function __construct(CoreDateTime $dateTime = null)
80
    {
81
        $this->dateTime = $dateTime ?: new CoreDateTime();
82
    }
83
84
    /**
85
     * @param DateInterval $interval
86
     * @return DateTime
87
     */
88
    public function add(DateInterval $interval)
89
    {
90
        $dateTime = clone $this->dateTime;
91
        $dateTime->add($interval);
92
93
        return new self($dateTime);
94
    }
95
96
    /**
97
     * @param DateInterval $interval
98
     * @return DateTime
99
     */
100
    public function sub(DateInterval $interval)
101
    {
102
        $dateTime = clone $this->dateTime;
103
        $dateTime->sub($interval);
104
105
        return new self($dateTime);
106
    }
107
108
    /**
109
     * @return DateTime
110
     */
111
    public function endOfDay()
112
    {
113
        $dateTime = clone $this->dateTime;
114
        $dateTime->setTime(23, 59, 59);
115
116
        return new self($dateTime);
117
    }
118
119
    /**
120
     * @param DateTime $dateTime
121
     * @return boolean
122
     */
123
    public function comesBefore(DateTime $dateTime)
124
    {
125
        return $this->dateTime < $dateTime->dateTime;
126
    }
127
128
    /**
129
     * @param DateTime $dateTime
130
     * @return boolean
131
     */
132
    public function comesBeforeOrIsEqual(DateTime $dateTime)
133
    {
134
        return $this->dateTime <= $dateTime->dateTime;
135
    }
136
137
    /**
138
     * @param DateTime $dateTime
139
     * @return boolean
140
     */
141
    public function comesAfter(DateTime $dateTime)
142
    {
143
        return $this->dateTime > $dateTime->dateTime;
144
    }
145
146
    /**
147
     * @param DateTime $dateTime
148
     * @return boolean
149
     */
150
    public function comesAfterOrIsEqual(DateTime $dateTime)
151
    {
152
        return $this->dateTime >= $dateTime->dateTime;
153
    }
154
155
    /**
156
     * @param $format
157
     * @return string
158
     */
159
    public function format($format)
160
    {
161
        $formatted = $this->dateTime->format($format);
162
163
        if ($formatted === false) {
164
            throw new InvalidArgumentException(sprintf(
165
                'Given format "%s" is not a valid format for DateTime',
166
                $format
167
            ));
168
        }
169
170
        return $formatted;
171
    }
172
173
    /**
174
     * @return string An ISO 8601 representation of this DateTime.
175
     */
176
    public function __toString()
177
    {
178
        return $this->format(self::FORMAT);
179
    }
180
}
181