Test Failed
Push — master ( 57dab0...ea5e19 )
by Evgenii
03:21
created

OrderStatusDispatcher::dispatchOrderStatus()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 5
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
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 $externalBarCode;
25 2
26
    /**
27 2
     * @param string $xmlBody
28 2
     */
29 2
    public function __construct(string $xmlBody)
30 2
    {
31
        $this->xmlBody = $xmlBody;
32 2
        $this->dispatchOrderStatus();
33
        $this->dispatchItemStatus();
34 2
        $this->dispatchPaymentType();
35 2
        $this->dispatchExternalBarCode();
36 2
    }
37 2
38 2
    private function dispatchOrderStatus(): void
39
    {
40 2
        $pattern = '/<status eventtime="(.+)" createtimegmt="(.+)" title="(.+)">([A-Z]*)<\/status>(<d|<statushis)/';
41
        if (preg_match($pattern, $this->xmlBody, $matches)) {
42 2
            $this->statusId = $matches[4];
43
            $this->statusName = $matches[3];
44 2
            $this->statusTimestamp = strtotime($matches[1]);
45 2
        }
46 2
    }
47 2
48 2
    private function dispatchPaymentType(): void
49 2
    {
50 2
        $pattern = '/<paytype>([A-Z]*)<\/paytype>/';
51 2
        if (preg_match($pattern, $this->xmlBody, $matches)) {
52 2
            $this->paymentType = $matches[1];
53 2
        }
54 2
    }
55
56
    private function dispatchExternalBarCode(): void
57 2
    {
58
        $pattern = '/<outstrbarcode>([A-Z0-9]*)<\/outstrbarcode>/';
59
        if (preg_match($pattern, $this->xmlBody, $matches)) {
60
            $this->externalBarCode = $matches[1];
61
        }
62
    }
63 1
64
    private function dispatchItemStatus()
65 1
    {
66 1
        $pattern = '#<item.*?barcode="(.*?)".*?returns="(.*?)"#';
67 1
        if (preg_match_all($pattern, $this->xmlBody, $matches)) {
68
            $itemsCount = sizeof($matches[0]);
69
            for ($i = 0; $i < $itemsCount; $i++) {
70 1
                $barcode = $matches[1][$i];
71
                if (empty($barcode))
72 1
                    continue;
73
                $return = boolval($matches[2][$i]);
74
                $this->items[$barcode] = (new Item())
75 1
                    ->setBarcode($barcode)
76
                    ->setReturn($return);
77 1
            }
78
        }
79
    }
80 1
81
    /**
82 1
     * @param string|int $barcode
83
     * @return bool|null
84
     */
85
    public function isItemReturned($barcode): ?bool
86
    {
87
        if (isset($this->items[$barcode]))
88 1
            return $this->items[$barcode]->isReturned();
89
        return null;
90 1
    }
91
92
    public function getStatusId(): ?string
93
    {
94
        return $this->statusId;
95
    }
96
97
    public function getStatusName(): ?string
98
    {
99
        return $this->statusName;
100
    }
101
102
    public function getStatusTimestamp(): ?int
103
    {
104
        return $this->statusTimestamp;
105
    }
106
107
    /**
108
     * @return Item[]
109
     */
110
    public function getReturnedItems(): array
111
    {
112
        return $this->items;
113
    }
114
115
    /**
116
     * @return string
117
     */
118
    public function getPaymentType(): string
119
    {
120
        return $this->paymentType;
121
    }
122
123
    /**
124
     * @return string
125
     */
126
    public function getExternalBarCode(): string
127
    {
128
        return $this->externalBarCode;
129
    }
130
}
131