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

Simple::getTableTitle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 0
dl 0
loc 13
rs 9.9666
c 0
b 0
f 0
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