Completed
Push — Lonja/albertg ( 67863a...bda4e5 )
by Albert
07:39 queued 05:26
created

LoadCostsDecorator::next()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 5
crap 2
1
<?php
2
3
namespace Kata\Lonja\Domain\Service\Costs;
4
5
use Kata\Lonja\Domain\Model\Destination;
6
use Kata\Lonja\Domain\Model\Load;
7
8
abstract class LoadCostsDecorator
9
{
10
    private $next_load_cost;
11
12 1
    public function __construct(self $next_load_cost = null)
13
    {
14 1
        $this->next_load_cost = $next_load_cost;
15 1
    }
16
17 1
    public function __invoke(Load $a_load, Destination $a_destination, array $some_price_rules, float $net_benefits, float $previous_costs = 0)
18
    {
19 1
        $costs = $previous_costs + $this->applyCosts($a_load, $a_destination, $some_price_rules, $net_benefits, $previous_costs);
20
21 1
        return $this->next($a_load, $a_destination, $some_price_rules, $net_benefits, $costs);
22
    }
23
24
    abstract protected function applyCosts(Load $a_load, Destination $a_destination, array $some_price_rules, float $net_benefits, float $previous_costs = 0): float;
25
26 1
    private function next($a_load, $a_destination, $some_price_rules, $net_benefits, $costs)
27
    {
28 1
        if (null === $this->next_load_cost)
29
        {
30 1
            return -$costs;
31
        }
32
33 1
        return $this->next_load_cost->__invoke($a_load, $a_destination, $some_price_rules, $net_benefits, $costs);
34
    }
35
}