Completed
Push — IncomprehensibleFinder/base ( 348f7f...2b8e11 )
by Albert
04:07 queued 01:29
created

FindCoupleByBirthdayDistance::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Kata\Algorithm;
6
7
use Kata\Algorithm\Model\Couple;
8
use Kata\Algorithm\Model\CoupleCollection;
9
use Kata\Algorithm\Model\CoupleEmpty;
10
use Kata\Algorithm\Model\Person;
11
12
final class FindCoupleByBirthdayDistance
13
{
14
    /** @var Person[] */
15
    private $people;
16
17
    /** @var CoupleCollection */
18
    private $couples;
19
20
    /** @var BirthdayDistanceCriteria */
21
    private $birthday_distance_criteria;
22
23 6
    public function __construct(array $people)
24
    {
25 6
        $this->people  = $people;
26 6
        $this->couples = CoupleCollection::buildAllPossibleCouplesFromPeopleArray($people);
27 6
    }
28
29 6
    public function findByBirthdaysDistance(int $find_criteria): Couple
30
    {
31 6
        if (0 == count($this->couples)) {
32 2
            return new CoupleEmpty();
33
        }
34
35 4
        $this->birthday_distance_criteria = BirthdayDistanceCriteria::fromCriteriaParameter($find_criteria);
36
37 4
        return $this->getBestMatchCouple();
38
    }
39
40 4
    private function getBestMatchCouple(): Couple
41
    {
42 4
        $selected_couple = $this->couples[0];
43
44 4
        foreach ($this->couples as $current_couple) {
45 4
            if ($this->birthday_distance_criteria->isSatisfiedBy($current_couple)->versus($selected_couple)) {
46 4
                $selected_couple = $current_couple;
47
            }
48
        }
49
50 4
        return $selected_couple;
51
    }
52
}
53