OrderService   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 236
Duplicated Lines 26.69 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 6
dl 63
loc 236
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 17 17 2
A edit() 17 17 2
B read() 0 24 4
A delete() 0 7 1
B generateBody() 0 24 3
A addCustomer() 0 17 1
A addAddress() 14 14 1
A addCustomerAddress() 15 15 1
A addOrderLines() 0 15 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/*
3
 * This file is part of the MailChimpEcommerceBundle package.
4
 *
5
 * Copyright (c) 2017 kevin92dev.es
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * Feel free to edit as you please, and have fun.
11
 *
12
 * @author Kevin Murillo <[email protected]>
13
 */
14
15
namespace Kevin92dev\MailChimpEcommerceBundle\Services;
16
17
use Kevin92dev\MailChimpEcommerceBundle\Entities\Address;
18
use Kevin92dev\MailChimpEcommerceBundle\Entities\Customer;
19
use Kevin92dev\MailChimpEcommerceBundle\Entities\Order;
20
use Kevin92dev\MailChimpEcommerceBundle\Entities\OrderLine;
21
use Kevin92dev\MailChimpEcommerceBundle\Exceptions\OrderNotFoundException;
22
use Kevin92dev\MailChimpEcommerceBundle\RequestTypes;
23
24
/**
25
 * OrderService
26
 *
27
 * @author Kevin Murillo <[email protected]>
28
 */
