Price::setPriceLessThan2()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Bpost\BpostApiClient\Bpost\ProductConfiguration;
4
5
use SimpleXMLElement;
6
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidWeightException;
7
8
/**
9
 * Class Price
10
 * @package Bpost\BpostApiClient\Bpost\ProductConfiguration
11
 */
12
class Price
13
{
14
    /** @var  string */
15
    private $countryIso2;
16
17
    /** @var  int */
18
    private $priceLessThan2;
19
    /** @var  int */
20
    private $price2To5;
21
    /** @var  int */
22
    private $price5To10;
23
    /** @var  int */
24
    private $price10To20;
25
    /** @var  int */
26
    private $price20To30;
27
28
    /**
29
     * @param SimpleXMLElement $xml
30
     *
31
     * @return Price
32
     */
33
    public static function createFromXML(SimpleXMLElement $xml)
34
    {
35
        /*
36
        <price price20To30="820" price10To20="720" price5To10="620" price2To5="520" priceLessThan2="420" countryIso2Code="BE"/>
37
        */
38
        $attributes = $xml->attributes();
39
40
        $price = new self();
41
        $price->setCountryIso2($attributes['countryIso2Code']);
42
        $price->setPriceLessThan2($attributes['priceLessThan2']);
43
        $price->setPrice2To5($attributes['price2To5']);
44
        $price->setPrice5To10($attributes['price5To10']);
45
        $price->setPrice10To20($attributes['price10To20']);
46
        $price->setPrice20To30($attributes['price20To30']);
47
48
        return $price;
49
    }
50
51
    /**
52
     * @param int $weight
53
     *
54
     * @return int
55
     * @throws BpostInvalidWeightException
56
     */
57
    public function getPriceByWeight($weight) {
58
        if ($weight <= 2000) {
59
            return $this->getPriceLessThan2();
60
        } elseif ($weight <= 5000) {
61
            return $this->getPrice2To5();
62
        } elseif ($weight <= 10000) {
63
            return $this->getPrice5To10();
64
        } elseif ($weight <= 20000) {
65
            return $this->getPrice10To20();
66
        } elseif ($weight <= 30000) {
67
            return $this->getPrice20To30();
68
        }
69
        throw new BpostInvalidWeightException($weight, 30);
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    public function getCountryIso2()
76
    {
77
        return $this->countryIso2;
78
    }
79
80
    /**
81
     * @param string $countryIso2
82
     */
83
    public function setCountryIso2($countryIso2)
84
    {
85
        $this->countryIso2 = (string) $countryIso2;
86
    }
87
88
    /**
89
     * @return int
90
     */
91
    public function getPriceLessThan2()
92
    {
93
        return $this->priceLessThan2;
94
    }
95
96
    /**
97
     * @param int $priceLessThan2
98
     */
99
    public function setPriceLessThan2($priceLessThan2)
100
    {
101
        $this->priceLessThan2 = (int) $priceLessThan2;
102
    }
103
104
    /**
105
     * @return int
106
     */
107
    public function getPrice2To5()
108
    {
109
        return $this->price2To5;
110
    }
111
112
    /**
113
     * @param int $price2To5
114
     */
115
    public function setPrice2To5($price2To5)
116
    {
117
        $this->price2To5 = (int) $price2To5;
118
    }
119
120
    /**
121
     * @return int
122
     */
123
    public function getPrice5To10()
124
    {
125
        return $this->price5To10;
126
    }
127
128
    /**
129
     * @param int $price5To10
130
     */
131
    public function setPrice5To10($price5To10)
132
    {
133
        $this->price5To10 = (int) $price5To10;
134
    }
135
136
    /**
137
     * @return int
138
     */
139
    public function getPrice10To20()
140
    {
141
        return $this->price10To20;
142
    }
143
144
    /**
145
     * @param int $price10To20
146
     */
147
    public function setPrice10To20($price10To20)
148
    {
149
        $this->price10To20 = (int) $price10To20;
150
    }
151
152
    /**
153
     * @return int
154
     */
155
    public function getPrice20To30()
156
    {
157
        return $this->price20To30;
158
    }
159
160
    /**
161
     * @param int $price20To30
162
     */
163
    public function setPrice20To30($price20To30)
164
    {
165
        $this->price20To30 = (int) $price20To30;
166
    }
167
}
168