Price   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
eloc 39
c 1
b 0
f 0
dl 0
loc 101
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A setPrice20To30() 0 3 1
A getPriceLessThan2() 0 3 1
A getPrice20To30() 0 3 1
A getPrice10To20() 0 3 1
A getPrice5To10() 0 3 1
A setPrice10To20() 0 3 1
A getCountryIso2() 0 3 1
A setPrice2To5() 0 3 1
A setCountryIso2() 0 3 1
A setPriceLessThan2() 0 3 1
A getPriceByWeight() 0 19 6
A setPrice5To10() 0 3 1
A createFromXML() 0 14 1
A getPrice2To5() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Bpost\BpostApiClient\Bpost\ProductConfiguration;
5
6
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidWeightException;
7
use SimpleXMLElement;
8
9
/**
10
 * Class Price
11
 */
12
class Price
13
{
14
    private string $countryIso2;
15
    private int $priceLessThan2;
16
    private int $price2To5;
17
    private int $price5To10;
18
    private int $price10To20;
19
    private int $price20To30;
20
21
    public static function createFromXML(SimpleXMLElement $xml): self
22
    {
23
        // <price price20To30="820" price10To20="720" price5To10="620" price2To5="520" priceLessThan2="420" countryIso2Code="BE"/>
24
        $a = $xml->attributes();
25
26
        $self = new self();
27
        $self->setCountryIso2((string) $a['countryIso2Code']);
28
        $self->setPriceLessThan2((int) $a['priceLessThan2']);
29
        $self->setPrice2To5((int) $a['price2To5']);
30
        $self->setPrice5To10((int) $a['price5To10']);
31
        $self->setPrice10To20((int) $a['price10To20']);
32
        $self->setPrice20To30((int) $a['price20To30']);
33
34
        return $self;
35
    }
36
37
    /**
38
     * @throws BpostInvalidWeightException
39
     */
40
    public function getPriceByWeight(int $weight): int
41
    {
42
        if ($weight <= 2000) {
43
            return $this->getPriceLessThan2();
44
        }
45
        if ($weight <= 5000) {
46
            return $this->getPrice2To5();
47
        }
48
        if ($weight <= 10000) {
49
            return $this->getPrice5To10();
50
        }
51
        if ($weight <= 20000) {
52
            return $this->getPrice10To20();
53
        }
54
        if ($weight <= 30000) {
55
            return $this->getPrice20To30();
56
        }
57
58
        throw new BpostInvalidWeightException($weight, 30);
59
    }
60
61
    public function getCountryIso2(): string
62
    {
63
        return $this->countryIso2;
64
    }
65
    public function setCountryIso2(string $countryIso2): void
66
    {
67
        $this->countryIso2 = $countryIso2;
68
    }
69
70
    public function getPriceLessThan2(): int
71
    {
72
        return $this->priceLessThan2;
73
    }
74
    public function setPriceLessThan2(int $priceLessThan2): void
75
    {
76
        $this->priceLessThan2 = $priceLessThan2;
77
    }
78
79
    public function getPrice2To5(): int
80
    {
81
        return $this->price2To5;
82
    }
83
    public function setPrice2To5(int $price2To5): void
84
    {
85
        $this->price2To5 = $price2To5;
86
    }
87
88
    public function getPrice5To10(): int
89
    {
90
        return $this->price5To10;
91
    }
92
    public function setPrice5To10(int $price5To10): void
93
    {
94
        $this->price5To10 = $price5To10;
95
    }
96
97
    public function getPrice10To20(): int
98
    {
99
        return $this->price10To20;
100
    }
101
    public function setPrice10To20(int $price10To20): void
102
    {
103
        $this->price10To20 = $price10To20;
104
    }
105
106
    public function getPrice20To30(): int
107
    {
108
        return $this->price20To30;
109
    }
110
    public function setPrice20To30(int $price20To30): void
111
    {
112
        $this->price20To30 = $price20To30;
113
    }
114
}