Completed
Push — IncomprehensibleFinder/dani-sa... ( f1c7e4 )
by
unknown
02:27
created

Finder::getCoupleByAge()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.4285
cc 2
eloc 10
nc 2
nop 2
crap 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Kata\Algorithm;
6
7
final class Finder
8
{
9
    /** @var Person[] */
10
    private $people;
11
12
    private $couple_type;
13
14 6
    public function __construct(array $people)
15
    {
16 6
        $this->people = $people;
17 6
    }
18
19 6
    public function find(int $couple_type): Couple
20
    {
21 6
        $this->couple_type = $couple_type;
22
23
        /** @var Couple[] $couple_array */
24 6
        $couple_array = [];
25 6
        $couple_array = $this->sortPeople($couple_array);
26
27 6
        $not_exists_any_couple = count($couple_array) < 1;
28
29 6
        if ($not_exists_any_couple) {
30 2
            return new Couple();
31
        }
32
33 4
        return $this->getCoupleResult($couple_array);
34
    }
35
36
    /**
37
     * @param $couple_array
38
     * @return array
39
     */
40 6
    private function sortPeople($couple_array): array
41
    {
42 6
        $number_of_people = count($this->people);
43
44 6
        for ($i = 0; $i < $number_of_people; $i++)
45
        {
46 5
            for ($j = $i + 1; $j < $number_of_people; $j++)
47
            {
48 4
                $couple_array[] = $this->getCoupleByAge($i, $j);
49
            }
50
        }
51
52 6
        return $couple_array;
53
    }
54
55
    /**
56
     * @param $couple_array
57
     * @return mixed
58
     */
59 4
    private function getCoupleResult($couple_array)
60
    {
61 4
        $answer = $couple_array[0];
62
63 4
        foreach ($couple_array as $couple_candidate) {
64
65 4
            $answer = $this->getCoupleByCoupleType($couple_candidate, $answer);
66
        }
67 4
        return $answer;
68
    }
69
70
    /**
71
     * @param $i
72
     * @param $j
73
     * @return Couple
74
     */
75 4
    private function getCoupleByAge($i, $j): Couple
76
    {
77 4
        $couple = new Couple();
78
79 4
        if ($this->people[$i]->birthDate < $this->people[$j]->birthDate) {
80 3
            $couple->person_1 = $this->people[$i];
81 3
            $couple->person_2 = $this->people[$j];
82
        } else {
83 3
            $couple->person_1 = $this->people[$j];
84 3
            $couple->person_2 = $this->people[$i];
85
        }
86
87 4
        $couple->age_difference = $couple->person_2->birthDate->getTimestamp() - $couple->person_1->birthDate->getTimestamp();
88 4
        return $couple;
89
    }
90
91
    /**
92
     * @param $couple_candidate
93
     * @param $answer
94
     * @return mixed
95
     */
96 4
    private function getCoupleByCoupleType($couple_candidate, $answer)
97
    {
98 4
        switch ($this->couple_type)
99
        {
100 4
            case CoupleType::CLOSE:
101 2
                if ($couple_candidate->age_difference < $answer->age_difference) {
102 1
                    $answer = $couple_candidate;
103
                }
104 2
                break;
105
106 2
            case CoupleType::FURTHER:
107 2
                if ($couple_candidate->age_difference > $answer->age_difference) {
108
                    $answer = $couple_candidate;
109
                }
110 2
                break;
111
        }
112 4
        return $answer;
113
    }
114
}
115