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

BirthdayDistanceCriteria::fromCriteriaParameter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2.0625
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Kata\Algorithm;
6
7
use Kata\Algorithm\Model\Couple;
8
9
final class BirthdayDistanceCriteria
10
{
11
    const CLOSEST_BIRTHDAY = 1;
12
    const FURTHEST_BIRTHDAY = 2;
13
14
    /** @var int */
15
    private $criteria;
16
17
    /** @var Couple */
18
    private $first_couple;
19
20 4
    private function __construct(int $criteria)
21
    {
22 4
        $this->criteria = $criteria;
23 4
    }
24
25 4
    public static function fromCriteriaParameter(int $criteria): BirthdayDistanceCriteria
26
    {
27 4
        if (!in_array($criteria, [self::CLOSEST_BIRTHDAY, self::FURTHEST_BIRTHDAY])) {
28
            throw new \InvalidArgumentException('Criteria ' . $criteria . ' is invalid.');
29
        }
30
31 4
        return new self($criteria);
32
    }
33
34 4
    public function isSatisfiedBy(Couple $first_couple)
35
    {
36 4
        $this->first_couple = $first_couple;
37
38 4
        return $this;
39
    }
40
41 4
    public function versus(Couple $second_couple): bool
42
    {
43 4
        if (self::CLOSEST_BIRTHDAY === $this->criteria) {
44 2
            return $this->first_couple->hasLessDistanceThan($second_couple);
45
        }
46
47 2
        if (self::FURTHEST_BIRTHDAY === $this->criteria) {
48 2
            return $this->first_couple->hasGreaterDistanceThan($second_couple);
49
        }
50
    }
51
}
52