Test Failed
Push — master ( f48202...c86343 )
by Evgenii
09:20
created

OrderStatusDispatcher::getDeliveredTo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
3
4
namespace floor12\DalliApi;
5
6
7
use floor12\DalliApi\Models\Item;
8
9
class OrderStatusDispatcher
10
{
11
    /** @var Item[] */
12
    protected $items = [];
13
    /** @var string */
14
    private $xmlBody;
15
    /** @var int|null */
16
    private $statusTimestamp;
17
    /** @var string|null */
18
    private $statusName;
19
    /** @var string|null */
20
    private $statusId;
21
    /** @var string */
22
    private $paymentType;
23
    /** @var string */
24
    private $deliveredTo;
25 2
    /** @var string */
26
    private $externalBarCode;
27 2
28 2
    /**
29 2
     * @param string $xmlBody
30 2
     */
31
    public function __construct(string $xmlBody)
32 2
    {
33
        echo $xmlBody;
34 2
        $this->xmlBody = $xmlBody;
35 2
        $this->dispatchOrderStatus();
36 2
        $this->dispatchItemStatus();
37 2
        $this->dispatchPaymentType();
38 2
        $this->dispatchExternalBarCode();
39
        $this->dispatchDeliveredTo();
40 2
    }
41
42 2
    private function dispatchOrderStatus(): void
43
    {
44 2
        $pattern = '/<status eventtime="(.+)" createtimegmt="(.+)" title="(.+)">([A-Z]*)<\/status>(<d|<statushis)/';
45 2
        if (preg_match($pattern, $this->xmlBody, $matches)) {
46 2
            $this->statusId = $matches[4];
47 2
            $this->statusName = $matches[3];
48 2
            $this->statusTimestamp = strtotime($matches[1]);
49 2
        }
50 2
    }
51 2
52 2
    private function dispatchDeliveredTo(): void
53 2
    {
54 2
        $pattern = '/<deliveredto>(.+)<\/deliveredto>/';
55
        if (preg_match($pattern, $this->xmlBody, $matches)) {
56
            $this->deliveredTo = $matches[1];
57 2
        }
58
    }
59
60
    private function dispatchPaymentType(): void
61
    {
62
        $pattern = '/<paytype>([A-Z]*)<\/paytype>/';
63 1
        if (preg_match($pattern, $this->xmlBody, $matches)) {
64
            $this->paymentType = $matches[1];
65 1
        }
66 1
    }
67 1
68
    private function dispatchExternalBarCode(): void
69
    {
70 1
        $pattern = '/<outstrbarcode>([A-Z0-9]*)<\/outstrbarcode>/';
71
        if (preg_match($pattern, $this->xmlBody, $matches)) {
72 1
            $this->externalBarCode = $matches[1];
73
        }
74
    }
75 1
76
    private function dispatchItemStatus()
77 1
    {
78
        $pattern = '#barcode="(\d+)" returns="(\d+)"#';
79
        if (preg_match_all($pattern, $this->xmlBody, $matches)) {
80 1
            $itemsCount = sizeof($matches[0]);
81
            for ($i = 0; $i < $itemsCount; $i++) {
82 1
                $barcode = $matches[1][$i];
83
                if (empty($barcode))
84
                    continue;
85
                $return = boolval($matches[2][$i]);
86
                $this->items[$barcode] = (new Item())
87
                    ->setBarcode($barcode)
88 1
                    ->setReturn($return);
89
            }
90 1
        }
91
    }
92
93
    /**
94
     * @param string|int $barcode
95
     * @return bool|null
96
     */
97
    public function isItemReturned($barcode): ?bool
98
    {
99
        if (isset($this->items[$barcode]))
100
            return $this->items[$barcode]->isReturned();
101
        return null;
102
    }
103
104
    public function getStatusId(): ?string
105
    {
106
        return $this->statusId;
107
    }
108
109
    public function getStatusName(): ?string
110
    {
111
        return $this->statusName;
112
    }
113
114
    public function getStatusTimestamp(): ?int
115
    {
116
        return $this->statusTimestamp;
117
    }
118
119
    /**
120
     * @return Item[]
121
     */
122
    public function getReturnedItems(): array
123
    {
124
        return $this->items;
125
    }
126
127
    /**
128
     * @return string
129
     */
130
    public function getPaymentType(): string
131
    {
132
        return $this->paymentType;
133
    }
134
135
    /**
136
     * @return string
137
     */
138
    public function getDeliveredTo(): ?string
139
    {
140
        return $this->deliveredTo;
141
    }
142
143
    /**
144
     * @return string|null
145
     */
146
    public function getExternalBarCode(): ?string
147
    {
148
        return $this->externalBarCode;
149
    }
150
}
151