National::setProduct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
namespace Bpost\BpostApiClient\Bpost\Order\Box;
3
4
use Bpost\BpostApiClient\Bpost\Order\Box\OpeningHour\Day;
5
use Bpost\BpostApiClient\Bpost\Order\Box\Option\Messaging;
6
use Bpost\BpostApiClient\Bpost\Order\Box\Option\Option;
7
use Bpost\BpostApiClient\BpostException;
8
use Bpost\BpostApiClient\Common\ComplexAttribute;
9
use Bpost\BpostApiClient\Exception\XmlException\BpostXmlInvalidItemException;
10
11
/**
12
 * bPost National class
13
 *
14
 * @author    Tijs Verkoyen <[email protected]>
15
 * @version   3.0.0
16
 * @copyright Copyright (c), Tijs Verkoyen. All rights reserved.
17
 * @license   BSD License
18
 */
19
abstract class National extends ComplexAttribute implements IBox
20
{
21
    /** @var string */
22
    protected $product;
23
24
    /** @var Option[] */
25
    protected $options;
26
27
    /** @var int */
28
    protected $weight;
29
30
    /** @var Day[] */
31
    private $openingHours;
32
33
    /** @var string */
34
    private $desiredDeliveryPlace;
35
36
    /**
37
     * @param Option[] $options
38
     */
39 2
    public function setOptions($options)
40
    {
41 2
        $this->options = $options;
42 2
    }
43
44
    /**
45
     * @return Option[]
46
     */
47 9
    public function getOptions()
48
    {
49 9
        return $this->options;
50
    }
51
52
    /**
53
     * @param Option $option
54
     */
55 4
    public function addOption(Option $option)
56
    {
57 4
        $this->options[] = $option;
58 4
    }
59
60
    /**
61
     * @param string $product
62
     */
63 9
    public function setProduct($product)
64
    {
65 9
        $this->product = $product;
66 9
    }
67
68
    /**
69
     * @return string
70
     */
71 9
    public function getProduct()
72
    {
73 9
        return $this->product;
74
    }
75
76
    /**
77
     * @remark should be implemented by the child class
78
     * @return array
79
     */
80 1
    public static function getPossibleProductValues()
81
    {
82 1
        return array();
83
    }
84
85
    /**
86
     * @param int $weight
87
     */
88 7
    public function setWeight($weight)
89
    {
90 7
        $this->weight = $weight;
91 7
    }
92
93
    /**
94
     * @return int
95
     */
96 9
    public function getWeight()
97
    {
98 9
        return $this->weight;
99
    }
100
101
    /**
102
     * @param Day[] $openingHours
103
     */
104 1
    public function setOpeningHours(array $openingHours)
105
    {
106 1
        $this->openingHours = $openingHours;
107 1
    }
108
109
    /**
110
     * @param Day $day
111
     */
112 2
    public function addOpeningHour(Day $day)
113
    {
114 2
        $this->openingHours[] = $day;
115 2
    }
116
117
    /**
118
     * @return Day[]
119
     */
120 9
    public function getOpeningHours()
121
    {
122 9
        return $this->openingHours;
123
    }
124
125
    /**
126
     * @param string $desiredDeliveryPlace
127
     */
128 2
    public function setDesiredDeliveryPlace($desiredDeliveryPlace)
129
    {
130 2
        $this->desiredDeliveryPlace = $desiredDeliveryPlace;
131 2
    }
132
133
    /**
134
     * @return string
135
     */
136 8
    public function getDesiredDeliveryPlace()
137
    {
138 8
        return $this->desiredDeliveryPlace;
139
    }
140
141
    /**
142
     * Return the object as an array for usage in the XML
143
     *
144
     * @param  \DomDocument $document
145
     * @param  string       $prefix
0 ignored issues
show
Documentation introduced by
Should the type for parameter $prefix not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
146
     * @param  string       $type
0 ignored issues
show
Documentation introduced by
Should the type for parameter $type not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
147
     * @return \DomElement
148
     */
149 7
    public function toXML(\DOMDocument $document, $prefix = null, $type = null)
150
    {
151 7
        $typeElement = $document->createElement($type);
152
153 7
        if ($this->getProduct() !== null) {
154 7
            $tagName = 'product';
155 7
            if ($prefix !== null) {
156
                $tagName = $prefix . ':' . $tagName;
157
            }
158 7
            $typeElement->appendChild(
159 7
                $document->createElement(
160 7
                    $tagName,
161 7
                    $this->getProduct()
162 7
                )
163 7
            );
164 7
        }
165
166 7
        $options = $this->getOptions();
167 7
        if (!empty($options)) {
168 2
            $optionsElement = $document->createElement('options');
169 2
            foreach ($options as $option) {
170 2
                $optionsElement->appendChild(
171 2
                    $option->toXML($document)
172 2
                );
173 2
            }
174 2
            $typeElement->appendChild($optionsElement);
175 2
        }
176
177 7
        if ($this->getWeight() !== null) {
178 5
            $typeElement->appendChild(
179 5
                $document->createElement($this->getPrefixedTagName('weight', $prefix), $this->getWeight())
180 5
            );
181 5
        }
182
183 7
        $openingHours = $this->getOpeningHours();
184 7
        if (!empty($openingHours)) {
185 1
            $openingHoursElement = $document->createElement('openingHours');
186
            /** @var Day $day */
187 1
            foreach ($openingHours as $day) {
188 1
                $openingHoursElement->appendChild(
189 1
                    $day->toXML($document)
190 1
                );
191 1
            }
192 1
            $typeElement->appendChild($openingHoursElement);
193 1
        }
194
195 7
        if ($this->getDesiredDeliveryPlace() !== null) {
196 1
            $typeElement->appendChild(
197 1
                $document->createElement(
198 1
                    $this->getPrefixedTagName('desiredDeliveryPlace', $prefix),
199 1
                    $this->getDesiredDeliveryPlace()
200 1
                )
201 1
            );
202 1
        }
203
204 7
        return $typeElement;
205
    }
206
207
208
    /**
209
     * @param \SimpleXMLElement $nationalXml
210
     * @param National          $self
0 ignored issues
show
Documentation introduced by
Should the type for parameter $self not be null|\self?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
211
     * @return AtHome
0 ignored issues
show
Documentation introduced by
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
212
     * @throws BpostException
213
     * @throws BpostXmlInvalidItemException
214
     */
215 2
    public static function createFromXML(\SimpleXMLElement $nationalXml, self $self = null)
216
    {
217 2
        if ($self === null) {
218
            throw new BpostException('Set an instance of National');
219
        }
220
221 2
        if (isset($nationalXml->product) && $nationalXml->product != '') {
222 2
            $self->setProduct(
223 2
                (string)$nationalXml->product
224 2
            );
225 2
        }
226
227 2 View Code Duplication
        if (isset($nationalXml->options) && !empty($nationalXml->options)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
228
            /** @var \SimpleXMLElement $optionData */
229 1
            foreach ($nationalXml->options as $optionData) {
230 1
                $optionData = $optionData->children('http://schema.post.be/shm/deepintegration/v3/common');
231
232 1
                if (in_array($optionData->getName(), array(
233 1
                        Messaging::MESSAGING_TYPE_INFO_DISTRIBUTED,
234 1
                        Messaging::MESSAGING_TYPE_INFO_NEXT_DAY,
235 1
                        Messaging::MESSAGING_TYPE_INFO_REMINDER,
236 1
                        Messaging::MESSAGING_TYPE_KEEP_ME_INFORMED,
237 1
                    ))
238 1
                ) {
239 1
                    $option = Messaging::createFromXML($optionData);
240 1
                } else {
241
                    $className = '\\Bpost\\BpostApiClient\\Bpost\\Order\\Box\\Option\\' . ucfirst($optionData->getName());
242
                    if (!method_exists($className, 'createFromXML')) {
243
                        throw new BpostXmlInvalidItemException();
244
                    }
245
                    $option = call_user_func(
246
                        array($className, 'createFromXML'),
247
                        $optionData
248
                    );
249
                }
250
251 1
                $self->addOption($option);
252 1
            }
253 1
        }
254
255 2
        if (isset($nationalXml->weight) && $nationalXml->weight != '') {
256 1
            $self->setWeight(
257 1
                (int)$nationalXml->weight
258 1
            );
259 1
        }
260
261 2
        if (isset($nationalXml->openingHours) && $nationalXml->openingHours != '') {
262 1
            foreach ($nationalXml->openingHours->children() as $day => $value) {
263 1
                $self->addOpeningHour(new Day($day, (string)$value));
264 1
            }
265 1
        }
266
267 2
        if (isset($nationalXml->desiredDeliveryPlace) && $nationalXml->desiredDeliveryPlace != '') {
268 1
            $self->setDesiredDeliveryPlace(
269 1
                (string)$nationalXml->desiredDeliveryPlace
270 1
            );
271 1
        }
272
273 2
        return $self;
274
275
    }
276
}
277