Issues (112)

src/Bpost/Order.php (2 issues)

Labels
Severity
1
<?php
2
declare(strict_types=1);
3
4
namespace Bpost\BpostApiClient\Bpost;
5
6
use Bpost\BpostApiClient\Bpost\Order\Box;
7
use Bpost\BpostApiClient\Bpost\Order\Line;
8
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException;
9
use Bpost\BpostApiClient\Exception\BpostNotImplementedException;
10
use Bpost\BpostApiClient\Exception\XmlException\BpostXmlNoReferenceFoundException;
11
use DOMDocument;
12
use DOMElement;
13
use SimpleXMLElement;
14
15
/**
16
 * bPost Order class
17
 *
18
 * @author Tijs Verkoyen <[email protected]>
19
 */
20
class Order
21
{
22
    /**
23
     * Order reference: unique ID used in your web shop to assign to an order.
24
     * If the value already exists, it will update current order info.
25
     * Existing boxes will not be changed, new boxes will be added.
26
     */
27
    private string $reference;
28
29
    /** This information is used on your invoice and allows you to attribute different cost centers */
30
    private ?string $costCenter = null;
31
32
    private array $lines = [];
33
34
    private array $boxes = [];
35
36
    public function __construct(string $reference)
37
    {
38
        $this->setReference($reference);
39
    }
40
41
    public function setBoxes(array $boxes): void
42
    {
43
        $this->boxes = $boxes;
44
    }
45
46
    public function getBoxes(): array
47
    {
48
        return $this->boxes;
49
    }
50
51
    public function addBox(Box $box): void
52
    {
53
        $this->boxes[] = $box;
54
    }
55
56
    public function setCostCenter(?string $costCenter): void
57
    {
58
        $this->costCenter = $costCenter;
59
    }
60
61
    public function getCostCenter(): ?string
62
    {
63
        return $this->costCenter;
64
    }
65
66
    public function setLines(array $lines): void
67
    {
68
        $this->lines = $lines;
69
    }
70
71
    public function getLines(): array
72
    {
73
        return $this->lines;
74
    }
75
76
    public function addLine(Line $line): void
77
    {
78
        $this->lines[] = $line;
79
    }
80
81
    public function setReference(string $reference): void
82
    {
83
        $this->reference = $reference;
84
    }
85
86
    public function getReference(): string
87
    {
88
        return $this->reference;
89
    }
90
91
    /**
92
     * @throws \DOMException
93
     */
94
    public function toXML(DOMDocument $document, string $accountId): DOMElement
95
    {
96
        $order = $document->createElement('tns:order');
97
        $order->setAttribute('xmlns:common', 'http://schema.post.be/shm/deepintegration/v5/common');
98
        $order->setAttribute('xmlns:tns', 'http://schema.post.be/shm/deepintegration/v5/');
99
        $order->setAttribute('xmlns', 'http://schema.post.be/shm/deepintegration/v5/national');
100
        $order->setAttribute('xmlns:international', 'http://schema.post.be/shm/deepintegration/v5/international');
101
        $order->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
102
        $order->setAttribute('xsi:schemaLocation', 'http://schema.post.be/shm/deepintegration/v5/');
103
        $order->setAttribute('xmlns:national', 'http://schema.post.be/shm/deepintegration/v5/national');
104
105
        $document->appendChild($order);
106
107
        $order->appendChild($document->createElement('tns:accountId', $accountId));
108
        $order->appendChild($document->createElement('tns:reference', $this->getReference()));
109
110
        if ($this->getCostCenter() !== null) {
111
            $order->appendChild($document->createElement('tns:costCenter', $this->getCostCenter()));
112
        }
113
114
        foreach ($this->getLines() as $line) {
115
            $order->appendChild($line->toXML($document, 'tns'));
116
        }
117
118
        foreach ($this->getBoxes() as $box) {
119
            $order->appendChild($box->toXML($document, 'tns'));
120
        }
121
122
        return $order;
123
    }
124
125
    /**
126
     * @throws BpostXmlNoReferenceFoundException
127
     * @throws BpostNotImplementedException
128
     * @throws BpostInvalidValueException
129
     */
130
    public static function createFromXML(SimpleXMLElement $xml): Order
131
    {
132
        if (!isset($xml->reference)) {
133
            throw new BpostXmlNoReferenceFoundException();
134
        }
135
136
        $order = new Order((string) $xml->reference);
137
138
        if (isset($xml->costCenter) && (string) $xml->costCenter !== '') {
139
            $order->setCostCenter((string) $xml->costCenter);
140
        }
141
142
        if (isset($xml->orderLine)) {
143
            foreach ($xml->orderLine as $orderLine) {
144
                $order->addLine(Line::createFromXML($orderLine));
0 ignored issues
show
It seems like $orderLine can also be of type null; however, parameter $xml of Bpost\BpostApiClient\Bpo...r\Line::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

144
                $order->addLine(Line::createFromXML(/** @scrutinizer ignore-type */ $orderLine));
Loading history...
145
            }
146
        }
147
148
        if (isset($xml->box)) {
149
            foreach ($xml->box as $box) {
150
                $order->addBox(Box::createFromXML($box));
0 ignored issues
show
It seems like $box can also be of type null; however, parameter $xml of Bpost\BpostApiClient\Bpo...er\Box::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

150
                $order->addBox(Box::createFromXML(/** @scrutinizer ignore-type */ $box));
Loading history...
151
            }
152
        }
153
154
        return $order;
155
    }
156
}
157