Passed
Branch master (1f9e9c)
by kouinkouin
04:09 queued 50s
created

International::setParcelWeight()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
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\Option\Messaging;
5
use Bpost\BpostApiClient\Bpost\Order\Box\Option\Option;
6
use Bpost\BpostApiClient\Bpost\ProductConfiguration\Product;
7
use Bpost\BpostApiClient\Bpost\Order\Box\Customsinfo\CustomsInfo;
8
use Bpost\BpostApiClient\Bpost\Order\Receiver;
9
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException;
10
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostNotImplementedException;
11
12
/**
13
 * bPost International class
14
 *
15
 * @author    Tijs Verkoyen <[email protected]>
16
 * @version   3.0.0
17
 * @copyright Copyright (c), Tijs Verkoyen. All rights reserved.
18
 * @license   BSD License
19
 */
20
class International implements IBox
21
{
22
    /**
23
     * @var string
24
     */
25
    private $product;
26
27
    /**
28
     * @var array
29
     */
30
    private $options;
31
32
    /**
33
     * @var \Bpost\BpostApiClient\Bpost\Order\Receiver
34
     */
35
    private $receiver;
36
37
    /**
38
     * @var int
39
     */
40
    private $parcelWeight;
41
42
    /**
43
     * @var \Bpost\BpostApiClient\Bpost\Order\Box\CustomsInfo\CustomsInfo
44
     */
45
    private $customsInfo;
46
47
    /**
48
     * @param \Bpost\BpostApiClient\Bpost\Order\Box\CustomsInfo\CustomsInfo $customsInfo
49
     */
50 1
    public function setCustomsInfo($customsInfo)
51
    {
52 1
        $this->customsInfo = $customsInfo;
53 1
    }
54
55
    /**
56
     * @return \Bpost\BpostApiClient\Bpost\Order\Box\CustomsInfo\CustomsInfo
57
     */
58 2
    public function getCustomsInfo()
59
    {
60 2
        return $this->customsInfo;
61
    }
62
63
    /**
64
     * @param Option[] $options
65
     */
66 1
    public function setOptions($options)
67
    {
68 1
        $this->options = $options;
69 1
    }
70
71
    /**
72
     * @return Option[]
73
     */
74 2
    public function getOptions()
75
    {
76 2
        return $this->options;
77
    }
78
79
    /**
80
     * @param Option $option
81
     */
82 1
    public function addOption(Option $option)
83
    {
84 1
        $this->options[] = $option;
85 1
    }
86
87
    /**
88
     * @param int $parcelWeight
89
     */
90 1
    public function setParcelWeight($parcelWeight)
91
    {
92 1
        $this->parcelWeight = $parcelWeight;
93 1
    }
94
95
    /**
96
     * @return int
97
     */
98 2
    public function getParcelWeight()
99
    {
100 2
        return $this->parcelWeight;
101
    }
102
103
    /**
104
     * @param string $product
105
     * @throws BpostInvalidValueException
106
     */
107 3
    public function setProduct($product)
108
    {
109 3
        if (!in_array($product, self::getPossibleProductValues())) {
110 1
            throw new BpostInvalidValueException('product', $product, self::getPossibleProductValues());
111
        }
112
113 2
        $this->product = $product;
114 2
    }
115
116
    /**
117
     * @return string
118
     */
119 2
    public function getProduct()
120
    {
121 2
        return $this->product;
122
    }
123
124
    /**
125
     * @return array
126
     */
127 3
    public static function getPossibleProductValues()
128
    {
129
        return array(
130 3
            Product::PRODUCT_NAME_BPACK_WORLD_BUSINESS,
131 3
            Product::PRODUCT_NAME_BPACK_WORLD_EASY_RETURN,
132 3
            Product::PRODUCT_NAME_BPACK_WORLD_EXPRESS_PRO,
133 3
            Product::PRODUCT_NAME_BPACK_EUROPE_BUSINESS,
134 3
        );
135
    }
136
137
    /**
138
     * @param \Bpost\BpostApiClient\Bpost\Order\Receiver $receiver
139
     */
140 2
    public function setReceiver($receiver)
141
    {
142 2
        $this->receiver = $receiver;
143 2
    }
144
145
    /**
146
     * @return \Bpost\BpostApiClient\Bpost\Order\Receiver
147
     */
148 2
    public function getReceiver()
149
    {
150 2
        return $this->receiver;
151
    }
152
153
    /**
154
     * Return the object as an array for usage in the XML
155
     *
156
     * @param  \DomDocument $document
157
     * @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...
158
     * @return \DomElement
159
     */
160 2
    public function toXML(\DOMDocument $document, $prefix = null)
161
    {
162 2
        $tagName = 'internationalBox';
163 2
        if ($prefix !== null) {
164
            $tagName = $prefix . ':' . $tagName;
165
        }
166
167 2
        $internationalBox = $document->createElement($tagName);
168 2
        $international = $document->createElement('international:international');
169 2
        $internationalBox->appendChild($international);
170
171 2
        if ($this->getProduct() !== null) {
172 2
            $international->appendChild(
173 2
                $document->createElement(
174 2
                    'international:product',
175 2
                    $this->getProduct()
176 2
                )
177 2
            );
178 2
        }
179
180 2
        $options = $this->getOptions();
181 2
        if (!empty($options)) {
182 1
            $optionsElement = $document->createElement('international:options');
183 1
            foreach ($options as $option) {
184 1
                $optionsElement->appendChild(
185 1
                    $option->toXML($document)
186 1
                );
187 1
            }
188 1
            $international->appendChild($optionsElement);
189 1
        }
190
191 2
        if ($this->getReceiver() !== null) {
192 2
            $international->appendChild(
193 2
                $this->getReceiver()->toXML($document, 'international')
194 2
            );
195 2
        }
196
197 2
        if ($this->getParcelWeight() !== null) {
198 1
            $international->appendChild(
199 1
                $document->createElement(
200 1
                    'international:parcelWeight',
201 1
                    $this->getParcelWeight()
202 1
                )
203 1
            );
204 1
        }
205
206 2
        if ($this->getCustomsInfo() !== null) {
207 1
            $international->appendChild(
208 1
                $this->getCustomsInfo()->toXML($document, 'international')
209 1
            );
210 1
        }
211
212 2
        return $internationalBox;
213
    }
214
215
    /**
216
     * @param  \SimpleXMLElement $xml
217
     *
218
     * @return International
219
     * @throws BpostInvalidValueException
220
     * @throws BpostNotImplementedException
221
     */
222
    public static function createFromXML(\SimpleXMLElement $xml)
223
    {
224
        $international = new International();
225
226
        if (isset($xml->international->product) && $xml->international->product != '') {
227
            $international->setProduct(
228
                (string) $xml->international->product
229
            );
230
        }
231 View Code Duplication
        if (isset($xml->international->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...
232
            /** @var \SimpleXMLElement $optionData */
233
            foreach ($xml->international->options as $optionData) {
234
                $optionData = $optionData->children('http://schema.post.be/shm/deepintegration/v3/common');
235
236
                if (in_array($optionData->getName(), array(Messaging::MESSAGING_TYPE_INFO_DISTRIBUTED))) {
237
                    $option = Messaging::createFromXML($optionData);
238
                } else {
239
                    $className = '\\Bpost\\BpostApiClient\\Bpost\\Order\\Box\\Option\\' . ucfirst($optionData->getName());
240
                    if (!method_exists($className, 'createFromXML')) {
241
                        throw new BpostNotImplementedException();
242
                    }
243
                    $option = call_user_func(
244
                        array($className, 'createFromXML'),
245
                        $optionData
246
                    );
247
                }
248
249
                $international->addOption($option);
250
            }
251
        }
252
        if (isset($xml->international->parcelWeight) && $xml->international->parcelWeight != '') {
253
            $international->setParcelWeight(
254
                (int) $xml->international->parcelWeight
255
            );
256
        }
257
        if (isset($xml->international->receiver)) {
258
            $receiverData = $xml->international->receiver->children(
259
                'http://schema.post.be/shm/deepintegration/v3/common'
260
            );
261
            $international->setReceiver(
262
                Receiver::createFromXML($receiverData)
263
            );
264
        }
265
        if (isset($xml->international->customsInfo)) {
266
            $international->setCustomsInfo(
267
                CustomsInfo::createFromXML($xml->international->customsInfo)
268
            );
269
        }
270
271
        return $international;
272
    }
273
}
274