Second::__construct()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 10
cts 10
cp 1
rs 9.0444
c 0
b 0
f 0
cc 6
nc 6
nop 1
crap 6
1
<?php
2
/**
3
 * Copyright 2016 Alexandru Guzinschi <[email protected]>.
4
 *
5
 * This software may be modified and distributed under the terms
6
 * of the MIT license. See the LICENSE file for details.
7
 */
8
namespace Gentle\Embeddable\Time;
9
10
use Gentle\Embeddable\Embeddable;
11
12
/**
13
 * @author Alexandru Guzinschi <[email protected]>
14
 */
15
final class Second extends Embeddable
16
{
17
    /**
18
     * @param string|int $value
19
     *
20
     * @throws \InvalidArgumentException
21
     * @throws \OutOfRangeException
22
     */
23 61
    public function __construct($value)
24
    {
25 61
        if (is_int($value)) {
26 20
            $value = (string)$value;
27
        }
28
29 61
        if (!is_string($value) || !ctype_digit($value)) {
30 9
            throw new \InvalidArgumentException(sprintf('Expected string or integer and got %s', gettype($value)));
31
        }
32
33 52
        $value = str_pad($value, 2, 0, STR_PAD_LEFT);
34
35 52
        if ((mb_strlen($value) !== 2) || !in_array((int)$value, range(0, 59), true)) {
36 8
            throw new \OutOfRangeException('Expected a numeric representation of seconds.');
37
        }
38
39 44
        $this->value = $value;
40 44
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 7
    public function equals(Embeddable $object)
46
    {
47 7
        return get_class($object) === 'Gentle\Embeddable\Time\Second' && $this->value === (string)$object;
48
    }
49
}
50