Completed
Push — develop ( 87537f...191b0e )
by Alexandru
01:11
created

Serial   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
c 1
b 0
f 1
lcom 1
cbo 1
dl 0
loc 33
ccs 12
cts 12
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 16 6
A equals() 0 4 2
1
<?php
2
/**
3
 * Copyright 2017 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 Vimishor\Cnp;
9
10
use Gentle\Embeddable\Embeddable;
11
12
/**
13
 * @author Alexandru Guzinschi <[email protected]>
14
 */
15
final class Serial extends Embeddable
16
{
17
    /**
18
     * @param string|int $number
19
     *
20
     * @throws \InvalidArgumentException
21
     * @throws \OutOfRangeException
22
     */
23 60
    public function __construct($number)
24
    {
25 60
        if (is_int($number)) {
26 22
            $number = (string)$number;
27 11
        }
28
29 60
        if (!is_string($number) || !ctype_digit($number)) {
30 14
            throw new \InvalidArgumentException(sprintf('Expected string or integer and got %s', gettype($number)));
31
        }
32
33 46
        if ((int)$number > 999 || (int)$number < 1) {
34 2
            throw new \OutOfRangeException(sprintf('Expected a number between 1 and 999 (got %d)', (int)$number));
35
        }
36
37 44
        $this->value = str_pad((string)$number, 3, 0, STR_PAD_LEFT);
38 44
    }
39
40
    /**
41
     * {@inheritDoc}
42
     */
43 22
    public function equals(Embeddable $object)
44
    {
45 22
        return get_class($object) === 'Vimishor\Cnp\Serial' && $this->value === (string)$object;
46
    }
47
}
48