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

CalculateBestDestination   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 3
dl 0
loc 39
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __invoke() 0 20 4
1
<?php
2
3
namespace Kata\Lonja\Application\Service;
4
5
use Kata\Lonja\Domain\Model\Destination;
6
use Kata\Lonja\Domain\Model\Load;
7
use Kata\Lonja\Domain\PriceRule;
8
use Kata\Lonja\Domain\Service\CalculateLoadBenefit;
9
use Kata\Lonja\Domain\Service\Costs\LoadCostsDecorator;
10
11
final class CalculateBestDestination
12
{
13
    private $load_benefit_calculator;
14
    private $load_cost_calculator;
15
16 1
    public function __construct(CalculateLoadBenefit $a_load_benefit_calculator, LoadCostsDecorator $a_load_cost_calculator)
17
    {
18 1
        $this->load_benefit_calculator = $a_load_benefit_calculator;
19 1
        $this->load_cost_calculator    = $a_load_cost_calculator;
20 1
    }
21
22
    /**
23
     * @param Load          $a_load
24
     * @param Destination[] $some_destinations
25
     * @param PriceRule[]   $some_price_rules
26
     *
27
     * @return Destination
28
     */
29 1
    public function __invoke(Load $a_load, array $some_destinations, array $some_price_rules): Destination
30
    {
31 1
        $best_destination = $some_destinations[0];
32 1
        foreach ($some_destinations as $destination)
33
        {
34 1
            $destination_benefits     = $this->load_benefit_calculator->__invoke($a_load, $destination, $some_price_rules);
35 1
            $destination_costs        = $this->load_cost_calculator->__invoke($a_load, $destination, $some_price_rules, $destination_benefits);
36 1
            $destination_net_benefits = $destination_benefits + $destination_costs;
37
38 1
            dump($destination->city() . ': Gross benefits > ' . $destination_benefits . ', Costs > ' . $destination_costs . ', Final: ' . $destination_net_benefits);
39
40 1
            if (!isset($best_benefits) || $best_benefits < $destination_net_benefits)
41
            {
42 1
                $best_destination = $destination;
43 1
                $best_benefits    = $destination_net_benefits;
44
            }
45
        }
46
47 1
        return $best_destination;
48
    }
49
}