National   A
last analyzed

Complexity

Total Complexity 37

Size/Duplication

Total Lines 264
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 37
eloc 80
dl 0
loc 264
rs 9.44
c 1
b 1
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A getPossibleProductValues() 0 3 1
A getOptions() 0 3 1
A addOpeningHour() 0 3 1
A getProduct() 0 3 1
C createFromXML() 0 52 15
A getOptionFromOptionData() 0 8 1
A getOpeningHours() 0 3 1
A setDesiredDeliveryPlace() 0 3 1
A setOpeningHours() 0 3 1
A setOptions() 0 3 1
A setWeight() 0 3 1
A getWeight() 0 3 1
A setProduct() 0 3 1
B toXML() 0 52 8
A addOption() 0 3 1
A getDesiredDeliveryPlace() 0 3 1
1
<?php
2
3
namespace Bpost\BpostApiClient\Bpost\Order\Box;
4
5
use Bpost\BpostApiClient\Bpost\Order\Box\OpeningHour\Day;
6
use Bpost\BpostApiClient\Bpost\Order\Box\Option\Messaging;
7
use Bpost\BpostApiClient\Bpost\Order\Box\Option\Option;
8
use Bpost\BpostApiClient\BpostException;
9
use Bpost\BpostApiClient\Common\ComplexAttribute;
10
use Bpost\BpostApiClient\Common\XmlHelper;
11
use Bpost\BpostApiClient\Exception\BpostNotImplementedException;
12
use Bpost\BpostApiClient\Exception\XmlException\BpostXmlInvalidItemException;
13
use DomDocument;
14
use DomElement;
15
use SimpleXMLElement;
16
17
/**
18
 * bPost National class
19
 *
20
 * @author    Tijs Verkoyen <[email protected]>
21
 *
22
 * @version   3.0.0
23
 *
24
 * @copyright Copyright (c), Tijs Verkoyen. All rights reserved.
25
 * @license   BSD License
26
 */
