Test Failed
Push — master ( b077ff...57dab0 )
by Evgenii
05:34
created

OrderStatusDispatcher::getItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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