ShippingEstimator   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 45
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
B getEstimates() 0 22 5
A getShippingMethods() 0 5 1
1
<?php
2
3
/**
4
 * Helper class for calculating rates for available shipping options.
5
 * Provides a little caching, so estimates aren't calculated more than once.
6
 *
7
 * @package silvershop-shipping
8
 */
9
class ShippingEstimator
10
{
11
    protected $order;
12
    protected $address;
13
    protected $estimates = null;
14
    protected $calculated = false;
15
16
    public function __construct(Order $order, Address $address = null)
17
    {
18
        $this->order = $order;
19
        $this->address = $address ? $address : $order->getShippingAddress();
20
    }
21
22
    public function getEstimates()
23
    {
24
        if ($this->calculated) {
25
            return $this->estimates;
26
        }
27
        $output = new ArrayList();
28
        if ($options = $this->getShippingMethods()) {
29
            foreach ($options as $option) {
30
                $rate = $option->getCalculator($this->order)->calculate($this->address);
31
                if ($rate !== null) {
32
                    $option->CalculatedRate = $rate;
33
                    $output->push($option);
34
                }
35
            }
36
        }
37
        $output->sort("CalculatedRate", "ASC"); //sort by rate, lowest to highest
38
        // cache estimates
39
        $this->estimates = $output;
40
        $this->calculated = true;
41
42
        return $output;
43
    }
44
45
    /**
46
     * get options that apply to package and location
47
     */
48
    public function getShippingMethods()
49
    {
50
        //TODO: restrict options to region / package specs
51
        return ShippingMethod::get()->filter("Enabled", 1);
52
    }
53
}
54