27
abstract class National extends ComplexAttribute implements IBox
28
{
29
    /** @var string */
30
    protected $product;
31
32
    /** @var Option[] */
33
    protected $options;
34
35
    /** @var int */
36
    protected $weight;
37
38
    /** @var Day[] */
39
    private $openingHours;
40
41
    /** @var string */
42
    private $desiredDeliveryPlace;
43
44
    /**
45
     * @param Option[] $options
46
     */
47
    public function setOptions($options)
48
    {
49
        $this->options = $options;
50
    }
51
52
    /**
53
     * @return Option[]
54
     */
55
    public function getOptions()
56
    {
57
        return $this->options;
58
    }
59
60
    /**
61
     * @param Option $option
62
     */
63
    public function addOption(Option $option)
64
    {
65
        $this->options[] = $option;
66
    }
67
68
    /**
69
     * @param string $product
70
     */
71
    public function setProduct($product)
72
    {
73
        $this->product = $product;
74
    }
75
76
    /**
77
     * @return string
78
     */
79
    public function getProduct()
80
    {
81
        return $this->product;
82
    }
83
84
    /**
85
     * @remark should be implemented by the child class
86
     *
87
     * @return array
88
     */
89
    public static function getPossibleProductValues()
90
    {
91
        return array();
92
    }
93
94
    /**
95
     * @param int $weight
96
     */
97
    public function setWeight($weight)
98
    {
99
        $this->weight = $weight;
100
    }
101
102
    /**
103
     * @return int
104
     */
105
    public function getWeight()
106
    {
107
        return $this->weight;
108
    }
109
110
    /**
111
     * @param Day[] $openingHours
112
     */
113
    public function setOpeningHours(array $openingHours)
114
    {
115
        $this->openingHours = $openingHours;
116
    }
117
118
    /**
119
     * @param Day $day
120
     */
121
    public function addOpeningHour(Day $day)
122
    {
123
        $this->openingHours[] = $day;
124
    }
125
126
    /**
127
     * @return Day[]
128
     */
129
    public function getOpeningHours()
130
    {
131
        return $this->openingHours;
132
    }
133
134
    /**
135
     * @param string $desiredDeliveryPlace
136
     */
137
    public function setDesiredDeliveryPlace($desiredDeliveryPlace)
138
    {
139
        $this->desiredDeliveryPlace = $desiredDeliveryPlace;
140
    }
141
142
    /**
143
     * @return string
144
     */
145
    public function getDesiredDeliveryPlace()
146
    {
147
        return $this->desiredDeliveryPlace;
148
    }
149
150
    /**
151
     * Return the object as an array for usage in the XML
152
     *
153
     * @param DomDocument $document
154
     * @param string      $prefix
155
     * @param string      $type
156
     *
157
     * @return DomElement
158
     */
159
    public function toXML(DOMDocument $document, $prefix = null, $type = null)
160
    {
161
        $typeElement = $document->createElement($type);
162
163
        if ($this->getProduct() !== null) {
0 ignored issues
show
introduced by
The condition $this->getProduct() !== null is always true.
Loading history...
164
            $typeElement->appendChild(
165
                $document->createElement(
166
                    XmlHelper::getPrefixedTagName('product', $prefix),
167
                    $this->getProduct()
168
                )
169
            );
170
        }
171
172
        $options = $this->getOptions();
173
        if (!empty($options)) {
174
            $optionsElement = $document->createElement('options');
175
            foreach ($options as $option) {
176
                $optionsElement->appendChild(
177
                    $option->toXML($document)
178
                );
179
            }
180
            $typeElement->appendChild($optionsElement);
181
        }
182
183
        if ($this->getWeight() !== null) {
0 ignored issues
show
introduced by
The condition $this->getWeight() !== null is always true.
Loading history...
184
            $typeElement->appendChild(
185
                $document->createElement(XmlHelper::getPrefixedTagName('weight', $prefix), $this->getWeight())
186
            );
187
        }
188
189
        $openingHours = $this->getOpeningHours();
190
        if (!empty($openingHours)) {
191
            $openingHoursElement = $document->createElement('openingHours');
192
            /** @var Day $day */
193
            foreach ($openingHours as $day) {
194
                $openingHoursElement->appendChild(
195
                    $day->toXML($document)
196
                );
197
            }
198
            $typeElement->appendChild($openingHoursElement);
199
        }
200
201
        if ($this->getDesiredDeliveryPlace() !== null) {
0 ignored issues
show
introduced by
The condition $this->getDesiredDeliveryPlace() !== null is always true.
Loading history...
202
            $typeElement->appendChild(
203
                $document->createElement(
204
                    XmlHelper::getPrefixedTagName('desiredDeliveryPlace', $prefix),
205
                    $this->getDesiredDeliveryPlace()
206
                )
207
            );
208
        }
209
210
        return $typeElement;
211
    }
212
213
    /**
214
     * @param SimpleXMLElement $nationalXml
215
     * @param National         $self
216
     *
217
     * @return AtHome
218
     *
219
     * @throws BpostException
220
     * @throws BpostXmlInvalidItemException
221
     */
222
    public static function createFromXML(SimpleXMLElement $nationalXml, National $self = null)
223
    {
224
        if ($self === null) {
225
            throw new BpostException('Set an instance of National');
226
        }
227
228
        if (isset($nationalXml->product) && $nationalXml->product != '') {
229
            $self->setProduct(
230
                (string) $nationalXml->product
231
            );
232
        }
233
234
        if (isset($nationalXml->options) && !empty($nationalXml->options)) {
235
            /** @var SimpleXMLElement $optionData */
236
            foreach ($nationalXml->options as $optionData) {
237
                $optionData = $optionData->children('http://schema.post.be/shm/deepintegration/v3/common');
0 ignored issues
show
Bug introduced by
The method children() does not exist on null. ( Ignorable by Annotation )

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

237
                /** @scrutinizer ignore-call */ 
238
                $optionData = $optionData->children('http://schema.post.be/shm/deepintegration/v3/common');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
238
239
                if (in_array($optionData->getName(), array(
240
                        Messaging::MESSAGING_TYPE_INFO_DISTRIBUTED,
241
                        Messaging::MESSAGING_TYPE_INFO_NEXT_DAY,
242
                        Messaging::MESSAGING_TYPE_INFO_REMINDER,
243
                        Messaging::MESSAGING_TYPE_KEEP_ME_INFORMED,
244
                    ))
245
                ) {
246
                    $option = Messaging::createFromXML($optionData);
247
                } else {
248
                    $option = self::getOptionFromOptionData($optionData);
249
                }
250
251
                $self->addOption($option);
252
            }
253
        }
254
255
        if (isset($nationalXml->weight) && $nationalXml->weight != '') {
256
            $self->setWeight(
257
                (int) $nationalXml->weight
258
            );
259
        }
260
261
        if (isset($nationalXml->openingHours) && $nationalXml->openingHours != '') {
262
            foreach ($nationalXml->openingHours->children() as $day => $value) {
263
                $self->addOpeningHour(new Day($day, (string) $value));
264
            }
265
        }
266
267
        if (isset($nationalXml->desiredDeliveryPlace) && $nationalXml->desiredDeliveryPlace != '') {
268
            $self->setDesiredDeliveryPlace(
269
                (string) $nationalXml->desiredDeliveryPlace
270
            );
271
        }
272
273
        return $self;
274
    }
275
276
    /**
277
     * @param SimpleXMLElement $optionData
278
     *
279
     * @return Option
280
     *
281
     * @throws BpostNotImplementedException
282
     */
283
    protected static function getOptionFromOptionData(SimpleXMLElement $optionData)
284
    {
285
        $className = '\\Bpost\\BpostApiClient\\Bpost\\Order\\Box\\Option\\' . ucfirst($optionData->getName());
286
        XmlHelper::assertMethodCreateFromXmlExists($className);
287
288
        return call_user_func(
289
            array($className, 'createFromXML'),
290
            $optionData
291
        );
292
    }
293
}
294