Product::createFromXML()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 29
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 29
rs 9.5555
c 0
b 0
f 0
cc 5
nc 4
nop 1
1
<?php
2
3
namespace Bpost\BpostApiClient\Bpost\ProductConfiguration;
4
5
use SimpleXMLElement;
6
7
/**
8
 * Class Product
9
 */
10
class Product
11
{
12
    const PRODUCT_NAME_BPACK_EASY_RETOUR = 'bpack Easy Retour';
13
    const PRODUCT_NAME_BPACK_24H_PRO = 'bpack 24h Pro';
14
    const PRODUCT_NAME_BPACK_24H_BUSINESS = 'bpack 24h business';
15
    const PRODUCT_NAME_BPACK_AT_BPOST = 'bpack@bpost';
16
    const PRODUCT_NAME_BPACK_CLICK_AND_COLLECT = 'bpack Click & Collect';
17
    const PRODUCT_NAME_BPACK_24_7 = 'bpack 24/7';
18
    const PRODUCT_NAME_BPACK_BUSINESS = 'bpack Bus';
19
    const PRODUCT_NAME_BPACK_PALLET = 'bpack Pallet';
20
    const PRODUCT_NAME_BPACK_WORLD_EASY_RETURN = 'bpack World Easy Return';
21
    const PRODUCT_NAME_BPACK_WORLD_EXPRESS_PRO = 'bpack World Express Pro';
22
    const PRODUCT_NAME_BPACK_WORLD_BUSINESS = 'bpack World Business';
23
    const PRODUCT_NAME_BPACK_EUROPE_BUSINESS = 'bpack Europe Business';
24
    const PRODUCT_NAME_BPACK_AT_BPOST_INTERNATIONAL = 'bpack@bpost international';
25
26
    /** @var bool */
27
    private $default;
28
    /** @var string */
29
    private $name;
30
31
    /** @var Price[] */
32
    private $prices = array();
33
    /** @var Option[] */
34
    private $options = array();
35
36
    /**
37
     * @param SimpleXMLElement $xml
38
     *
39
     * @return Product
40
     */
41
    public static function createFromXML(SimpleXMLElement $xml)
42
    {
43
        /*
44
        <product default="true" name="bpack 24/7">
45
          <price price20To30="750" price10To20="650" price5To10="550" price2To5="450" priceLessThan2="350" countryIso2Code="BE"/>
46
          <option visiblity="NOT_VISIBLE_BY_CONSUMER_OPTIONAL" price="0" name="Saturday"/>
47
          <option visiblity="NOT_VISIBLE_BY_CONSUMER_OPTIONAL" price="0" name="Info &quot;Distributed&quot;"/>
48
          <option visiblity="NOT_VISIBLE_BY_CONSUMER_OPTIONAL" price="0" name="Insurance"/>
49
        </product>
50
        */
51
        $attributes = $xml->attributes();
52
        $children = $xml->children();
53
54
        $product = new self();
55
        $product->setDefault($attributes['default'] == 'true');
56
        $product->setName($attributes['name']);
57
58
        if (isset($children->price)) {
59
            foreach ($children->price as $priceXml) {
60
                $product->addPrice(Price::createFromXML($priceXml));
0 ignored issues
show
Bug introduced by
It seems like $priceXml can also be of type null; however, parameter $xml of Bpost\BpostApiClient\Bpo...\Price::createFromXML() does only seem to accept SimpleXMLElement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
                $product->addPrice(Price::createFromXML(/** @scrutinizer ignore-type */ $priceXml));
Loading history...
61
            }
62
        }
63
        if (isset($children->option)) {
64
            foreach ($children->option as $optionXml) {
65
                $product->addOption(Option::createFromXML($optionXml));
0 ignored issues
show
Bug introduced by
It seems like $optionXml can also be of type null; however, parameter $xml of Bpost\BpostApiClient\Bpo...Option::createFromXML() does only seem to accept SimpleXMLElement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
                $product->addOption(Option::createFromXML(/** @scrutinizer ignore-type */ $optionXml));
Loading history...
66
            }
67
        }
68
69
        return $product;
70
    }
71
72
    /**
73
     * @return bool
74
     */
75
    public function isDefault()
76
    {
77
        return $this->default;
78
    }
79
80
    /**
81
     * @param bool $default
82
     */
83
    public function setDefault($default)
84
    {
85
        $this->default = (bool) $default;
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    public function getName()
92
    {
93
        return $this->name;
94
    }
95
96
    /**
97
     * @param string $name
98
     */
99
    public function setName($name)
100
    {
101
        $this->name = (string) $name;
102
    }
103
104
    /**
105
     * @return Price[]
106
     */
107
    public function getPrices()
108
    {
109
        return $this->prices;
110
    }
111
112
    /**
113
     * @param Price $price
114
     */
115
    public function addPrice(Price $price)
116
    {
117
        $this->prices[] = $price;
118
    }
119
120
    /**
121
     * @return Option[]
122
     */
123
    public function getOptions()
124
    {
125
        return $this->options;
126
    }
127
128
    /**
129
     * @param Option $option
130
     */
131
    public function addOption(Option $option)
132
    {
133
        $this->options[] = $option;
134
    }
135
136
    /**
137
     * @return bool
138
     */
139
    public function isForNationalShipping()
140
    {
141
        switch ($this->getName()) {
142
            case self::PRODUCT_NAME_BPACK_EASY_RETOUR:
143
            case self::PRODUCT_NAME_BPACK_24H_PRO:
144
            case self::PRODUCT_NAME_BPACK_24H_BUSINESS:
145
            case self::PRODUCT_NAME_BPACK_AT_BPOST:
146
            case self::PRODUCT_NAME_BPACK_CLICK_AND_COLLECT:
147
            case self::PRODUCT_NAME_BPACK_24_7:
148
                return true;
149
150
            case self::PRODUCT_NAME_BPACK_WORLD_EASY_RETURN:
151
            case self::PRODUCT_NAME_BPACK_WORLD_EXPRESS_PRO:
152
            case self::PRODUCT_NAME_BPACK_WORLD_BUSINESS:
153
            case self::PRODUCT_NAME_BPACK_EUROPE_BUSINESS:
154
            case self::PRODUCT_NAME_BPACK_AT_BPOST_INTERNATIONAL:
155
            default:
156
                return false;
157
        }
158
    }
159
}
160