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

Venture::setStatusToCanceled()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 8
ccs 0
cts 0
cp 0
rs 9.4286
cc 2
eloc 4
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Iris\SaleWrapper;
4
5
use Iris\Exceptions\InvalidTransfer;
6
7
class Venture extends Base implements VentureInterface
8
{
9
    /**
10 9
     * @return \Iris\Interfaces\IrisPartner
11
     */
12 9
    public function getPartnerService()
13
    {
14
        return $this->getManager()->getService(\Iris\Factory::PARTNER);
15
    }
16
17
    /**
18 2
     * @return \Iris\Interfaces\IrisVenture
19
     */
20 2
    public function getVentureService()
21
    {
22
        return $this->getManager()->getService(\Iris\Factory::VENTURE);
23
    }
24
25
    /**
26 2
     * @return \Iris\Interfaces\Customer
27
     */
28 2
    public function getCustomerService()
29
    {
30
        return $this->getManager()->getService(\Iris\Factory::CUSTOMER);
31
    }
32
33
    /**
34 2
     * {@inheritdoc}
35
     */
36 2
    public function createOrder(array $orderData, $originCode)
37
    {
38 2
        $partner = $this->getPartnerService()->findByCode($originCode);
39
40 2
        $salesOrder = $this->getOrderMapping()->assign($orderData);
41
42 2
        $customer = $this->getCustomerService()->createByExternalData($salesOrder->getCustomer());
43
44
        $idIrisCustomer = $customer->getIdIrisCustomer();
45 2
46 2
        try {
47 2
            $irisOrder = $this->getOrderService()->getOrderNrByExternalshopNumberAndPartner(
48 2
                $orderData['order_number'],
49 1
                $partner->getPartnerCode()
50
            );
51 2
            $result = array('order_number' => $irisOrder);
52 1
53 1
        } catch (\Iris\Exceptions\OrderNotFound $exception) {
54 1
            $salesOrder = $this->getOrderService()->save($salesOrder);
55 1
            $orderData['id_iris_customer'] = $idIrisCustomer;
56
            $this->getVentureService()->saveExternalData($orderData, $salesOrder);
57
            $result = array('order_number' => $salesOrder->getOrderNr());
58 2
        }
59
60
        return $result;
61
    }
62
63
    /**
64 2
     * {@inheritdoc}
65
     */
66 2
    public function confirmPaymentOrder($orderNumber, $originCode)
67 2
    {
68 2
        $partner = $this->getPartnerService()->findByCode($originCode);
69 2
        $order = $this->getOrderService()->getOrderByOrderNumberAndPartnerCode(
70 2
            $orderNumber,
71 2
            $partner->getPartnerCode()
72 1
        );
73
        if (!$this->getOrderService()->confirmPayment($order)) {
74 1
            throw new \RuntimeException(sprintf('Order number %s can\'t be confirmed', $orderNumber));
75
        }
76
        return array('order_number' => $order->getOrderNr());
77
    }
78
79
    /**
80 1
     * {@inheritdoc}
81
     */
82 1
    public function shipOrderToPartner($orderNumber, $partnerCode)
83 1
    {
84 1
        $partner = $this->getPartnerService()->findByCode($partnerCode);
85 1
        $order = $this->getOrderService()->getOrderByOrderNumberAndPartnerCode(
86 1
            $orderNumber,
87
            $partner->getPartnerCode()
88 1
        );
89 1
90
        $item = $order->getItemCollection()[0];
91 1
        $additionals = unserialize($item->getHistoryLogAdditionals());
92
93 1
        $request = array();
94 1
95 1
        if (is_array($additionals)) {
96 1
            $trackingInfo = array_filter(
97 1
                $additionals,
98
                function ($additional) {
99 1
                    return $additional['type'] == 'Tracking_URL';
100 1
                }
101 1
            );
102 1
            if (isset($trackingInfo[0]) && isset($trackingInfo[0]['info'])) {
103 1
                $request = array('notification' => $trackingInfo[0]['info']);
104
            }
105 1
        }
106
107
        return $this->getVentureApiClient()->shippedOrder($order->getOrderNr(), $request);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getVenture...etOrderNr(), $request); (GuzzleHttp\Message\Response) is incompatible with the return type declared by the interface Iris\SaleWrapper\Venture...ace::shipOrderToPartner of type boolean.

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...
108
    }
109
110
    /**
111 1
     * {@inheritdoc}
112
     */
113 1
    public function findOrderByOrderNumber($orderNumber, $originCode)
114
    {
115
        return $this->getOrderService()->getOrderByOrderNumberAndPartnerCode($orderNumber, $originCode);
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...
116
    }
