Gender::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.9332
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4
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 Gender extends Embeddable
16
{
17
    /**
18
     * @param string|int $gender Odd for male, even for female.
19
     *
20
     * @throws \InvalidArgumentException
21
     */
22 72
    public function __construct($gender)
23
    {
24 72
        $gender = is_string($gender) ? (int)$gender : $gender;
25
26 72
        if (!is_int($gender) || !in_array($gender, range(1, 8), false)) {
27 22
            throw new \InvalidArgumentException('Unknown gender');
28
        }
29
30 50
        $this->value = (string)$gender;
31 50
    }
32
33
    /**
34
     * @access public
35
     * @return bool
36
     */
37 12
    public function isMale()
38
    {
39 12
        return (int)$this->value % 2 !== 0;
40
    }
41
42
    /**
43
     * {@inheritDoc}
44
     */
45 12
    public function equals(Embeddable $object)
46
    {
47 12
        return get_class($object) === 'Vimishor\Cnp\Gender' && $this->value === (string)$object;
48
    }
49
}
50