Completed
Push — master ( 1629d3...30b37e )
by Joschi
05:03
created

Selector::__construct()   C

Complexity

Conditions 12
Paths 16

Size

Total Lines 98
Code Lines 66

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 50
CRAP Score 12.2666

Importance

Changes 9
Bugs 0 Features 2
Metric Value
cc 12
eloc 66
c 9
b 0
f 2
nc 16
nop 10
dl 0
loc 98
ccs 50
cts 57
cp 0.8772
crap 12.2666
rs 5.034

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
     * Year component
52
     *
53
     * @var int
54
     */
55
    private $year = null;
56
    /**
57
     * Month component
58
     *
59
     * @var int
60
     */
61
    private $month = null;
62
    /**
63
     * Day component
64
     *
65
     * @var int
66
     */
67
    private $day = null;
68
    /**
69
     * Hour component
70
     *
71
     * @var int
72
     */
73
    private $hour = null;
74
    /**
75
     * Minute component
76
     *
77
     * @var int
78
     */
79
    private $minute = null;
80
    /**
81
     * Second component
82
     *
83
     * @var int
84
     */
85
    private $second = null;
86
    /**
87
     * Object ID
88
     *
89
     * @var int|string
90
     */
91
    private $uid = null;
92
    /**
93
     * Object type
94
     *
95
     * @var string
96
     */
97
    private $type = null;
98
    /**
99
     * Revision component
100
     *
101
     * @var int
102
     */
103
    private $revision = null;
104
    /**
105
     * Object visibility
106
     *
107
     * @var int
108
     */
109
    private $visibility = SelectorInterface::VISIBLE;
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
     * @param int $visibility
124
     * @throws InvalidArgumentException If any of the components isn't valid
125
     */
126 16
    public function __construct(
127
        $year = self::WILDCARD,
128
        $month = self::WILDCARD,
129
        $day = self::WILDCARD,
130
        $hour = self::WILDCARD,
131
        $minute = self::WILDCARD,
132
        $second = self::WILDCARD,
133
        $uid = self::WILDCARD,
134
        $type = self::WILDCARD,
135
        $revision = Revision::CURRENT,
136
        $visibility = SelectorInterface::VISIBLE
137
    ) {
138 16
        $datePrecision = intval(getenv('OBJECT_DATE_PRECISION'));
139
140
        // Validate the creation date and ID components
141
        $dateComponents = [
142 16
            'year' => $year,
143 16
            'month' => $month,
144 16
            'day' => $day,
145 16
            'hour' => $hour,
146 16
            'minute' => $minute,
147
            'second' => $second
148 16
        ];
149 16
        foreach (array_slice($dateComponents, 0, $datePrecision) as $label => $component) {
150
            // If the component isn't valid
151 16
            if (!is_int($component) && ($component !== self::WILDCARD)) {
152 1
                throw new InvalidArgumentException(
153 1
                    sprintf(
154 1
                        'Invalid repository selector '.$label.' component "%s"',
155
                        $component
156 1
                    ),
157 1
                    InvalidArgumentException::INVALID_REPOSITORY_SELECTOR_COMPONENT,
158 1
                    null,
159
                    $label
160 1
                );
161
            }
162
163
            // Set the component value
164 15
            $this->$label =
165 15
                ($component === self::WILDCARD) ? self::WILDCARD : str_pad($component, 2, '0', STR_PAD_LEFT);
166 15
        }
167
168
        // If the ID component isn't valid
169 15
        if (!is_int($uid) && ($uid !== self::WILDCARD)) {
170 1
            throw new InvalidArgumentException(
171 1
                sprintf(
172 1
                    'Invalid repository selector ID component "%s"',
173
                    $uid
174 1
                ),
175 1
                InvalidArgumentException::INVALID_REPOSITORY_SELECTOR_COMPONENT,
176 1
                null,
177
                'id'
178 1
            );
179
        }
180 14
        $this->uid = $uid;
181
182
        // If the type component isn't valid
183 14
        if (!Type::isValidType($type) && ($type !== self::WILDCARD)) {
184 1
            throw new InvalidArgumentException(
185 1
                sprintf(
186 1
                    'Invalid repository selector type component "%s"',
187
                    $type
188 1
                ),
189 1
                InvalidArgumentException::INVALID_REPOSITORY_SELECTOR_COMPONENT,
190 1
                null,
191
                'type'
192 1
            );
193
        }
194 13
        $this->type = $type;
195
196
        // If the revision component isn't valid
197 13
        if (!Revision::isValidRevision($revision) && ($revision !== self::WILDCARD)) {
198 1
            throw new InvalidArgumentException(
199 1
                sprintf(
200 1
                    'Invalid repository selector revision component "%s"',
201
                    $revision
202 1
                ),
203 1
                InvalidArgumentException::INVALID_REPOSITORY_SELECTOR_COMPONENT,
204 1
                null,
205
                'revision'
206 1
            );
207
        }
208 12
        $this->revision = $revision;
209
210
        // If the object visibility isn't valid
211 12
        if (!self::isValidVisibility($visibility)) {
212
            throw new InvalidArgumentException(
213
                sprintf(
214
                    'Invalid repository selector visibility "%s"',
215
                    $visibility
216
                ),
217
                InvalidArgumentException::INVALID_REPOSITORY_SELECTOR_COMPONENT,
218
                null,
219
                'visibility'
220
            );
221
        }
222 12
        $this->visibility = $visibility;
223 12
    }
