AgeCalculator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
ccs 6
cts 6
cp 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the FabienCrassat\CurriculumVitaeBundle Symfony bundle.
5
 *
6
 * (c) Fabien Crassat <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FabienCrassat\CurriculumVitaeBundle\Utility;
13
14
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
15
16
class AgeCalculator
17
{
18
    private $birthdayDate;
19
    private $birthday;
20
    private $age = 0;
21
    private $birth;
22
    private $today;
23
24
    /**
25
     * @param string $birthday
26
     */
27 17
    public function __construct($birthday)
28
    {
29 17
        $this->birthday = $birthday;
30 17
        $this->birth    = new \stdClass();
31 17
        $this->setBirthday();
32
33 15
        $this->today = new \stdClass();
34 15
        $this->setToday();
35 15
    }
36
37 15
    public function age()
38
    {
39
        // The calculator of the age
40 15
        $this->setAgeByYear();
41
42 15
        if ($this->birth->month >= $this->today->month) {
43 3
            if ($this->birth->month == $this->today->month) {
44 2
                if ($this->birth->day > $this->today->day) {
45 1
                    $this->setAgeMinusOne();
46 1
                    return $this->getAge();
47
                }
48
49 1
                return $this->getAge();
50
            }
51
52 1
            $this->setAgeMinusOne();
53 1
            return $this->getAge();
54
        }
55
56 12
        return $this->getAge();
57
    }
58
59 17
    private function setBirthday()
60
    {
61 17
        $this->birthdayDate = date_parse_from_format('Y-m-d', $this->birthday);
62 17
        if ($this->birthdayDate['error_count'] > 0) {
63 1
            throw new InvalidArgumentException('The date ('. $this->birthday .') is bad formatted, expected Y-m-d.');
64
        }
65
        // Retreive the date and transform it to integer
66 16
        $this->birth->day   = (int) $this->birthdayDate['day'];
67 16
        $this->birth->month = (int) $this->birthdayDate['month'];
68 16
        $this->birth->year  = (int) $this->birthdayDate['year'];
69
70 16
        if (!checkdate($this->birth->month, $this->birth->day, $this->birth->year)) {
71 1
            throw new InvalidArgumentException('The date ('. $this->birthday .') is unknown.');
72
        }
73 15
    }
74
75 15
    private function setToday()
76
    {
77
        // Retreive today and transform it to integer
78 15
        $this->today->day   = (int) date('j');
79 15
        $this->today->month = (int) date('n');
80 15
        $this->today->year  = (int) date('Y');
81 15
    }
82
83 15
    private function getAge()
84
    {
85 15
        return $this->age;
86
    }
87
88 2
    private function setAgeMinusOne()
89
    {
90 2
        $this->age = $this->getAge() - 1;
91 2
    }
92
93 15
    private function setAgeByYear()
94
    {
95 15
        $this->age = (int) ($this->today->year - $this->birth->year);
96 15
    }
97
}
98