Completed
Pull Request — master (#11)
by Fabien
05:31 queued 02:40
created

AgeCalculator::setToday()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
ccs 5
cts 5
cp 1
cc 1
eloc 4
nc 1
nop 0
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 15
        $this->today    = new \stdClass();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 4 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
33 15
        $this->setToday();
34 15
    }
35
36 15
    public function age()
37
    {
38
        // The calculator of the age
39 15
        $this->setAgeByYear();
40
41 15
        if ($this->today->month <= $this->birth->month) {
42 3
            if ($this->birth->month == $this->today->month) {
43 3
                if ($this->birth->day > $this->today->day) {
44 2
                    $this->setAgeMinusOne();
45 2
                    return $this->getAge();
46
                }
47
48 1
                return $this->getAge();
49
            }
50
51
            $this->setAgeMinusOne();
52
            return $this->getAge();
53
        };
54
55 12
        return $this->getAge();
56
    }
57
58 17
    private function setBirthday()
59
    {
60 17
        $this->birthdayDate = date_parse_from_format('Y-m-d', $this->birthday);
61 17
        if ($this->birthdayDate['error_count'] > 0) {
62 1
            throw new InvalidArgumentException('The date ('. $this->birthday .') is bad formatted, expected Y-m-d.');
63
        }
64
        // Retreive the date and transform it to integer
65 16
        $this->birth->day   = (int) $this->birthdayDate['day'];
66 16
        $this->birth->month = (int) $this->birthdayDate['month'];
67 16
        $this->birth->year  = (int) $this->birthdayDate['year'];
68
69 16
        if(!checkdate($this->birth->month, $this->birth->day, $this->birth->year)) {
70 1
            throw new InvalidArgumentException('The date ('. $this->birthday .') is unknown.');
71
        };
72 15
    }
73
74 15
    private function setToday()
75
    {
76
        // Retreive today and transform it to integer
77 15
        $this->today->day   = (int) date('j');
78 15
        $this->today->month = (int) date('n');
79 15
        $this->today->year  = (int) date('Y');
80 15
    }
81
82 15
    private function getAge()
83
    {
84 15
        return $this->age;
85
    }
86
87 2
    private function setAgeMinusOne()
88
    {
89 2
        $this->age = $this->getAge() - 1;
90 2
    }
91
92 15
    private function setAgeByYear()
93
    {
94 15
        $this->age = (int) ($this->today->year - $this->birth->year);
95 15
    }
96
}
97