Completed
Push — Lonja/albertg ( 92d4fb...e32987 )
by Albert
03:50
created

recalculateLoadLineBenefits()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 4
crap 2
1
<?php
2
3
namespace Kata\Lonja\Domain\Service;
4
5
use Kata\Lonja\Domain\Model\Destination;
6
use Kata\Lonja\Domain\Model\Load;
7
use Kata\Lonja\Domain\Model\LoadLine;
8
use Kata\Lonja\Domain\PriceRule;
9
10
final class CalculateLoadBenefit
11
{
12
    /**
13
     * @param Load        $a_load
14
     * @param Destination $a_destination
15
     * @param PriceRule[] $some_price_rules
16
     *
17
     * @return float
18
     */
19 4
    public function __invoke(Load $a_load, Destination $a_destination, array $some_price_rules): float
20
    {
21 4
        $load_lines = $a_load->lines();
22 4
        $benefits   = 0;
23
24 4
        foreach ($load_lines as $load_line)
25
        {
26 4
            $this->recalculateLoadLineBenefits($a_destination, $some_price_rules, $load_line, $benefits);
27
        }
28
29 4
        return $benefits;
30
    }
31
32
    /**
33
     * @param Destination $a_destination
34
     * @param PriceRule[] $some_price_rules
35
     * @param LoadLine    $load_line
36
     * @param float       $benefits
37
     *
38
     * @return void
39
     */
40 4
    private function recalculateLoadLineBenefits(Destination $a_destination, array $some_price_rules, LoadLine $load_line, float &$benefits): void
41
    {
42 4
        foreach ($some_price_rules as $price_rule)
43
        {
44 4
            $this->recalculateLoadLineBenefitsIfMatchPriceRules($a_destination, $load_line, $price_rule, $benefits);
45
        }
46 4
    }
47
48 4
    private function recalculateLoadLineBenefitsIfMatchPriceRules(Destination $a_destination, LoadLine $load_line, PriceRule $price_rule, float &$benefits): void
49
    {
50 4
        if ($this->currentProductAndDestinationMatchPriceRule($a_destination, $load_line, $price_rule))
51
        {
52 4
            $benefits += $price_rule->price() * $load_line->kilograms();
53
        }
54 4
    }
55
56 4
    private function currentProductMatchPriceRule(LoadLine $load_line, PriceRule $price_rule): bool
57
    {
58 4
        return $load_line->product()->equals($price_rule->product());
59
    }
60
61 4
    private function currentDestinationMatchPriceRule(Destination $a_destination, PriceRule $price_rule): bool
62
    {
63 4
        return $a_destination->city() === $price_rule->city();
64
    }
65
66 4
    private function currentProductAndDestinationMatchPriceRule(Destination $a_destination, LoadLine $load_line, PriceRule $price_rule): bool
67
    {
68 4
        return $this->currentProductMatchPriceRule($load_line, $price_rule) && $this->currentDestinationMatchPriceRule($a_destination, $price_rule);
69
    }
70
}