117
118
    /**
119 2
     * {@inheritdoc}
120
     */
121 2
    public function cancelOrder($orderNumber, $originCode)
122 2
    {
123 2
        $partner = $this->getPartnerService()->findByCode($originCode);
124 2
        $order = $this->getOrderService()->getOrderByOrderNumberAndPartnerCode(
125 2
            $orderNumber,
126 2
            $partner->getPartnerCode()
127 1
        );
128
        if (!$this->getOrderService()->cancel($order)) {
129 1
            throw new \RuntimeException(sprintf('Order number %s can\'t be canceled', $orderNumber));
130
        }
131
        return array('order_number' => $order->getOrderNr());
132
    }
133
134
    /**
135 1
     * {@inheritdoc}
136
     */
137 1
    public function createProductsOnPartner(\Iris\Transfer\Catalog\ConfigCollection $products, $partnerCode)
138 1
    {
139
        $this->getVentureApiClient()->createProducts($products, $partnerCode);
140
    }
141
142
    /**
143 1
     * {@inheritdoc}
144
     */
145 1
    public function updateProductsOnPartner(\Iris\Transfer\Catalog\ConfigCollection $products)
146 1
    {
147
        $this->getVentureApiClient()->updateProducts($products);
148
    }
149
150
    /**
151 1
     * {@inheritdoc}
152
     */
153 1
    public function updateStockOnPartner($skuSimple, $stock)
154 1
    {
155
        $this->getVentureApiClient()->updateStock($skuSimple, $stock);
156
    }
157
158
    /**
159 1
     * {@inheritdoc}
160
     */
161
    public function updatePriceOnPartner(
162
        $skuSimple,
163
        $price,
164
        $specialPrice = null,
165
        $specialFromDate = null,
166 1
        $specialToDate = null
167 1
    ) {
168 1
        $this->getVentureApiClient()->updatePrice(
169 1
            $skuSimple,
170 1
            $price,
171
            $specialPrice ?: $price,
172 1
            $specialFromDate,
173 1
            $specialToDate
174
        );
175
    }
176
177
    /**
178 1
     * {@inheritdoc}
179
     */
180 1
    public function isPartnerItem(\Iris\Transfer\Sales\Order\Item $item)
181 1
    {
182
        $irisPartner = $this->getPartnerService()->findBySalesOrderId($item->getFkSalesOrder());
183
        return ($irisPartner instanceof \Iris\Transfer\Partner);
184
    }
185
186
    /**
187 1
     * {@inheritdoc}
188
     */
189 1
    public function setStatusToShipped(\Iris\Transfer\Tracking\ShippedCollection $items)
190
    {
191
        foreach ($items as $item) {
192
            $this->validateTransfer($item);
193
        }
194
195
        return $this->getVentureApiClient()->setStatusToShipped($items);
196
    }
197
198
    /**
199
     * {@inheritdoc}
200
     */
201
    public function setStatusToDelivered(\Iris\Transfer\Tracking\DeliveredCollection $items)
202
    {
203
        foreach ($items as $item) {
204
            $this->validateTransfer($item);
205
        }
206
207
        return $this->getVentureApiClient()->setStatusToDelivered($items);
208
    }
209
210
    /**
211
     * NOTE: Not implemented yet!
212
     *
213
     * {@inheritdoc}
214
     */
215
    public function setStatusToCanceled(\Iris\Transfer\Tracking\CanceledCollection $items)
216
    {
217
        foreach ($items as $item) {
218
            $this->validateTransfer($item);
219
        }
220
221
        return $this->getVentureApiClient()->setStatusToCanceled($items);
222
    }
223
224
    /**
225
     * {@inheritdoc}
226
     */
227
    public function setStatusToFailedDelivery(\Iris\Transfer\Tracking\FailedDeliveryCollection $items)
228
    {
229
        foreach ($items as $item) {
230
            $this->validateTransfer($item);
231
        }
232
233
        return $this->getVentureApiClient()->setStatusToFailedDelivery($items);
234
    }
235
236
    /**
237
     * {@inheritdoc}
238
     */
239
    public function getName()
240
    {
241
        return 'venture';
242
    }
243
244
    /**
245
     * @param \Iris\Transfer\Tracking\AbstractTracking $transfer
246
     * @throws InvalidTransfer
247
     */
248
    private function validateTransfer(\Iris\Transfer\Tracking\AbstractTracking $transfer)
249
    {
250
        if (!$transfer->isValid()) {
251
            throw new InvalidTransfer('Invalid transfer data - ' . implode(', ', $transfer->getErrors()));
252
        }
253
    }
254
}
255