Test Failed
Push — master ( 04fcd4...836f53 )
by Evgenii
05:13
created

OrderStatusDispatcher::getStatuses()   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 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
c 1
b 1
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 Exception;
8
use floor12\DalliApi\Enum\DalliOrderStatus;
9
use floor12\DalliApi\Models\Item;
10
use floor12\DalliApi\Models\Response\DalliOrderStatusEvent;
11
12
class OrderStatusDispatcher
13
{
14
    /** @var Item[] */
15
    protected $items = [];
16
    /** @var string */
17
    private $xmlBody;
0 ignored issues
show
introduced by
The private property $xmlBody is not used, and could be removed.
Loading history...
18
    /** @var int|null */
19
    private $statusTimestamp;
0 ignored issues
show
introduced by
The private property $statusTimestamp is not used, and could be removed.
Loading history...
20
    /** @var string|null */
21
    private $statusName;
0 ignored issues
show
introduced by
The private property $statusName is not used, and could be removed.
Loading history...
22
    /** @var string|null */
23
    private $statusId;
0 ignored issues
show
introduced by
The private property $statusId is not used, and could be removed.
Loading history...
24
    /** @var string */
25 2
    private $paymentType;
0 ignored issues
show
introduced by
The private property $paymentType is not used, and could be removed.
Loading history...
26
    /** @var string */
27 2
    private $deliveredTo;
0 ignored issues
show
introduced by
The private property $deliveredTo is not used, and could be removed.
Loading history...
28 2
    /** @var string */
29 2
    private $externalBarCode;
30 2
    /**
31
     * @var \$1|false|\SimpleXMLElement
0 ignored issues
show
Bug introduced by
The type $1 was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32 2
     */
33
    private $xml;
34 2
    /**
35 2
     * @var DalliOrderStatusEvent[]
36 2
     */
37 2
    private $statuses;
38 2
39
    /**
40 2
     * @param string $xmlBody
41
     * @throws Exception
42 2
     */
43
    public function __construct(string $xmlBody)
44 2
    {
45 2
        $this->xml = simplexml_load_string($xmlBody);
46 2
        if ($this->xml === false)
47 2
            throw new Exception('XML body is not valid.');
48 2
        if ($this->xml->order) {
49 2
50 2
            foreach ($this->xml->order->statushistory->status as $statusItem) {
51 2
                $this->statuses[] = new DalliOrderStatusEvent(
52 2
                    trim($statusItem[0]),
53 2
                    $statusItem['eventstore'],
54 2
                    strtotime($statusItem['eventtime']));
55
            }
56
57 2
58
            foreach ($this->xml->order->items->item as $item) {
59
                $orderItem = new Item();
60
                $orderItem->setBarcode((string)$item['barcode'])
61
                    ->setQuantity((int)$item['quantity'])
62
                    ->setReturn(boolval($item['returns']))
63 1
                    ->setRetprice((float)$item['retprice']);
64
                $this->items[] = $orderItem;
65 1
            }
66 1
        }
67 1
    }
68
69
    /**
70 1
     * @param string|int $barcode
71
     * @return bool|null
72 1
     */
73
    public function isItemReturned($barcode): ?bool
74
    {
75 1
        if (isset($this->items[$barcode]))
76
            return $this->items[$barcode]->isReturned();
77 1
        return null;
78
    }
79
80 1
    public function getStatusId(): ?string
81
    {
82 1
        if ($this->statuses) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->statuses of type floor12\DalliApi\Models\...DalliOrderStatusEvent[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
83
            return $this->statuses[0]->getStatusId();
84
        }
85
        return null;
86
    }
87
88 1
    public function getStatusName(): ?string
89
    {
90 1
        if ($this->getStatusId())
91
            DalliOrderStatus::getLabel($this->getStatusId());
92
        return null;
93
    }
94
95
    public function getStatusTimestamp(): ?int
96
    {
97
        if ($this->statuses) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->statuses of type floor12\DalliApi\Models\...DalliOrderStatusEvent[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
98
            return $this->statuses[0]->getTimestamp();
99
        }
100
        return null;
101
    }
102
103
    public function getItems(): array
104
    {
105
        return $this->items;
106
    }
107
108
    /**
109
     * @return Item[]
110
     */
111
    public function getReturnedItems(): array
112
    {
113
        $returned = [];
114
        foreach ($this->items as $item) {
115
            if ($item->isReturned())
116
                $returned[] = $item;
117
        }
118
        return $returned;
119
    }
120
121
    public function getOrderHistory(): ?array
122
    {
123
        return $this->statuses;
124
    }
125
126
    public function getStatuses(): array
127
    {
128
        return $this->statuses;
129
    }
130
131
    public function getExternalBarCode()
132
    {
133
        return $this->externalBarCode;
134
    }
135
}
136