Completed
Push — Lonja/xserrat ( 69e2b6 )
by Xavier Serrat
17:08 queued 08:59
created

Calculator::findBestCity()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 16
cts 16
cp 1
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 16
nc 3
nop 0
crap 3
1
<?php
2
3
namespace Kata;
4
5
final class Calculator
6
{
7
    /** @var City[] */
8
    private $cities;
9
10
    /** @var Product[] */
11
    private $products;
12
13 1
    public function __construct(array $some_cities, array $some_products)
14
    {
15 1
        $this->cities   = $some_cities;
16 1
        $this->products = $some_products;
17 1
    }
18
19 1
    public function findBestCity(): City
20
    {
21 1
        $results = [];
22
23 1
        foreach ($this->cities as $current_city)
24
        {
25 1
            $product_totals = [];
26
27 1
            foreach ($this->products as $current_product)
28
            {
29 1
                $shipping_rule = $current_city->productShippingRule($current_product);
30
31 1
                $total = $current_product->weight() * $shipping_rule->price();
32 1
                $total -= $current_city->saleCostEveryThousandKmPercentage() * $total;
33 1
                $product_totals[] = $total;
34
            }
35
36 1
            $total = array_sum($product_totals);
37 1
            $total -= $current_city->loadCost();
38
39 1
            $results[$current_city->name()] = $total;
40
        }
41
42 1
        asort($results);
43
44 1
        end($results);
45
46 1
        $city_name = key($results);
47
48 1
        return $this->cityByName($city_name);
49
    }
50
51 1
    private function cityByName(string $name): City
52
    {
53 1
        foreach ($this->cities as $current_city)
54
        {
55 1
            if ($current_city->name() === $name)
56
            {
57 1
                return $current_city;
58
            }
59
        }
60
61
        return null;
62
    }
63
}
64