Order   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 251
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 26
lcom 1
cbo 3
dl 0
loc 251
ccs 109
cts 109
cp 1
rs 10
c 1
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
C toXML() 0 78 7
A __construct() 0 4 1
A setBoxes() 0 4 1
A getBoxes() 0 4 1
A addBox() 0 4 1
A setCostCenter() 0 4 1
A getCostCenter() 0 4 1
A setLines() 0 4 1
A getLines() 0 4 1
A addLine() 0 4 1
A setReference() 0 4 1
A getReference() 0 4 1
C createFromXML() 0 27 8
1
<?php
2
3
namespace Bpost\BpostApiClient\Bpost;
4
5
use Bpost\BpostApiClient\Bpost\Order\Box;
6
use Bpost\BpostApiClient\Bpost\Order\Line;
7
use Bpost\BpostApiClient\Exception\BpostNotImplementedException;
8
use Bpost\BpostApiClient\Exception\XmlException\BpostXmlNoReferenceFoundException;
9
10
/**
11
 * bPost Order class
12
 *
13
 * @author Tijs Verkoyen <[email protected]>
14
 */
15
class Order
16
{
17
    /**
18
     * Order reference: unique ID used in your web shop to assign to an order.
19
     * The value of this parameter is not managed by bpost. If the value
20
     * already exists, it will update current order info. Existing boxes will
21
     * not be changed, new boxes will be added.
22
     *
23
     * @var string
24
     */
25
    private $reference;
26
27
    /**
28
     * This information is used on your invoice and allows you to attribute
29
     * different cost centers
30
     *
31
     * @var string
32
     */
33
    private $costCenter;
34
35
    /**
36
     * The items that are included in the order.
37
     * Order lines are shown in the back end of the Shipping Manager and
38
     * facilitate the use of the tool.
39
     *
40
     * @var array
41
     */
42
    private $lines;
43
44
    /**
45
     * Box tags
46
     *
47
     * @var array
48
     */
49
    private $boxes;
50
51
    /**
52
     * Create an order
53
     *
54
     * @param string $reference
55
     */
56 2
    public function __construct($reference)
57
    {
58 2
        $this->setReference($reference);
59 2
    }
60
61
    /**
62
     * @param Box[] $boxes
63
     */
64 1
    public function setBoxes(array $boxes)
65
    {
66 1
        $this->boxes = $boxes;
67 1
    }
68
69
    /**
70
     * @return Box[]
71
     */
72 2
    public function getBoxes()
73
    {
74 2
        return $this->boxes;
75
    }
76
77
    /**
78
     * Add a box
79
     *
80
     * @param Box $box
81
     */
82 2
    public function addBox(Box $box)
83
    {
84 2
        $this->boxes[] = $box;
85 2
    }
86
87
    /**
88
     * @param string $costCenter
89
     */
90 2
    public function setCostCenter($costCenter)
91
    {
92 2
        $this->costCenter = $costCenter;
93 2
    }
94
95
    /**
96
     * @return string
97
     */
98 2
    public function getCostCenter()
99
    {
100 2
        return $this->costCenter;
101
    }
102
103
    /**
104
     * @param Line[] $lines
105
     */
106 1
    public function setLines(array $lines)
107
    {
108 1
        $this->lines = $lines;
109 1
    }
110
111
    /**
112
     * @return Line[]
113
     */
114 2
    public function getLines()
115
    {
116 2
        return $this->lines;
117
    }
118
119
    /**
120
     * Add an order line
121
     *
122
     * @param Line $line
123
     */
124 2
    public function addLine(Line $line)
125
    {
126 2
        $this->lines[] = $line;
127 2
    }
128
129
    /**
130
     * @param string $reference
131
     */
132 2
    public function setReference($reference)
133
    {
134 2
        $this->reference = $reference;
135 2
    }
136
137
    /**
138
     * @return string
139
     */
140 2
    public function getReference()
141
    {
142 2
        return $this->reference;
143
    }
144
145
    /**
146
     * Return the object as an array for usage in the XML
147
     *
148
     * @param  \DOMDocument $document
149
     * @param  string       $accountId
150
     * @return \DOMElement
151
     */
152 1
    public function toXML(\DOMDocument $document, $accountId)
153
    {
154 1
        $order = $document->createElement(
155
            'tns:order'
156 1
        );
157 1
        $order->setAttribute(
158 1
            'xmlns:common',
159
            'http://schema.post.be/shm/deepintegration/v3/common'
160 1
        );
161 1
        $order->setAttribute(
162 1
            'xmlns:tns',
163
            'http://schema.post.be/shm/deepintegration/v3/'
164 1
        );
165 1
        $order->setAttribute(
166 1
            'xmlns',
167
            'http://schema.post.be/shm/deepintegration/v3/national'
168 1
        );
169 1
        $order->setAttribute(
170 1
            'xmlns:international',
171
            'http://schema.post.be/shm/deepintegration/v3/international'
172 1
        );
173 1
        $order->setAttribute(
174 1
            'xmlns:xsi',
175
            'http://www.w3.org/2001/XMLSchema-instance'
176 1
        );
177 1
        $order->setAttribute(
178 1
            'xsi:schemaLocation',
179
            'http://schema.post.be/shm/deepintegration/v3/'
180 1
        );
181
182 1
        $document->appendChild($order);
183
184 1
        $order->appendChild(
185 1
            $document->createElement(
186 1
                'tns:accountId',
187
                (string) $accountId
188 1
            )
189 1
        );
190
191 1
        if ($this->getReference() !== null) {
192 1
            $order->appendChild(
193 1
                $document->createElement(
194 1
                    'tns:reference',
195 1
                    $this->getReference()
196 1
                )
197 1
            );
198 1
        }
199 1
        if ($this->getCostCenter() !== null) {
200 1
            $order->appendChild(
201 1
                $document->createElement(
202 1
                    'tns:costCenter',
203 1
                    $this->getCostCenter()
204 1
                )
205 1
            );
206 1
        }
207
208 1
        $lines = $this->getLines();
209 1
        if (!empty($lines)) {
210 1
            foreach ($lines as $line) {
211
                /** @var $line \Bpost\BpostApiClient\Bpost\Order\Line */
212 1
                $order->appendChild(
213 1
                    $line->toXML($document, 'tns')
214 1
                );
215 1
            }
216 1
        }
217
218 1
        $boxes = $this->getBoxes();
219 1
        if (!empty($boxes)) {
220 1
            foreach ($boxes as $box) {
221
                /** @var $box \Bpost\BpostApiClient\Bpost\Order\Box */
222 1
                $order->appendChild(
223 1
                    $box->toXML($document, 'tns')
224 1
                );
225 1
            }
226 1
        }
227
228 1
        return $order;
229
    }
230
231
    /**
232
     * @param  \SimpleXMLElement $xml
233
     *
234
     * @return Order
235
     * @throws BpostXmlNoReferenceFoundException
236
     * @throws BpostNotImplementedException
237
     */
238 2
    public static function createFromXML(\SimpleXMLElement $xml)
239
    {
240
        // @todo work with classmaps ...
241 2
        if (!isset($xml->reference)) {
242 1
            throw new BpostXmlNoReferenceFoundException();
243
        }
244
245 1
        $order = new Order((string) $xml->reference);
246
247 1
        if (isset($xml->costCenter) && $xml->costCenter != '') {
248 1
            $order->setCostCenter((string) $xml->costCenter);
249 1
        }
250 1
        if (isset($xml->orderLine)) {
251 1
            foreach ($xml->orderLine as $orderLine) {
252 1
                $order->addLine(
253 1
                    Line::createFromXML($orderLine)
254 1
                );
255 1
            }
256 1
        }
257 1
        if (isset($xml->box)) {
258 1
            foreach ($xml->box as $box) {
259 1
                $order->addBox(Box::createFromXML($box));
260 1
            }
261 1
        }
262
263 1
        return $order;
264
    }
265
}
266