224
225
    /**
226
     * Return the year component
227
     *
228
     * @return int Year component
229
     */
230 10
    public function getYear()
231
    {
232 10
        return $this->year;
233
    }
234
235
    /**
236
     * Return the month component
237
     *
238
     * @return int Month component
239
     */
240 10
    public function getMonth()
241
    {
242 10
        return $this->month;
243
    }
244
245
    /**
246
     * Return the day component
247
     *
248
     * @return int Day component
249
     */
250 10
    public function getDay()
251
    {
252 10
        return $this->day;
253
    }
254
255
    /**
256
     * Return the hour component
257
     *
258
     * @return int Hour component
259
     */
260 10
    public function getHour()
261
    {
262 10
        return $this->hour;
263
    }
264
265
    /**
266
     * Return the minute component
267
     *
268
     * @return int
269
     */
270 10
    public function getMinute()
271
    {
272 10
        return $this->minute;
273
    }
274
275
    /**
276
     * Return the second component
277
     *
278
     * @return int
279
     */
280 10
    public function getSecond()
281
    {
282 10
        return $this->second;
283
    }
284
285
    /**
286
     * Return the ID component
287
     *
288
     * @return int ID component
289
     */
290 10
    public function getId()
291
    {
292 10
        return $this->uid;
293
    }
294
295
    /**
296
     * Return the Type component
297
     *
298
     * @return string
299
     */
300 10
    public function getType()
301
    {
302 10
        return $this->type;
303
    }
304
305
    /**
306
     * Return the revision component
307
     *
308
     * @return int Revision component
309
     */
310 10
    public function getRevision()
311
    {
312 10
        return $this->revision;
313
    }
314
315
    /**
316
     * Return the object visibility
317
     *
318
     * @return int Object visibility
319
     */
320 10
    public function getVisibility()
321
    {
322 10
        return $this->visibility;
323
    }
324
325
    /**
326
     * Test if the given argument is a valid object visibility
327
     *
328
     * @param int $visibility Object visibility
329
     * @return boolean Valid visibility
330
     */
331 12
    public static function isValidVisibility($visibility)
332
    {
333 12
        return ($visibility === SelectorInterface::VISIBLE)
334 2
        || ($visibility === SelectorInterface::HIDDEN)
335 12
        || ($visibility === SelectorInterface::ALL);
336
    }
337
}
338