OrderService::validateOrder()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 12
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/*
4
 * This file is part of PHP CS Fixer.
5
 *
6
 * (c) Fabien Potencier <[email protected]>
7
 *     Dariusz Rumiński <[email protected]>
8
 *
9
 * This source file is subject to the MIT license that is bundled
10
 * with this source code in the file LICENSE.
11
 */
12
13
namespace Etrias\PaazlConnector\Services;
14
15
use DateTime;
16
use Etrias\PaazlConnector\Client\PaazlClientInterface;
17
use Etrias\PaazlConnector\Exceptions\ReferenceAlreadyExistsException;
18
use Etrias\PaazlConnector\SoapTypes\ChangeOrderRequest;
19
use Etrias\PaazlConnector\SoapTypes\ChangeProducts;
20
use Etrias\PaazlConnector\SoapTypes\ChangeSenderAddress;
21
use Etrias\PaazlConnector\SoapTypes\ChangeShippingAddress;
22
use Etrias\PaazlConnector\SoapTypes\ChangeShippingMethod;
23
use Etrias\PaazlConnector\SoapTypes\CommitOrderRequest;
24
use Etrias\PaazlConnector\SoapTypes\DeleteOrderRequest;
25
use Etrias\PaazlConnector\SoapTypes\ListOrdersRequest;
26
use Etrias\PaazlConnector\SoapTypes\OrderDetailsRequest;
27
use Etrias\PaazlConnector\SoapTypes\OrderRequest;
28
use Etrias\PaazlConnector\SoapTypes\OrderStatusRequest;
29
use Etrias\PaazlConnector\SoapTypes\SenderAddress;
30
use Etrias\PaazlConnector\SoapTypes\ShippingAddress;
31
use Etrias\PaazlConnector\SoapTypes\ShippingMethod;
32
use Etrias\PaazlConnector\SoapTypes\UpdateOrderRequest;
33
use Etrias\PaazlConnector\SoapTypes\ValidateOrderRequest;
34
35
class OrderService implements OrderServiceInterface
36
{
37
    /**
38
     * @var PaazlClientInterface
39
     */
40
    protected $client;
41
    /**
42
     * @var SecurityServiceInterface
43
     */
44
    protected $security;
45
46
    /**
47
     * DocumentService constructor.
48
     *
49
     * @param PaazlClientInterface     $client
50
     * @param SecurityServiceInterface $security
51
     */
52
    public function __construct(PaazlClientInterface $client, SecurityServiceInterface $security)
53
    {
54
        $this->security = $security;
55
        $this->client = $client;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function getOrderDetails($orderReference, $targetWebShop = null, $extendedDetails = null)
62
    {
63
        $request = new OrderDetailsRequest(
64
            $this->security->getHash($orderReference),
65
            $this->client->getWebShopId(),
66
            $targetWebShop,
67
            $orderReference,
68
            $extendedDetails
69
        );
70
71
        return $this->client->orderDetails($request);
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function createOrder($orderReference, array $products, $override = false, $targetWebShop = null)
78
    {
79
        $request = new OrderRequest(
80
            $this->security->getHash($orderReference),
81
            $this->client->getWebShopId(),
82
            $targetWebShop,
83
            $orderReference,
84
            $products
85
        );
86
87
        if ($override === true) {
88
            try {
89
                return $this->client->order($request);
90
            } catch (ReferenceAlreadyExistsException $e) {
91
                return $this->updateOrder($orderReference, $products, $targetWebShop);
92
            }
93
        } else {
94
            return $this->client->order($request);
95
        }
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function updateOrder($orderReference, array $products, $targetWebShop = null)
102
    {
103
        $request = new UpdateOrderRequest(
104
            $this->security->getHash($orderReference),
105
            $this->client->getWebShopId(),
106
            $targetWebShop,
107
            $orderReference,
108
            $products
109
        );
110
111
        return $this->client->updateorder($request);
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function changeOrder(
118
        $orderReference,
119
        $newOrderReference = null,
120
        ChangeShippingMethod $shippingMethod = null,
121
        ChangeShippingAddress $shippingAddress = null,
122
        ChangeSenderAddress $returnAddress = null,
123
        ChangeSenderAddress $shipperAddress = null,
124
        ChangeProducts $products = null,
125
        $totalAmount = null,
126
        $totalAmountCurrency = null,
127
        $language = null,
128
        $customerEmail = null,
129
        $customerPhoneNumber = null,
130
        $targetWebShop = null
131
    ) {
132
        $request = new ChangeOrderRequest(
133
            $this->security->getHash($orderReference),
134
            $this->client->getWebShopId(),
135
            $targetWebShop,
136
            $orderReference,
137
            $newOrderReference,
138
            $totalAmount,
139
            $totalAmountCurrency,
140
            $language,
141
            $customerEmail,
142
            $customerPhoneNumber,
143
            $shippingMethod,
144
            $shipperAddress,
145
            $returnAddress,
146
            $shippingAddress,
147
            $products
148
        );
149
150
        return $this->client->changeOrder($request);
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156
    public function validateOrder(
157
        $orderReference,
158
        $pendingOrderReference,
159
        $totalAmount,
160
        $totalAmountCurrency,
161
        ShippingMethod $shippingMethod,
162
        ShippingAddress $shippingAddress,
163
        $customerEmail = null,
164
        $language = null,
165
        $customerPhoneNumber = null,
166
        SenderAddress $shipperAddress = null,
167
        SenderAddress $returnAddress = null,
168
        $targetWebShop = null
169
    ) {
170
        $request = new ValidateOrderRequest(
171
            $this->security->getHash($pendingOrderReference),
172
            $this->client->getWebShopId(),
173
            $targetWebShop,
174
            $orderReference,
175
            $pendingOrderReference,
176
            $totalAmount,
177
            $totalAmountCurrency,
178
            $language,
179
            $customerEmail,
180
            $customerPhoneNumber,
181
            $shippingMethod,
182
            $shipperAddress,
183
            $returnAddress,
184
            $shippingAddress
185
        );
186
187
        return $this->client->validateOrder($request);
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193
    public function commitOrder(
194
        $orderReference,
195
        ShippingMethod $shippingMethod,
196
        ShippingAddress $shippingAddress,
197
        $totalAmount = null,
198
        $totalAmountCurrency = null,
199
        $language = null,
200
        $customerEmail = null,
201
        $customerPhoneNumber = null,
202
        SenderAddress $returnAddress = null,
203
        SenderAddress $shipperAddress = null,
204
        $pendingOrderReference = null,
205
        $targetWebShop = null
206
    ) {
207
        if ($pendingOrderReference === null) {
208
            $pendingOrderReference = $orderReference;
209
        }
210
211
        $request = new CommitOrderRequest(
212
            $this->security->getHash($pendingOrderReference),
213
            $this->client->getWebShopId(),
214
            $targetWebShop,
215
            $orderReference,
216
            $pendingOrderReference,
217
            $totalAmount,
218
            $totalAmountCurrency,
219
            $language,
220
            $customerEmail,
221
            $customerPhoneNumber,
222
            $shippingMethod,
223
            $shipperAddress,
224
            $returnAddress,
225
            $shippingAddress
226
        );
227
228
        return $this->client->commitOrder($request);
229
    }
230
231
    /**
232
     * {@inheritdoc}
233
     */
234
    public function deleteOrder($orderReference, $targetWebShop = null)
235
    {
236
        $request = new DeleteOrderRequest(
237
            $this->security->getHash($orderReference),
238
            $this->client->getWebShopId(),
239
            $targetWebShop,
240
            $orderReference
241
        );
242
243
        return $this->client->deleteOrder($request);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->client->deleteOrder($request) returns the type Etrias\PaazlConnector\So...pes\DeleteOrderResponse which is incompatible with the return type mandated by Etrias\PaazlConnector\Se...nterface::deleteOrder() of Etrias\PaazlConnector\Services\DeleteOrderResponse.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
244
    }
245
246
    /**
247
     * {@inheritdoc}
248
     */
249
    public function getOrderStatus(
250
        $orderReference,
251
        $includeLabels = null,
252
        $getCarrierStatus = null,
253
        $targetWebShop = null
254
    ) {
255
        $request = new OrderStatusRequest(
256
            $this->security->getHash($orderReference),
257
            $this->client->getWebShopId(),
258
            $targetWebShop,
259
            $orderReference,
260
            $includeLabels,
261
            $getCarrierStatus
262
        );
263
264
        return $this->client->orderStatus($request);
265
    }
266
267
    /**
268
     * {@inheritdoc}
269
     */
270 View Code Duplication
    public function listOrders(DateTime $changedSince, $page = null, $carrierStatus = null, $targetWebShop = null)
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...
271
    {
272
        $today = new DateTime();
273
        $request = new ListOrdersRequest(
274
            $this->security->getHash($today->format('Ymd')),
275
            $this->client->getWebShopId(),
276
            $targetWebShop,
277
            $changedSince,
278
            $page,
279
            $carrierStatus
280
        );
281
282
        return $this->client->listOrders($request);
283
    }
284
}
285