Completed
Pull Request — master (#2)
by
unknown
06:17 queued 03:34
created

Venture::validateTransfer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4286
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Iris\SaleWrapper;
4
5
use Iris\Exceptions\InvalidTransfer;
6
7
class Venture extends Base implements VentureInterface
8
{
9
    /**
10
     * @return \Iris\Interfaces\IrisPartner
11
     */
12 9
    public function getPartnerService()
13
    {
14 9
        return $this->getManager()->getService(\Iris\Factory::PARTNER);
15
    }
16
17
    /**
18
     * @return \Iris\Interfaces\IrisVenture
19
     */
20 2
    public function getVentureService()
21
    {
22 2
        return $this->getManager()->getService(\Iris\Factory::VENTURE);
23
    }
24
25
    /**
26
     * @return \Iris\Interfaces\Customer
27
     */
28 2
    public function getCustomerService()
29
    {
30 2
        return $this->getManager()->getService(\Iris\Factory::CUSTOMER);
31
    }
32
33
    /**
34
     * {@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 2
        $idIrisCustomer = $customer->getIdIrisCustomer();
45
46
        try {
47 2
            $irisOrder = $this->getOrderService()->getOrderNrByExternalshopNumberAndPartner(
48 2
                $orderData['order_number'],
49 2
                $partner->getPartnerCode()
50 2
            );
51 1
            $result = array('order_number' => $irisOrder);
52
53 2
        } catch (\Iris\Exceptions\OrderNotFound $exception) {
54 1
            $salesOrder = $this->getOrderService()->save($salesOrder);
55 1
            $orderData['id_iris_customer'] = $idIrisCustomer;
56 1
            $this->getVentureService()->saveExternalData($orderData, $salesOrder);
57 1
            $result = array('order_number' => $salesOrder->getOrderNr());
58
        }
59
60 2
        return $result;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 2
    public function confirmPaymentOrder($orderNumber, $originCode)
67
    {
68 2
        $partner = $this->getPartnerService()->findByCode($originCode);
69 2
        $order = $this->getOrderService()->getOrderByOrderNumberAndPartnerCode(
70 2
            $orderNumber,
71 2
            $partner->getPartnerCode()
72 2
        );
73 2
        if (!$this->getOrderService()->confirmPayment($order)) {
74 1
            throw new \RuntimeException(sprintf('Order number %s can\'t be confirmed', $orderNumber));
75
        }
76 1
        return array('order_number' => $order->getOrderNr());
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 1
    public function shipOrderToPartner($orderNumber, $partnerCode)
83
    {
84 1
        $partner = $this->getPartnerService()->findByCode($partnerCode);
85 1
        $order = $this->getOrderService()->getOrderByOrderNumberAndPartnerCode(
86 1
            $orderNumber,
87 1
            $partner->getPartnerCode()
88 1
        );
89
90 1
        $item = $order->getItemCollection()[0];
91 1
        $additionals = unserialize($item->getHistoryLogAdditionals());
92
93 1
        $request = array();
94
95 1
        if (is_array($additionals)) {
96 1
            $trackingInfo = array_filter(
97 1
                $additionals,
98 1
                function ($additional) {
99 1
                    return $additional['type'] == 'Tracking_URL';
100
                }
101 1
            );
102 1
            if (isset($trackingInfo[0]) && isset($trackingInfo[0]['info'])) {
103 1
                $request = array('notification' => $trackingInfo[0]['info']);
104 1
            }
105 1
        }
106
107 1
        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
     * {@inheritdoc}
112
     */
113 1
    public function findOrderByOrderNumber($orderNumber, $originCode)
114
    {
115 1
        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
     * {@inheritdoc}
120
     */
121 2
    public function cancelOrder($orderNumber, $originCode)
122
    {
123 2
        $partner = $this->getPartnerService()->findByCode($originCode);
124 2
        $order = $this->getOrderService()->getOrderByOrderNumberAndPartnerCode(
125 2
            $orderNumber,
126 2
            $partner->getPartnerCode()
127 2
        );
128 2
        if (!$this->getOrderService()->cancel($order)) {
129 1
            throw new \RuntimeException(sprintf('Order number %s can\'t be canceled', $orderNumber));
130
        }
131 1
        return array('order_number' => $order->getOrderNr());
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137 1
    public function createProductsOnPartner(\Iris\Transfer\Catalog\ConfigCollection $products, $partnerCode)
138
    {
139 1
        $this->getVentureApiClient()->createProducts($products, $partnerCode);
140 1
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145 1
    public function updateProductsOnPartner(\Iris\Transfer\Catalog\ConfigCollection $products)
146
    {
147 1
        $this->getVentureApiClient()->updateProducts($products);
148 1
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153 1
    public function updateStockOnPartner($skuSimple, $stock)
154
    {
155 1
        $this->getVentureApiClient()->updateStock($skuSimple, $stock);
156 1
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161 1
    public function updatePriceOnPartner(
162
        $skuSimple,
163
        $price,
164
        $specialPrice = null,
165
        $specialFromDate = null,
166
        $specialToDate = null
167
    ) {
168 1
        $this->getVentureApiClient()->updatePrice(
169 1
            $skuSimple,
170 1
            $price,
171 1
            $specialPrice ?: $price,
172 1
            $specialFromDate,
173
            $specialToDate
174 1
        );
175 1
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180 1
    public function isPartnerItem(\Iris\Transfer\Sales\Order\Item $item)
181
    {
182 1
        $irisPartner = $this->getPartnerService()->findBySalesOrderId($item->getFkSalesOrder());
183 1
        return ($irisPartner instanceof \Iris\Transfer\Partner);
184
    }
185
186
    /**
187
     * {@inheritdoc}
188
     */
189 2
    public function setStatusToShipped(\Iris\Transfer\Tracking\Shipped $status)
190
    {
191 2
        $this->validateTransfer($status);
192
193 1
        return $this->getVentureApiClient()->setStatusToShipped($status);
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199 2
    public function setStatusToDelivered(\Iris\Transfer\Tracking\Delivered $status)
200
    {
201 2
        $this->validateTransfer($status);
202
203 1
        return $this->getVentureApiClient()->setStatusToDelivered($status);
204
    }
205
206
    /**
207
     * NOTE: Not implemented yet!
208
     *
209
     * {@inheritdoc}
210
     */
211 2
    public function setStatusToCanceled(\Iris\Transfer\Tracking\Canceled $status)
212
    {
213 2
        $this->validateTransfer($status);
214
215 1
        return $this->getVentureApiClient()->setStatusToCanceled($status);
216
    }
217
218
    /**
219
     * {@inheritdoc}
220
     */
221 2
    public function setStatusToFailedDelivery(\Iris\Transfer\Tracking\FailedDelivery $status)
222
    {
223 2
        $this->validateTransfer($status);
224
225 1
        return $this->getVentureApiClient()->setStatusToFailedDelivery($status);
226
    }
227
228
    /**
229
     * {@inheritdoc}
230
     */
231 1
    public function getName()
232
    {
233 1
        return 'venture';
234
    }
235
236
    /**
237
     * @param \Iris\Transfer\Tracking\AbstractTracking $status
238
     * @throws InvalidTransfer
239
     */
240 8
    private function validateTransfer(\Iris\Transfer\Tracking\AbstractTracking $status)
241
    {
242 8
        if (!$status->isValid()) {
243 4
            throw new InvalidTransfer('Invalid transfer data - ' . implode(', ', $status->getErrors()));
244
        }
245 4
    }
246
}
247