Completed
Pull Request — master (#6)
by Rafael
07:18
created

Partner::updateItemsWithTheIncomingData()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 5.0043

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 28
ccs 17
cts 18
cp 0.9444
rs 8.439
cc 5
eloc 16
nc 9
nop 2
crap 5.0043
1
<?php
2
3
namespace Iris\SaleWrapper;
4
5
class Partner extends Base implements PartnerInterface
6
{
7
    /**
8
     * Gets Venture service.
9
     * @return Iris\Interfaces\IrisVenture
10
     */
11 9
    public function getVentureService()
12
    {
13 9
        return $this->getManager()->getService(\Iris\Factory::VENTURE);
14
    }
15
16
    /**
17
     * Gets Stock service.
18
     * @return Iris\Interfaces\Stock
19
     */
20 3
    public function getStockService()
21
    {
22 3
        return $this->getManager()->getService(\Iris\Factory::STOCK);
23
    }
24
25
    /**
26
     * Gets Stock service.
27
     * @return Iris\Interfaces\Catalog
28
     */
29 2
    public function getCatalogService()
30
    {
31 2
        return $this->getManager()->getService(\Iris\Factory::CATALOG);
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 2
    public function confirmReserveAndUpdateStock($sku, $itemId, $ventureCode, $stockQuantity, $orderNumber)
38
    {
39 2
        $this->confirmReserve($sku, $itemId, $ventureCode, $orderNumber);
40 2
        $this->updateStock($sku, $stockQuantity);
41 2
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 1
    public function confirmReserve($sku, $itemId, $ventureCode, $orderNumber)
47
    {
48 1
        $venture = $this->getVentureService()->findByCode($ventureCode);
0 ignored issues
show
Bug introduced by
The method findByCode() does not seem to exist on object<Iris\Interfaces\Service>.

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...
49
50 1
        $item = $this->getOrderService()->findItemByIdAndSkuAndVentureCode(
51 1
            $itemId,
52 1
            $sku,
53 1
            $venture->getVentureCode(),
54
            $orderNumber
55 1
        );
56
57 1
        $this->getStockService()->dropReserve($item);
0 ignored issues
show
Bug introduced by
The method dropReserve() does not seem to exist on object<Iris\Interfaces\Service>.

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...
58 1
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 1
    public function updateStock($sku, $quantity)
64
    {
65 1
        $product = $this->getCatalogService()->getProductDataBySku($sku);
0 ignored issues
show
Bug introduced by
The method getProductDataBySku() does not seem to exist on object<Iris\Interfaces\Service>.

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...
66 1
        return $this->getStockService()->updateAbsoluteStock($product['sku'], $quantity);
0 ignored issues
show
Bug introduced by
The method updateAbsoluteStock() does not seem to exist on object<Iris\Interfaces\Service>.

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...
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 1
    public function isVentureItem(\Iris\Transfer\Sales\Order\Item $item)
73
    {
74 1
        $irisVenture = $this->getVentureService()
0 ignored issues
show
Bug introduced by
The method getIrisVentureBySalesOrderId() does not seem to exist on object<Iris\Interfaces\Service>.

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...
75 1
            ->getIrisVentureBySalesOrderId($item->getFkSalesOrder());
76
77 1
        return ($irisVenture instanceof \Iris\Transfer\Venture);
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 4
    public function findOrderByOrderNumber($orderNumber, $originCode)
84
    {
85 4
        return $this->getOrderService()
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getOrderSe...erNumber, $originCode); (Iris\Transfer\Sales\Order) is incompatible with the return type declared by the interface Iris\SaleWrapper\BaseInt...:findOrderByOrderNumber of type Iris\SaleWrapper\Iris\Transfer\Sales\Order.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
86 4
            ->getOrderByOrderNumberAndVentureCode($orderNumber, $originCode);
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 1
    public function sendOrderToVenture($orderNumber, $ventureCode)
93
    {
94 1
        $order = $this->findOrderByOrderNumber($orderNumber, $ventureCode);
95
96 1
        $mappedOrder = $this->getOrderMapping()->map($order);
97 1
        return $this->getPartnerApiClient()->createOrderOnVenture($ventureCode, $mappedOrder);
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 1
    public function confirmOrderPaidOnVenture($orderNumber, $ventureCode)
104
    {
105 1
        $order = $this->findOrderByOrderNumber($orderNumber, $ventureCode);
106
107 1
        return $this->getPartnerApiClient()
108 1
            ->confirmPaymentForVenture($ventureCode, $order->getOrderNr());
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 1
    public function cancelOrderOnVenture($orderNumber, $ventureCode)
115
    {
116 1
        $order = $this->findOrderByOrderNumber($orderNumber, $ventureCode);
117 1
        return $this->getPartnerApiClient()
118 1
            ->cancelOrderOnVenture($ventureCode, $order->getOrderNr());
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124 1
    public function cancelOrder($orderNumber, $originCode)
125
    {
126 1
        $venture = $this->getVentureService()->findByCode($originCode);
0 ignored issues
show
Bug introduced by
The method findByCode() does not seem to exist on object<Iris\Interfaces\Service>.

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...
127 1
        $order = $this->getOrderService()->getOrderByOrderNumberAndVentureCode(
128 1
            $orderNumber,
129 1
            $venture->getVentureCode()
130 1
        );
131 1
        $this->getOrderService()->cancel($order);
132 1
        return ['order_number' => $order->getOrderNr()];
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138 2
    public function getName()
139
    {
140 2
        return 'partner';
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146 1
    public function shippedOrder(\Iris\Transfer\Sales\Order $order, $originCode)
147
    {
148 1
        $itemsRequestedToBeShipped = $this->getOrderItems($order);
149 1
        $venture                   = $this->getVentureService()->findByCode($originCode);
0 ignored issues
show
Bug introduced by
The method findByCode() does not seem to exist on object<Iris\Interfaces\Service>.

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...
150
151 1
        $salesOrder = $this->getOrderService()
152 1
            ->getOrderByOrderNumberAndOrderItemsAndVentureCode(
153 1
                $order->getOrderNr(),
154 1
                $itemsRequestedToBeShipped,
155 1
                $venture->getVentureCode()
156 1
            );
157
158 1
        $salesOrder = $this->updateItemsWithTheIncomingData(
159 1
            $salesOrder,
160 1
            $order->getItemCollection()
161 1
        );
162
163 1
        return $this->getOrderService()->shippedByVenture($salesOrder);
164
    }
165
    /**
166
     * {@inheritdoc}
167
     */
168 1
    public function deliveredOrder(\Iris\Transfer\Sales\Order $order, $originCode)
169
    {
170 1
        $itemsRequestedToBeDelivery = $this->getOrderItems($order);
171 1
        $venture                    = $this->getVentureService()->findByCode($originCode);
0 ignored issues
show
Bug introduced by
The method findByCode() does not seem to exist on object<Iris\Interfaces\Service>.

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...
172
173 1
        $salesOrder = $this->getOrderService()
174 1
            ->getOrderByOrderNumberAndOrderItemsAndVentureCode(
175 1
                $order->getOrderNr(),
176 1
                $itemsRequestedToBeDelivery,
177 1
                $venture->getVentureCode()
178 1
            );
179
180 1
        return $this->getOrderService()->deliveredByVenture($salesOrder);
181
    }
182
183
    /**
184
     * {@inheritdoc}
185
     */
186 1
    public function deliveryFailedOrder(\Iris\Transfer\Sales\Order $order, $originCode)
187
    {
188 1
        $itemsRequestedToBeDeliveryFailed = $this->getOrderItems($order);
189 1
        $venture                          = $this->getVentureService()->findByCode($originCode);
0 ignored issues
show
Bug introduced by
The method findByCode() does not seem to exist on object<Iris\Interfaces\Service>.

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...
190
191 1
        $salesOrder = $this->getOrderService()
192 1
            ->getOrderByOrderNumberAndOrderItemsAndVentureCode(
193 1
                $order->getOrderNr(),
194 1
                $itemsRequestedToBeDeliveryFailed,
195 1
                $venture->getVentureCode()
196 1
            );
197
198 1
        return $this->getOrderService()->deliveryFailedByVenture($salesOrder);
199
    }
200
201
    /**
202
     * {@inheritdoc}
203
     */
204 1
    public function setNfeKey(
205
        $orderNumber,
206
        \Iris\Transfer\Sales\Order\ItemCollection $items,
207
        $ventureCode
208
    ) {
209 1
        foreach ($items as $item) {
210 1
            $this->getOrderService()
211 1
                ->setNFeKeyForItem(
212 1
                    $orderNumber,
213 1
                    $ventureCode,
214 1
                    $item->getIdSalesOrderItem(),
215 1
                    $item->getNfeKey()
216 1
                );
217 1
        }
218 1
    }
219
220
    /**
221
     * {@inheritdoc}
222
     */
223 1
    public function setNfeKeyAndTrackingUrl(\Iris\Transfer\Sales\Order $order, $originCode)
224
    {
225 1
        $itemsRequested = $this->getOrderItems($order);
226 1
        $venture        = $this->getVentureService()->findByCode($originCode);
0 ignored issues
show
Bug introduced by
The method findByCode() does not seem to exist on object<Iris\Interfaces\Service>.

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...
227 1
        $orderService   = $this->getOrderService();
228
        
229 1
        $salesOrder     = $orderService->getOrderByOrderNumberAndOrderItemsAndVentureCode(
230 1
            $order->getOrderNr(),
231 1
            $itemsRequested,
232 1
            $venture->getVentureCode()
233 1
        );
234
235 1
        $orderItemCollection = $order->getItemCollection();
236
237 1
        foreach ($orderItemCollection as $item) {
238
            $data = [
239 1
                'nfe_key' => $item->getNfeKey(),
240 1
                'tracking_url' => $item->getTrackingUrl(),
241 1
                'tracking_code' => md5($item->getTrackingUrl()),
242 1
            ];
243
244
            /**
245
             * @todo When ExternalShop starts to send the tracking_code key we
246
             * must put this as required and not more as optionally.
247
             */
248 1
            if ($item->getTrackingCode()) {
249 1
                $data['tracking_code'] = $item->getTrackingCode();
250 1
            }
251
252 1
            $orderService->updateItem(
253 1
                $salesOrder->getIdSalesOrder(),
0 ignored issues
show
Documentation Bug introduced by
The method getIdSalesOrder does not exist on object<Iris\Transfer\Sales\Order>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
254 1
                $venture->getVentureCode(),
255 1
                $item->getIdSalesOrderItem(),
256
                $data
257 1
            );
258 1
        }
259 1
    }
260
261
    /**
262
     * {@inheritdoc}
263
     */
264 1
    public function isInvalidOrder(\Iris\Transfer\Sales\Order $order)
265
    {
266 1
        return $this->getOrderService()->isInvalidOrder($order);
267
    }
268
269
    /**
270
     * @param Iris\Transfer\Sales\Order $order
271
     * @return array
272
     */
273 4
    private function getOrderItems(\Iris\Transfer\Sales\Order $order)
274
    {
275 4
        $orderItems = [];
276 4
        $orderItemCollection = $order->getItemCollection();
277
278 4
        foreach ($orderItemCollection as $item) {
279 4
            $orderItems[] = $item->getIdSalesOrderItem();
280 4
        }
281
282 4
        return $orderItems;
283
    }
284
285
    /**
286
     * @param Iris\Transfer\Sales\Order $order
287
     * @param Iris\Trnasfer\Sales\Order\ItemsCollection $items
288
     * @return Iris\Transfer\Sales\Order
289
     */
290 1
    private function updateItemsWithTheIncomingData(
291
        \Iris\Transfer\Sales\Order $order,
292
        \Iris\Transfer\Sales\Order\ItemCollection $items
293
    ) {
294 1
        foreach ($items as $item) {
295
296 1
            $orderItem = $order->getItemCollection()->offsetGet($item->getIdSalesOrderItem());
297
298 1
            if (!empty($item->getTrackingUrl())) {
299 1
                $this->getOrderService()->setTrackingUrl(
300 1
                    $orderItem,
301 1
                    $item->getTrackingUrl()
302 1
                );
303 1
            }
304
305 1
            if (!empty($item->getNfeKey())) {
306 1
                $orderItem->setDanfeId($item->getNfeKey());
307 1
            }
308
309 1
            if (!empty($item->getTrackingCode())) {
310 1
                $orderItem->setTracking($item->getTrackingCode());
311 1
            } else {
312
                $orderItem->setTracking(md5($item->getTrackingUrl()));
313
            }
314 1
        }
315
316 1
        return $order;
317
    }
318
319
    /**
320
     * {@inheritdoc}
321
     */
322 1
    public function bindVentureOrder($orderNumber, $ventureCode, $ventureOrderNumber)
323
    {
324 1
        $venture = $this->getVentureService()->findByCode($ventureCode);
0 ignored issues
show
Bug introduced by
The method findByCode() does not seem to exist on object<Iris\Interfaces\Service>.

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...
325
326 1
        $order = $this->getOrderService()
327 1
            ->getOrderByOrderNumberAndVentureCode(
328 1
                $orderNumber,
329 1
                $venture->getVentureCode()
330 1
            );
331
332 1
        $this->getVentureService()
0 ignored issues
show
Bug introduced by
The method bindPartnerOrderWithVentureOrder() does not seem to exist on object<Iris\Interfaces\Service>.

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...
333 1
            ->bindPartnerOrderWithVentureOrder(
334 1
                $order,
335 1
                $venture,
336
                $ventureOrderNumber
337 1
            );
338 1
    }
339
}
340