Test Failed
Push — master ( d9d4f1...04fcd4 )
by Evgenii
06:20
created

OrderStatusDispatcher::getExternalBarCode()   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 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
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;
0 ignored issues
show
introduced by
The private property $externalBarCode is not used, and could be removed.
Loading history...
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
49 2
        foreach ($this->xml->order->statushistory->status as $statusItem) {
50 2
            $this->statuses[] = new DalliOrderStatusEvent(
51 2
                trim($statusItem[0]),
52 2
                $statusItem['eventstore'],
53 2
                strtotime($statusItem['eventtime']));
54 2
        }
55
56
57 2
        foreach ($this->xml->order->items->item as $item) {
58
            $orderItem = new Item();
59
            $orderItem->setBarcode((string)$item['barcode'])
60
                ->setQuantity((int)$item['quantity'])
61
                ->setReturn(boolval($item['returns']))
62
                ->setRetprice((float)$item['retprice']);
63 1
            $this->items[] = $orderItem;
64
        }
65 1
    }
66 1
67 1
    /**
68
     * @param string|int $barcode
69
     * @return bool|null
70 1
     */
71
    public function isItemReturned($barcode): ?bool
72 1
    {
73
        if (isset($this->items[$barcode]))
74
            return $this->items[$barcode]->isReturned();
75 1
        return null;
76
    }
77 1
78
    public function getStatusId(): ?string
79
    {
80 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...
81
            return $this->statuses[0]->getStatusId();
82 1
        }
83
        return null;
84
    }
85
86
    public function getStatusName(): ?string
87
    {
88 1
        if ($this->getStatusId())
89
            DalliOrderStatus::getLabel($this->getStatusId());
90 1
        return null;
91
    }
92
93
    public function getStatusTimestamp(): ?int
94
    {
95
        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...
96
            return $this->statuses[0]->getTimestemp();
0 ignored issues
show
Bug introduced by
The method getTimestemp() does not exist on floor12\DalliApi\Models\...e\DalliOrderStatusEvent. Did you maybe mean getTimestamp()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

96
            return $this->statuses[0]->/** @scrutinizer ignore-call */ getTimestemp();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
97
        }
98
        return null;
99
    }
100
101
    public function getItems(): array
102
    {
103
        return $this->items;
104
    }
105
106
    /**
107
     * @return Item[]
108
     */
109
    public function getReturnedItems(): array
110
    {
111
        $returned = [];
112
        foreach ($this->items as $item) {
113
            if ($item->isReturned())
114
                $returned[] = $item;
115
        }
116
        return $returned;
117
    }
118
119
    public function getOrderHistory(): ?array
120
    {
121
        return $this->statuses;
122
    }
123
}
124