Passed
Push — master ( 6e29fd...6a8746 )
by Will
02:54
created

Simple   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 52
rs 10
c 0
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A value() 0 8 3
A Country() 0 9 4
A getTableTitle() 0 13 2
1
<?php
2
3
namespace SilverShop\Model\Modifiers\Shipping;
4
5
use SilverStripe\SiteConfig\SiteConfig;
6
7
/**
8
 * Flat shipping to specific countries.
9
 *
10
 * @package    shop
11
 * @subpackage modifiers
12
 */
13
class Simple extends Base
14
{
15
    /**
16
     * @config
17
     * @var float
18
     */
19
    private static $default_charge = 10;
20
21
    /**
22
     * @config
23
     * @var array
24
     */
25
    private static $charges_by_country = array();
26
27
    public function value($subtotal = null)
28
    {
29
        $country = $this->Country();
30
        if ($country && isset(self::config()->charges_by_country[$country])) {
31
            return self::config()->charges_by_country[$country];
32
        }
33
34
        return self::config()->default_charge;
35
    }
36
37
    public function getTableTitle()
38
    {
39
        if ($country = $this->Country()) {
40
            $countryList = SiteConfig::current_site_config()->getCountriesList();
41
42
            return _t(
43
                __CLASS__ . '.ShipToCountry',
44
                'Ship to {Country}',
45
                '',
46
                ['Country' => $countryList[$country]]
47
            );
48
        } else {
49
            return parent::getTableTitle();
50
        }
51
    }
52
53
    /**
54
     * @return string | null
55
     */
56
    public function Country()
57
    {
58
        if ($order = $this->Order()) {
59
            if ($order->getShippingAddress()->exists() && $order->getShippingAddress()->Country) {
60
                return $order->getShippingAddress()->Country;
61
            }
62
        }
63
64
        return null;
65
    }
66
}
67