|
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
|
|
|
} |