Completed
Push — master ( e31061...1587a0 )
by Joschi
02:44
created

Selector::__construct()   C

Complexity

Conditions 11
Paths 13

Size

Total Lines 83
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 38
CRAP Score 11

Importance

Changes 8
Bugs 0 Features 1
Metric Value
c 8
b 0
f 1
dl 0
loc 83
ccs 38
cts 38
cp 1
rs 5.2653
cc 11
eloc 56
nc 13
nop 9
crap 11

How to fix   Long Method    Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/**
4
 * apparat-object
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Object
8
 * @subpackage  Apparat\Object\Domain
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Object\Domain\Repository;
38
39
use Apparat\Object\Domain\Model\Object\Revision;
40
use Apparat\Object\Domain\Model\Object\Type;
41
42
/**
43
 * Repository selector
44
 *
45
 * @package Apparat\Object
46
 * @subpackage Apparat\Object\Domain
47
 */
48
class Selector implements SelectorInterface
49
{
50
    /**
51
     * Wildcard
52
     *
53
     * @var string
54
     */
55
    const WILDCARD = '*';
56
    /**
57
     * Year component
58
     *
59
     * @var int
60
     */
61
    private $year = null;
62
    /**
63
     * Month component
64
     *
65
     * @var int
66
     */
67
    private $month = null;
68
    /**
69
     * Day component
70
     *
71
     * @var int
72
     */
73
    private $day = null;
74
    /**
75
     * Hour component
76
     *
77
     * @var int
78
     */
79
    private $hour = null;
80
    /**
81
     * Minute component
82
     *
83
     * @var int
84
     */
85
    private $minute = null;
86
    /**
87
     * Second component
88
     *
89
     * @var int
90
     */
91
    private $second = null;
92
    /**
93
     * Object ID
94
     *
95
     * @var int|string
96
     */
97
    private $uid = null;
98
    /**
99
     * Object type
100
     *
101
     * @var string
102
     */
103
    private $type = null;
104
    /**
105
     * Revision component
106
     *
107
     * @var int
108
     */
109
    private $revision = null;
110
111
    /**
112
     * Repository selector constructor
113
     *
114
     * @param string|int|NULL $year
115
     * @param string|int|NULL $month
116
     * @param string|int|NULL $day
117
     * @param string|int|NULL $hour
118
     * @param string|int|NULL $minute
119
     * @param string|int|NULL $second
120
     * @param string|int|NULL $uid Object ID
121
     * @param string|NULL $type Object type
122
     * @param int|NULL $revision
123
     * @throws InvalidArgumentException If any of the components isn't valid
124
     */
125 14
    public function __construct(
126
        $year = self::WILDCARD,
127
        $month = self::WILDCARD,
128
        $day = self::WILDCARD,
129
        $hour = self::WILDCARD,
130
        $minute = self::WILDCARD,
131
        $second = self::WILDCARD,
132
        $uid = self::WILDCARD,
133
        $type = self::WILDCARD,
134
        $revision = Revision::CURRENT
135
    ) {
136 14
        $datePrecision = intval(getenv('OBJECT_DATE_PRECISION'));
137
138
        // Validate the creation date and ID components
139
        $dateComponents = [
140 14
            'year' => $year,
141 14
            'month' => $month,
142 14
            'day' => $day,
143 14
            'hour' => $hour,
144 14
            'minute' => $minute,
145 14
            'second' => $second
146
        ];
147 14
        foreach (array_slice($dateComponents, 0, $datePrecision) as $label => $component) {
148
            // If the component isn't valid
149 14
            if (!is_int($component) && ($component !== self::WILDCARD)) {
150 1
                throw new InvalidArgumentException(
151
                    sprintf(
152 1
                        'Invalid repository selector '.$label.' component "%s"',
153
                        $component
154
                    ),
155 1
                    InvalidArgumentException::INVALID_REPOSITORY_SELECTOR_COMPONENT,
156 1
                    null,
157
                    $label
158
                );
159
            }
160
161
            // Set the component value
162 13
            $this->$label =
163 13
                ($component === self::WILDCARD) ? self::WILDCARD : str_pad($component, 2, '0', STR_PAD_LEFT);
164
        }
165
166
        // If the ID component isn't valid
167 13
        if (!is_int($uid) && ($uid !== self::WILDCARD)) {
168 1
            throw new InvalidArgumentException(
169
                sprintf(
170 1
                    'Invalid repository selector ID component "%s"',
171
                    $uid
172
                ),
173 1
                InvalidArgumentException::INVALID_REPOSITORY_SELECTOR_COMPONENT,
174 1
                null,
175 1
                'id'
176
            );
177
        }
178 12
        $this->uid = $uid;
179
180
        // If the type component isn't valid
181 12
        if (!Type::isValidType($type) && ($type !== self::WILDCARD)) {
182 1
            throw new InvalidArgumentException(
183
                sprintf(
184 1
                    'Invalid repository selector type component "%s"',
185
                    $type
186
                ),
187 1
                InvalidArgumentException::INVALID_REPOSITORY_SELECTOR_COMPONENT,
188 1
                null,
189 1
                'type'
190
            );
191
        }
192 11
        $this->type = $type;
193
194
        // If the revision component isn't valid
195 11
        if (!Revision::isValidRevision($revision) && ($revision !== self::WILDCARD)) {
196 1
            throw new InvalidArgumentException(
197
                sprintf(
198 1
                    'Invalid repository selector revision component "%s"',
199
                    $revision
200
                ),
201 1
                InvalidArgumentException::INVALID_REPOSITORY_SELECTOR_COMPONENT,
202 1
                null,
203 1
                'revision'
204
            );
205
        }
206 10
        $this->revision = $revision;
207 10
    }
208
209
    /**
210
     * Return the year component
211
     *
212
     * @return int Year component
213
     */
214 10
    public function getYear()
215
    {
216 10
        return $this->year;
217
    }
218
219
    /**
220
     * Return the month component
221
     *
222
     * @return int Month component
223
     */
224 10
    public function getMonth()
225
    {
226 10
        return $this->month;
227
    }
228
229
    /**
230
     * Return the day component
231
     *
232
     * @return int Day component
233
     */
234 10
    public function getDay()
235
    {
236 10
        return $this->day;
237
    }
238
239
    /**
240
     * Return the hour component
241
     *
242
     * @return int Hour component
243
     */
244 10
    public function getHour()
245
    {
246 10
        return $this->hour;
247
    }
248
249
    /**
250
     * Return the minute component
251
     *
252
     * @return int
253
     */
254 10
    public function getMinute()
255
    {
256 10
        return $this->minute;
257
    }
258
259
    /**
260
     * Return the second component
261
     *
262
     * @return int
263
     */
264 10
    public function getSecond()
265
    {
266 10
        return $this->second;
267
    }
268
269
    /**
270
     * Return the ID component
271
     *
272
     * @return int ID component
273
     */
274 10
    public function getId()
275
    {
276 10
        return $this->uid;
277
    }
278
279
    /**
280
     * Return the Type component
281
     *
282
     * @return string
283
     */
284 10
    public function getType()
285
    {
286 10
        return $this->type;
287
    }
288
289
    /**
290
     * Return the revision component
291
     *
292
     * @return int Revision component
293
     */
294 10
    public function getRevision()
295
    {
296 10
        return $this->revision;
297
    }
298
}
299