PostageOption   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 31
c 4
b 0
f 0
dl 0
loc 141
rs 10
wmc 14

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getTax() 0 3 1
A setTax() 0 4 1
A getSummary() 0 13 2
A __construct() 0 5 1
A setPrice() 0 5 1
A getPrice() 0 3 1
A setName() 0 4 1
A getKey() 0 3 1
A getName() 0 3 1
A getTotalPrice() 0 3 1
A getTaxPrice() 0 7 3
1
<?php
2
3
namespace SilverCommerce\Postage\Helpers;
4
5
use SilverCommerce\TaxAdmin\Model\TaxRate;
6
use SilverStripe\Core\Injector\Injectable;
7
use SilverStripe\ORM\FieldType\DBCurrency;
8
use SilverStripe\SiteConfig\SiteConfig;
9
10
/**
11
 * Generic container for postage data, PossiblePostage objects need to be
12
 * created by PostageTypes and then returned as part of the ArrayList
13
 *
14
 * This object contains 3 generic params:
15
 *
16
 *  - name (the name of the postage)
17
 *  - price (the cost of this object)
18
 *  - tax (the TaxCategory assigned to this object)
19
 */
20
class PostageOption
21
{
22
    use Injectable;
23
24
    /**
25
     * The Name of the current postage item
26
     */
27
    protected $name = null;
28
29
    /**
30
     * Get the value of name
31
     */
32
    public function getName()
33
    {
34
        return $this->name;
35
    }
36
37
    /**
38
     * Set the value of name
39
     *
40
     * @return  self
41
     */
42
    public function setName($name)
43
    {
44
        $this->name = $name;
45
        return $this;
46
    }
47
48
    /**
49
     * The price of this current shipping option
50
     *
51
     * @var int
52
     */
53
    protected $price = 0;
54
55
    /**
56
     * Get the value of price
57
     */
58
    public function getPrice()
59
    {
60
        return $this->price;
61
    }
62
63
    /**
64
     * Set the value of price
65
     *
66
     * @return  self
67
     */
68
    public function setPrice($price)
69
    {
70
        $this->price = $price;
71
72
        return $this;
73
    }
74
75
    /**
76
     * Assign tax to this item, this should be an instance of
77
     * TaxCategory
78
     *
79
     * @var TaxRate
80
     */
81
    protected $tax = null;
82
83
    /**
84
     * Get the value of tax
85
     */
86
    public function getTax()
87
    {
88
        return $this->tax;
89
    }
90
91
    /**
92
     * Set the value of tax
93
     *
94
     * @return self
95
     */
96
    public function setTax(TaxRate $tax)
97
    {
98
        $this->tax = $tax;
99
        return $this;
100
    }
101
102
    /**
103
     * Get the monitary value of tax for this option
104
     *
105
     * @return float
106
     */
107
    public function getTaxPrice()
108
    {
109
        $price = $this->getPrice();
110
        $tax = $this->getTax();
111
        $rate = ($tax && $tax->exists()) ? $tax->Rate : 0;
112
113
        return ($price / 100 * $rate);
114
    }
115
116
    /**
117
     * Get the total monitary value of this option
118
     *
119
     * @return float
120
     */
121
    public function getTotalPrice()
122
    {
123
        return $this->getPrice() + $this->getTaxPrice();
124
    }
125
126
    /**
127
     * Generate a summary of this postage option
128
     *
129
     * @var string
130
     */
131
    public function getSummary()
132
    {
133
        $config = SiteConfig::current_site_config();
134
        $show_tax = $config->ShowPriceAndTax;
135
        $area_currency = new DBCurrency("Cost");
136
137
        if ($show_tax) {
138
            $area_currency->setValue($this->getTotalPrice());
139
        } else {
140
            $area_currency->setValue($this->getPrice());
141
        }
142
143
        return $this->getName() . " (" . $area_currency->Nice() . ")";
144
    }
145
146
    /**
147
     * Generate a unique key for this parcel
148
     *
149
     * @return string
150
     */
151
    public function getKey()
152
    {
153
        return base64_encode(json_encode((array)$this));
154
    }
155
156
    public function __construct($name, $price, TaxRate $tax = null)
157
    {
158
        $this->name = $name;
159
        $this->price = $price;
160
        $this->tax = $tax;
161
    }
162
}
163