Completed
Pull Request — master (#2)
by
unknown
08:19
created

VentureClient::updatePrice()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 2

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 29
ccs 19
cts 19
cp 1
rs 8.8571
cc 2
eloc 20
nc 2
nop 5
crap 2
1
<?php
2
3
namespace Iris\Api;
4
5
class VentureClient extends BaseClient
6
{
7
    /**
8
     * Let the partner know the order was shipped,
9
     * will be changed to allow to inform the status by item
10
     *
11
     * @param string $orderNumber
12
     * @param array $request
13
     * @return bool|\GuzzleHttp\Message\Response
14
     */
15 2
    public function shippedOrder($orderNumber, array $request)
16
    {
17 2
        $response = false;
18 2
        $url = 'api/' . $this->getVersion() . '/order/' . $orderNumber . '/ship';
19
20
        try {
21 2
            $response = $this->update($url, $request);
22 2
        } catch (\Exception $e) {
23 1
            $this->throwException($e, 'Unable to ship order', ['OrderNr' => $orderNumber]);
24
        }
25
26 2
        return $response;
27
    }
28
29
    /**
30
     * Create products on partner
31
     *
32
     * @param \Iris\Transfer\Catalog\ConfigCollection $products
33
     * @param string $partnerCode
34
     * @return bool|\GuzzleHttp\Message\Response
35
     */
36 2
    public function createProducts(\Iris\Transfer\Catalog\ConfigCollection $products, $partnerCode)
37
    {
38 2
        $response = false;
39 2
        $url = 'api/' . $this->getVersion() . '/' . $partnerCode . '/product';
40 2
        $body = $products->toSimpleArray();
41
42
        try {
43 2
            $response = $this->create($url, $body);
44 2
        } catch (\Exception $e) {
45 1
            $this->throwException($e, 'Unable to create products');
46
        }
47
48 2
        return $response;
49
    }
50
51
    /**
52
     * Update products on partner
53
     *
54
     * @param \Iris\Transfer\Catalog\ConfigCollection $products
55
     * @return bool|\GuzzleHttp\Message\Response
56
     */
57 2
    public function updateProducts(\Iris\Transfer\Catalog\ConfigCollection $products)
58
    {
59 2
        $response = false;
60 2
        $url = 'api/' . $this->getVersion() . '/product';
61 2
        $body = $products->toSimpleArray();
62
63
        try {
64 2
            $response = $this->update($url, $body);
65 2
        } catch (\Exception $e) {
66 1
            $this->throwException($e, 'Unable to update products');
67
        }
68
69 2
        return $response;
70
    }
71
72
    /**
73
     * Inform partner to update stock
74
     *
75
     * @param string $sku
76
     * @param integer $quantity
77
     * @return bool
78
     */
79 2
    public function updateStock($sku, $quantity)
80
    {
81 2
        $response = false;
82 2
        $url = 'api/' . $this->getVersion() . '/product/' . $sku . '/stock';
83 2
        $body = ['quantity' => (int) $quantity];
84
85
        try {
86 2
            $response = $this->update($url, $body);
87 2
        } catch (\Exception $e) {
88 1
            $this->throwException($e, 'Unable to update stock', ['Sku' => $sku, 'Quantity' => $quantity]);
89
        }
90
91 2
        return $response;
92
    }
93
94
    /**
95
     * Inform the partner a price update
96
     *
97
     * @param string $sku
98
     * @param float $price
99
     * @param float $specialPrice
100
     * @param string $specialFromDate
101
     * @param string $specialToDate
102
     * @return bool
103
     */
104 2
    public function updatePrice($sku, $price, $specialPrice, $specialFromDate, $specialToDate)
105
    {
106 2
        $response = false;
107 2
        $url = 'api/' . $this->getVersion() . '/product/' . $sku . '/price';
108
        $body = [
109 2
            'price' => (float) $price,
110 2
            'special_price' => (float) $specialPrice,
111 2
            'special_from_date' => $specialFromDate,
112
            'special_to_date' => $specialToDate
113 2
        ];
114
115
        try {
116 2
            $response = $this->update($url, $body);
117 2
        } catch (\Exception $e) {
118 1
            $this->throwException(
119 1
                $e,
120 1
                'Unable to update price',
121
                [
122 1
                    'Sku'               => $sku,
123 1
                    'Price'             => $price,
124 1
                    'SpecialPrice'      => $specialPrice,
125 1
                    'SpecialFromDate'   => $specialFromDate,
126
                    'SpecialToDate'     => $specialToDate
127 1
                ]
128 1
            );
129
        }
130
131 2
        return $response;
132
    }
133
134
    /**
135
     * @param \Iris\Transfer\Tracking\ShippedCollection $items
136
     * @return bool
137
     */
138
    public function setStatusToShipped(\Iris\Transfer\Tracking\ShippedCollection $items)
139
    {
140
        $bodyData = [];
141
142
        foreach ($items as $item) {
143
            $bodyData['items'][] = [
144
                'venture_order_item_id' => $item->getVentureOrderItemId(),
145
                'delivery_type' => $item->getDeliveryType(),
146
                'shipping_provider' => $item->getShippingProvider(),
147
                'tracking_url' => $item->getTrackingUrl(),
148
                'nfe_key' => $item->getNfeKey(),
149
            ];
150
        }
151
152
        $url = sprintf(
153
            'api/%s/order/%s/ship',
154
            $this->getVersion(),
155
            $items->offsetGet(0)->getVentureOrderNumber()
156
        );
157
158
        try {
159
            $this->update($url, $bodyData);
160
        } catch (\Exception $e) {
161
            $this->throwException(
162
                $e,
163
                'Unable to set status to ship',
164
                [
165
                    'VentureOrderItemId' => $items->offsetGet(0)->getVentureOrderItemId(),
166
                    'PartnerCode' => $items->offsetGet(0)->getPartnerCode(),
167
                    'DeliveryType' => $items->offsetGet(0)->getDeliveryType(),
168
                    'ShippingProvider' => $items->offsetGet(0)->getShippingProvider(),
169
                    'TrackingUrl' => $items->offsetGet(0)->getTrackingUrl(),
170
                    'NfeKey' => $items->offsetGet(0)->getNfeKey(),
171
                    'VentureOrderNumber' => $items->offsetGet(0)->getVentureOrderNumber(),
172
                    'Event' => 'tracking-update',
173
                    'EventType' => 'notify-externalshop-shipped',
174
                ]
175
            );
176
177
            return false;
178
        }
179
180
        return true;
181
    }
182
183
    /**
184
     * @param \Iris\Transfer\Tracking\Delivered $status
185
     * @return bool
186
     */
187
    public function setStatusToDelivered(\Iris\Transfer\Tracking\DeliveredCollection $status)
188
    {
189
        $bodyData = [
190
            'items' => [
191
                [
192
                    'venture_order_item_id' => $status->getVentureOrderItemId(),
0 ignored issues
show
Bug introduced by
The method getVentureOrderItemId() does not seem to exist on object<Iris\Transfer\Tra...ng\DeliveredCollection>.

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...
193
                ]
194
            ]
195
        ];
196
197
        $url = sprintf(
198
            'api/%s/order/%s/deliver',
199
            $this->getVersion(),
200
            $status->getVentureOrderNumber()
0 ignored issues
show
Bug introduced by
The method getVentureOrderNumber() does not seem to exist on object<Iris\Transfer\Tra...ng\DeliveredCollection>.

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...
201
        );
202
203
        try {
204
            $this->update($url, $bodyData);
205
        } catch (\Exception $e) {
206
            $this->throwException(
207
                $e,
208
                'Unable to set status to delivered',
209
                [
210
                    'VentureOrderItemId' => $status->getVentureOrderItemId(),
0 ignored issues
show
Bug introduced by
The method getVentureOrderItemId() does not seem to exist on object<Iris\Transfer\Tra...ng\DeliveredCollection>.

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...
211
                    'PartnerCode' => $status->getPartnerCode(),
0 ignored issues
show
Bug introduced by
The method getPartnerCode() does not seem to exist on object<Iris\Transfer\Tra...ng\DeliveredCollection>.

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...
212
                    'VentureOrderNumber' => $status->getVentureOrderNumber(),
0 ignored issues
show
Bug introduced by
The method getVentureOrderNumber() does not seem to exist on object<Iris\Transfer\Tra...ng\DeliveredCollection>.

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...
213
                    'Event' => 'tracking-update',
214
                    'EventType' => 'notify-externalshop-delivered',
215
                ]
216
            );
217
218
            return false;
219
        }
220
221
        return true;
222
    }
223
224
    /**
225
     * @param \Iris\Transfer\Tracking\FailedDelivery $status
0 ignored issues
show
Bug introduced by
There is no parameter named $status. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
226
     * @return bool
227
     */
228
    public function setStatusToFailedDelivery(\Iris\Transfer\Tracking\FailedDeliveryCollection $items)
0 ignored issues
show
Unused Code introduced by
The parameter $items is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
229
    {
230
        $bodyData = [
231
            'items' => [
232
                [
233
                    'venture_order_item_id' => $status->getVentureOrderItemId(),
0 ignored issues
show
Bug introduced by
The variable $status does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
234
                    'reason' => $status->getReason(),
235
                    'reason_detail' => $status->getReasonDetail(),
236
                ]
237
            ]
238
        ];
239
240
        $url = sprintf(
241
            'api/%s/order/%s/failed-delivery',
242
            $this->getVersion(),
243
            $status->getVentureOrderNumber()
244
        );
245
246
        try {
247
            $this->update($url, $bodyData);
248
        } catch (\Exception $e) {
249
            $this->throwException(
250
                $e,
251
                'Unable to set status to failed delivery',
252
                [
253
                    'VentureOrderItemId' => $status->getVentureOrderItemId(),
254
                    'PartnerCode' => $status->getPartnerCode(),
255
                    'Reason' => $status->getReason(),
256
                    'ReasonDetail' => $status->getReasonDetail(),
257
                    'VentureOrderNumber' => $status->getVentureOrderNumber(),
258
                    'Event' => 'tracking-update',
259
                    'EventType' => 'notify-externalshop-failed-delivery',
260
                ]
261
            );
262
263
            return false;
264
        }
265
266
        return true;
267
    }
268
269
    /**
270
     * @param \Iris\Transfer\Tracking\CanceledCollection $items
271
     * @throws \RuntimeException Not implemented yet!
272
     */
273
    public function setStatusToCanceled(\Iris\Transfer\Tracking\CanceledCollection $items)
0 ignored issues
show
Unused Code introduced by
The parameter $items is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
274
    {
275
        throw new \RuntimeException('Not implemented yet!');
276
    }
277
}
278