29
class OrderService
30
{
31
    /**
32
     * @var MailChimp
33
     */
34
    private $mailChimp;
35
36
    /**
37
     * @var array
38
     */
39
    private $body;
40
41
    /**
42
     * Initializes OrderService
43
     *
44
     * @param MailChimp $mailChimp
45
     */
46
    public function __construct(MailChimp $mailChimp)
47
    {
48
        $this->mailChimp = $mailChimp;
49
    }
50
51
    /**
52
     * Create a new order in MailChimp
53
     *
54
     * @var Order $order
55
     * @throws \Exception
56
     */
57 View Code Duplication
    public function create(Order $order)
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...
58
    {
59
        $method = RequestTypes::$POST;
60
        $resource = '/orders';
61
62
        $this->generateBody($order);
63
        $this->addCustomer($order);
64
        $this->addAddress($order->getCustomer(), $order->getShippingAddress(), 'shipping_address');
65
        $this->addAddress($order->getCustomer(), $order->getBillingAddress(), 'billing_address');
66
        $this->addOrderLines($order);
67
68
        try {
69
            $this->mailChimp->doRequest($method, $this->body, $resource);
70
        } catch (\Exception $e) {
71
            throw $e;
72
        }
73
    }
74
75
    /**
76
     * Edit an order in MailChimp
77
     *
78
     * @var Order $order
79
     * @throws \Exception
80
     */
81 View Code Duplication
    public function edit(Order $order)
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...
82
    {
83
        $method = RequestTypes::$PATCH;
84
        $resource = '/orders/'.$order->getId();
85
86
        $this->generateBody($order, false);
87
        $this->addCustomer($order);
88
        $this->addAddress($order->getCustomer(), $order->getShippingAddress(), 'shipping_address');
89
        $this->addAddress($order->getCustomer(), $order->getBillingAddress(), 'billing_address');
90
        $this->addOrderLines($order);
91
92
        try {
93
            $this->mailChimp->doRequest($method, $this->body, $resource);
94
        } catch (\Exception $e) {
95
            throw $e;
96
        }
97
    }
98
99
    /**
100
     * Read orders from MailChimp
101
     *
102
     * @param Order $order
103
     * @param int $count
104
     * @param int $offset
105
     *
106
     * @return string
107
     * @throws OrderNotFoundException|\Exception
108
     */
109
    public function read(Order $order = null, $count = 50, $offset = 0)
110
    {
111
        $method = RequestTypes::$GET;
112
113
        if ($order instanceof Order) {
114
            $resource = '/orders/'.$order->getId();
115
        } else {
116
            $resource = '/orders';
117
        }
118
119
        $resource .= '?count='.$count.'&offset='.$offset;
120
121
        $data = [];
122
123
        try {
124
            $request = $this->mailChimp->doRequest($method, $data, $resource);
125
        } catch (OrderNotFoundException $e) {
126
            throw $e;
127
        } catch (\Exception $e) {
128
            throw $e;
129
        }
130
131
        return $request->getBody()->getContents();
132
    }
133
134
    /**
135
     * Delete order from MailChimp
136
     *
137
     * @var Order $order
138
     */
139
    public function delete(Order $order)
140
    {
141
        $method = RequestTypes::$DELETE;
142
        $resource = '/orders/'.$order->getId();
143
144
        $this->mailChimp->doRequest($method, null, $resource);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
145
    }
146
147
    /**
148
     * Generate body array
149
     *
150
     * @param  Order $order
151
     * @param  bool  $addId
152
     */
153
    public function generateBody(Order $order, $addId = true)
154
    {
155
        $this->body = [
156
            'landing_site' => strval($order->getLandingSite()),
157
            'financial_status' => strval($order->getFinancialStatus()),
158
            'fulfillment_status' => strval($order->getFulfillmentStatus()),
159
            'currency_code' => strval($order->getCurrencyCode()),
160
            'order_total' => floatval($order->getOrderTotal()),
161
            'tax_total' => floatval($order->getTaxTotal()),
162
            'shipping_total' => floatval($order->getShippingTotal()),
163
            'tracking_code' => strval($order->getTrackingCode()),
164
            'processed_at_foreign' => strval($order->getProcessedAtForeign()),
165
            'cancelled_at_foreign' => strval($order->getCancelledAtForeign()),
166
            'updated_at_foreign' => strval($order->getUpdatedAtForeign()),
167
        ];
168
169
        if ($addId === true) {
170
            $this->body['id'] = strval($order->getId());
171
        }
172
173
        if (!is_null($order->getCampaignId())) {
174
            $this->body['campaign_id'] = strval($order->getCampaignId());
175
        }
176
    }
177
178
    /**
179
     * Add customer to array
180
     *
181
     * @param Order $order
182
     */
183
    public function addCustomer(Order $order)
184
    {
185
        $customer = $order->getCustomer();
186
187
        $this->body['customer'] = [
188
            'id' => strval($customer->getId()),
189
            'email_address' => strval($customer->getEmail()),
190
            'opt_in_status' => boolval($customer->isOptInStatus()),
191
            'company' => strval($customer->getCompany()),
192
            'first_name' => strval($customer->getFirstname()),
193
            'last_name' => strval($customer->getLastname()),
194
            'orders_count' => intval($customer->getOrdersCount()),
195
            'total_spent' => floatval($customer->getTotalSpent()),
196
        ];
197
198
        $this->addCustomerAddress($customer);
199
    }
200
201
    /**
202
     * Add address to a array
203
     *
204
     * @param Customer $customer
205
     * @param Address  $address
206
     * @param string   $type
207
     */
208 View Code Duplication
    public function addAddress(Customer $customer, Address $address, $type)
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...
209
    {
210
        $this->body[$type] = [
211
            'name' => $type.' address for '.$customer->getEmail(),
212
            'address1' => strval($address->getAddress1()),
213
            'address2' => strval($address->getAddress2()),
214
            'city' => strval($address->getCity()),
215
            'province' => strval($address->getProvince()),
216
            'province_code' => strval($address->getProvinceCode()),
217
            'postal_code' => strval($address->getPostalCode()),
218
            'country' => strval($address->getCountry()),
219
            'country_code' => strval($address->getCountryCode()),
220
        ];
221
    }
222
223
    /**
224
     * Add address to a customer
225
     *
226
     * @param Customer $customer
227
     */
228 View Code Duplication
    public function addCustomerAddress(Customer $customer)
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...
229
    {
230
        $address = $customer->getAddress();
231
232
        $this->body['customer']['address'] = [
233
            'address1' => strval($address->getAddress1()),
234
            'address2' => strval($address->getAddress2()),
235
            'city' => strval($address->getCity()),
236
            'province' => strval($address->getProvince()),
237
            'province_code' => strval($address->getProvinceCode()),
238
            'postal_code' => strval($address->getPostalCode()),
239
            'country' => strval($address->getCountry()),
240
            'country_code' => strval($address->getCountryCode()),
241
        ];
242
    }
243
244
    /**
245
     * Add order lines to array
246
     *
247
     * @param Order $order
248
     */
249
    public function addOrderLines(Order $order)
250
    {
251
        /**
252
         * @var OrderLine $orderLine
253
         */
254
        foreach ($order->getOrderLines() as $orderLine) {
255
            $this->body['lines'][] = [
256
                'id' => strval($orderLine->getId()),
257
                'product_id' => strval($orderLine->getProduct()->getId()),
258
                'product_variant_id' => strval($orderLine->getProductVariant()->getId()),
259
                'quantity' => intval($orderLine->getQuantity()),
260
                'price' => floatval($orderLine->getPrice()),
261
            ];
262
        }
263
    }
264
}
265