Test Failed
Push — master ( 222832...e0456b )
by Evgenii
02:52
created

OrderStatusDispatcher::getItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

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
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 string */
12
    private $xmlBody;
13
    /** @var int */
14
    private $statusTimestamp;
15
    /** @var string */
16
    private $statusName;
17
    /** @var string */
18
    private $statusId;
19
    /** @var ?Item[] */
20
    protected $items = [];
21
22
    /**
23
     * @param string $xmlBody
24
     */
25
    public function __construct(string $xmlBody)
26
    {
27
        $this->xmlBody = $xmlBody;
28
        $this->dispatchOrderStatus();
29
        $this->dispatchItemStatus();
30
    }
31
32
    private function dispatchOrderStatus(): void
33
    {
34
        $pattern = '/<status eventtime="(.+)" createtimegmt="(.+)" title="(.+)">(.+)<\/status/';
35
        if (preg_match($pattern, $this->xmlBody, $matches)) {
36
            $this->statusId = $matches[4];
37
            $this->statusName = $matches[3];
38
            $this->statusTimestamp = strtotime($matches[1]);
39
        }
40
    }
41
42
    private function dispatchItemStatus()
43
    {
44
        $pattern = '/<item.+barcode="(.+)" a.+returns="(\d)">/';
45
        if (preg_match_all($pattern, $this->xmlBody, $matches)) {
46
            for ($i = 0; $i < sizeof($matches[0]); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function sizeof() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
47
                $barcode = $matches[1][$i];
48
                $return = boolval($matches[2][$i]);
49
                $this->items[$barcode] = (new Item())
50
                    ->setBarcode($barcode)
51
                    ->setReturn($return);
52
            }
53
        }
54
    }
55
56
    /**
57
     * @param string|int $barcode
58
     * @return bool|null
59
     */
60
    public function isItemReturned($barcode): ?bool
61
    {
62
        return ($item = $this->items[$barcode]) ? $item->isReturned() : null;
63
    }
64
65
    public function getStatusId(): string
66
    {
67
        return $this->statusId;
68
    }
69
70
    public function getStatusName(): string
71
    {
72
        return $this->statusName;
73
    }
74
75
    public function getStatusTimestamp(): int
76
    {
77
        return $this->statusTimestamp;
78
    }
79
80
    /**
81
     * @return Item[]
82
     */
83
    public function getItems(): array
84
    {
85
        return $this->items;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->items could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
86
    }
87
}
88