Order::setOrderComment()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 14
loc 14
ccs 0
cts 9
cp 0
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
namespace Loevgaard\Dandomain\Api\Endpoint;
3
4
use Assert\Assert;
5
6
class Order extends Endpoint
7
{
8
    /**
9
     * @param \DateTimeInterface $dateStart
10
     * @param \DateTimeInterface $dateEnd
11
     * @param int|null $orderStateId
12
     * @return int
13
     */
14
    public function countByModifiedInterval(\DateTimeInterface $dateStart, \DateTimeInterface $dateEnd, int $orderStateId = null) : int
15
    {
16
        Assert::that($dateStart)->lessThan($dateEnd, '$dateStart must be before $dateEnd');
17
        Assert::thatNullOr($orderStateId)->integer('$orderStateId must be an integer');
18
19
        $q = sprintf(
20
            '/admin/WEBAPI/Endpoints/v1_0/OrderService/{KEY}/CountByModifiedInterval?start=%s&end=%s',
21
            $dateStart->format('Y-m-d\TH:i:s'),
22
            $dateEnd->format('Y-m-d\TH:i:s')
23
        );
24
25
        if ($orderStateId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $orderStateId of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
26
            $q .= sprintf('&orderstateid=%d', $orderStateId);
27
        }
28
29
        return (int)$this->master->doRequest('GET', $q);
30
    }
31
32
    /**
33
     * @param array|\stdClass $order
34
     * @return array
35
     */
36
    public function createOrder($order) : array
37
    {
38
        return (array)$this->master->doRequest('POST', '/admin/WEBAPI/Endpoints/v1_0/OrderService/{KEY}', $order);
39
    }
40
41
    /**
42
     * @param \DateTimeInterface $dateStart
43
     * @param \DateTimeInterface $dateEnd
44
     * @return array
45
     */
46
    public function getOrders(\DateTimeInterface $dateStart, \DateTimeInterface $dateEnd) : array
47
    {
48
        Assert::that($dateStart)->lessThan($dateEnd, '$dateStart must be before $dateEnd');
49
50
        return (array)$this->master->doRequest(
51
            'GET',
52
            sprintf(
53
                '/admin/WEBAPI/Endpoints/v1_0/OrderService/{KEY}/GetByDateInterval?start=%s&end=%s',
54
                $dateStart->format('Y-m-d\TH:i:s'),
55
                $dateEnd->format('Y-m-d\TH:i:s')
56
            )
57
        );
58
    }
59
60
    /**
61
     * @param int $orderId
62
     * @return array
63
     */
64
    public function getOrder(int $orderId) : array
65
    {
66
        Assert::that($orderId)->greaterThan(0, 'The $orderId has to be positive');
67
68
        return (array)$this->master->doRequest('GET', sprintf('/admin/WEBAPI/Endpoints/v1_0/OrderService/{KEY}/%d', $orderId));
69
    }
70
71
    /**
72
     * Deletes an order
73
     *
74
     * @param int $orderId
75
     * @return bool
76
     */
77
    public function deleteOrder(int $orderId) : bool
78
    {
79
        Assert::that($orderId)->greaterThan(0, 'The $orderId has to be positive');
80
81
        return (bool)$this->master->doRequest('DELETE', sprintf('/admin/WEBAPI/Endpoints/v1_0/OrderService/{KEY}/%d', $orderId));
82
    }
83
84
    /**
85
     * @param int $orderId
86
     * @return bool
87
     */
88
    public function completeOrder(int $orderId) : bool
89
    {
90
        Assert::that($orderId)->greaterThan(0, 'The $orderId has to be positive');
91
92
        return (bool)$this->master->doRequest(
93
            'PUT',
94
            sprintf('/admin/WEBAPI/Endpoints/v1_0/OrderService/{KEY}/CompleteOrder/%d', $orderId)
95
        );
96
    }
97
98
    /**
99
     * @param int $customerNumber
100
     * @return array
101
     */
102
    public function getOrdersByCustomerNumber(int $customerNumber) : array
103
    {
104
        Assert::that($customerNumber)->greaterThan(0, 'The $customerNumber has to be positive');
105
106
        return (array)$this->master->doRequest(
107
            'GET',
108
            sprintf('/admin/WEBAPI/Endpoints/v1_0/OrderService/{KEY}/GetByCustomerNumber/%d', $customerNumber)
109
        );
110
    }
111
112
    /**
113
     * @param \DateTimeInterface $dateStart
114
     * @param \DateTimeInterface $dateEnd
115
     * @param int $page
116
     * @param int $pageSize
117
     * @param int|null $orderStateId
118
     * @return array
119
     */
120
    public function getOrdersInModifiedInterval(\DateTimeInterface $dateStart, \DateTimeInterface $dateEnd, int $page = 1, int $pageSize = 100, int $orderStateId = null) : array
121
    {
122
        Assert::that($dateStart)->lessThan($dateEnd, '$dateStart must be before $dateEnd');
123
        Assert::that($page)->greaterThan(0, 'The $page has to be positive');
124
        Assert::that($pageSize)->greaterThan(0, 'The $pageSize has to be positive');
125
        Assert::thatNullOr($orderStateId)->integer('$orderStateId must be an integer');
126
127
        $q = sprintf(
128
            '/admin/WEBAPI/Endpoints/v1_0/OrderService/{KEY}/GetByModifiedInterval?start=%s&end=%s&pageIndex=%d&pageSize=%d',
129
            $dateStart->format('Y-m-d\TH:i:s'),
130
            $dateEnd->format('Y-m-d\TH:i:s'),
131
            $page,
132
            $pageSize
133
        );
134
135
        if ($orderStateId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $orderStateId of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
136
            $q .= sprintf('&orderstateid=%d', $orderStateId);
137
        }
138
139
        return (array)$this->master->doRequest('GET', $q);
140
    }
141
142
    /**
143
     * @return array
144
     */
145
    public function getOrderStates() : array
146
    {
147
        return (array)$this->master->doRequest('GET', '/admin/WEBAPI/Endpoints/v1_0/OrderService/{KEY}/OrderStates');
148
    }
149
150
    /**
151
     * @param int $orderId
152
     * @param string $comment
153
     * @return bool
154
     */
155 View Code Duplication
    public function setOrderComment(int $orderId, string $comment) : bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
156
    {
157
        Assert::that($orderId)->greaterThan(0, 'The $orderId has to be positive');
158
        Assert::that($comment)->minLength(1, 'The length of $comment has to be > 0');
159
160
        return (bool)$this->master->doRequest(
161
            'PUT',
162
            sprintf(
163
                '/admin/WEBAPI/Endpoints/v1_0/OrderService/{KEY}/SetOrderComment/%d?comment=%s',
164
                $orderId,
165
                rawurlencode($comment)
166
            )
167
        );
168
    }
169
170
    /**
171
     * @param int $orderId
172
     * @param int $orderState
173
     * @return bool
174
     */
175 View Code Duplication
    public function setOrderState(int $orderId, int $orderState) : bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
176
    {
177
        Assert::that($orderId)->greaterThan(0, 'The $orderId has to be positive');
178
        Assert::that($orderState)->greaterThan(0, 'The $orderState has to be positive');
179
180
        return (bool)$this->master->doRequest(
181
            'PUT',
182
            sprintf('/admin/WEBAPI/Endpoints/v1_0/OrderService/{KEY}/SetOrderState/%d/%d', $orderId, $orderState)
183
        );
184
    }
185
186
    /**
187
     * @param int $orderId
188
     * @param string $trackingNumber
189
     * @return bool
190
     */
191 View Code Duplication
    public function setTrackNumber(int $orderId, string $trackingNumber) : bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
192
    {
193
        Assert::that($orderId)->greaterThan(0, 'The $orderId has to be positive');
194
        Assert::that($trackingNumber)->minLength(1, 'The length of $trackingNumber has to be > 0');
195
196
        return (bool)$this->master->doRequest(
197
            'PUT',
198
            sprintf(
199
                '/admin/WEBAPI/Endpoints/v1_0/OrderService/{KEY}/SetTrackNumber/%d?tracknumber=%s',
200
                $orderId,
201
                rawurlencode($trackingNumber)
202
            )
203
        );
204
    }
205
206
    /******************
207
     * Helper methods *
208
     *****************/
209
    /**
210
     * Will copy an order based on the order id
211
     * If the $orderLines is empty, it will also copy the order lines
212
     * If the $orderState is > 0, the method will update the order state
213
     * If $completeOrder is true, the method will also complete the order, otherwise it will be marked as incomplete by default
214
     * Returns the new order
215
     *
216
     * @param int $orderId
217
     * @param array $orderLines
218
     * @param int $orderState
219
     * @param boolean $completeOrder
220
     * @return array
221
     */
222
    public function copyOrder(int $orderId, array $orderLines = [], int $orderState = 0, bool $completeOrder = true) : array
223
    {
224
        Assert::that($orderId)->greaterThan(0, 'The $orderId has to be positive');
225
226
        $order = $this->getOrder($orderId);
227
228
        $data = [
229
            'siteId'            => $order['siteId'],
230
            'altDeliveryInfo'   => null,
231
            'currencyCode'      => $order['currencyCode'],
232
            'customerId'        => $order['customerInfo']['id'],
233
            'paymentId'         => $order['paymentInfo']['id'],
234
            'shippingId'        => $order['shippingInfo']['id'],
235
            'orderLines'        => $order['orderLines']
236
        ];
237
238
        if (!empty($orderLines)) {
239
            $data['orderLines'] = $orderLines;
240
        }
241
242
        $newOrder = $this->createOrder($data);
243
        $newOrderId = (int)$newOrder['id'];
244
245
        if ($completeOrder) {
246
            $this->completeOrder($newOrderId);
247
        }
248
249
        if ($orderState) {
250
            $this->setOrderState($newOrderId, $orderState);
251
        }
252
253
        return $newOrder;
254
    }
255
}
256