Order   C
last analyzed

Complexity

Total Complexity 64

Size/Duplication

Total Lines 434
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 13

Importance

Changes 0
Metric Value
wmc 64
lcom 2
cbo 13
dl 0
loc 434
rs 5.6626
c 0
b 0
f 0

61 Methods

Rating   Name   Duplication   Size   Complexity  
A getProductTotal() 0 4 1
A setProductTotal() 0 4 1
A __construct() 0 17 1
A getClient() 0 4 1
A setClient() 0 4 1
A getClientDetails() 0 4 1
A setClientDetails() 0 4 1
A getContactDetails() 0 4 1
A setContactDetails() 0 4 1
A getBillingAddress() 0 4 1
A setBillingAddress() 0 4 1
A getShippingAddress() 0 4 1
A setShippingAddress() 0 4 1
A isConfirmed() 0 4 1
A setConfirmed() 0 4 1
A getNumber() 0 4 1
A setNumber() 0 4 1
A getCurrency() 0 4 1
A setCurrency() 0 4 1
A getCurrencyRate() 0 4 1
A setCurrencyRate() 0 4 1
A getSessionId() 0 4 1
A setSessionId() 0 4 1
A getCoupon() 0 4 1
A setCoupon() 0 4 1
A getProducts() 0 4 1
A setProducts() 0 4 1
A hasModifier() 0 4 1
A removeModifier() 0 4 1
A getModifier() 0 4 1
A getModifiers() 0 4 1
A getSummary() 0 4 1
A setSummary() 0 4 1
A getCurrentStatus() 0 4 1
A setCurrentStatus() 0 4 1
A getOrderStatusHistory() 0 4 1
A getOrderNotes() 0 4 1
A getComment() 0 4 1
A setComment() 0 4 1
A getPayments() 0 4 1
A acceptVisitor() 0 4 1
A isEmpty() 0 4 1
A getShippingMethodOption() 0 4 1
A setShippingMethodOption() 0 4 1
A isConditionsAccepted() 0 4 1
A setConditionsAccepted() 0 4 1
A isIssueInvoice() 0 4 1
A setIssueInvoice() 0 4 1
A getInvoices() 0 4 1
A getShipments() 0 4 1
A getShop() 0 4 1
A setShop() 0 4 1
A getShippingMethod() 0 4 1
A setShippingMethod() 0 4 1
A getPaymentMethod() 0 4 1
A setPaymentMethod() 0 4 1
A getMinimumOrderAmount() 0 4 1
A setMinimumOrderAmount() 0 4 1
A hasMinimumValue() 0 4 1
A hasMethods() 0 4 2
A isConfirmable() 0 4 3

How to fix   Complexity   

Complex Class

Complex classes like Order often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Order, and based on these observations, apply Extract Interface, too.

1
<?php
2
/*
3
 * WellCommerce Open-Source E-Commerce Platform
4
 *
5
 * This file is part of the WellCommerce package.
6
 *
7
 * (c) Adam Piotrowski <[email protected]>
8
 *
9
 * For the full copyright and license information,
10
 * please view the LICENSE file that was distributed with this source code.
11
 */
12
13
namespace WellCommerce\Bundle\OrderBundle\Entity;
14
15
use Doctrine\Common\Collections\ArrayCollection;
16
use Doctrine\Common\Collections\Collection;
17
use Knp\DoctrineBehaviors\Model\Timestampable\Timestampable;
18
use WellCommerce\Bundle\AppBundle\Entity\Client;
19
use WellCommerce\Bundle\AppBundle\Entity\ClientBillingAddress;
20
use WellCommerce\Bundle\AppBundle\Entity\ClientContactDetails;
21
use WellCommerce\Bundle\AppBundle\Entity\ClientDetails;
22
use WellCommerce\Bundle\AppBundle\Entity\ClientShippingAddress;
23
use WellCommerce\Bundle\AppBundle\Entity\MinimumOrderAmount;
24
use WellCommerce\Bundle\AppBundle\Entity\Shop;
25
use WellCommerce\Bundle\CoreBundle\Doctrine\Behaviours\Identifiable;
26
use WellCommerce\Bundle\CoreBundle\Entity\EntityInterface;
27
use WellCommerce\Bundle\CouponBundle\Entity\Coupon;
28
use WellCommerce\Bundle\OrderBundle\Visitor\OrderVisitorInterface;
29
use WellCommerce\Extra\OrderBundle\Entity\OrderExtraTrait;
30
31
/**
32
 * Class Order
33
 *
34
 * @author  Adam Piotrowski <[email protected]>
35
 */
36
class Order implements EntityInterface
37
{
38
    use Identifiable;
39
    use Timestampable;
40
    use OrderExtraTrait;
41
    
42
    protected $confirmed            = false;
43
    protected $number               = null;
44
    protected $currency             = null;
45
    protected $currencyRate         = 1.0000;
46
    protected $sessionId            = '';
47
    protected $shippingMethodOption = null;
48
    protected $comment              = '';
49
    protected $issueInvoice         = false;
50
    protected $conditionsAccepted   = false;
51
    
52
    /**
53
     * @var null|Client
54
     */
55
    protected $client;
56
    
57
    /**
58
     * @var ClientDetails
59
     */
60
    protected $clientDetails;
61
    
62
    /**
63
     * @var ClientContactDetails
64
     */
65
    protected $contactDetails;
66
    
67
    /**
68
     * @var ClientBillingAddress
69
     */
70
    protected $billingAddress;
71
    
72
    /**
73
     * @var ClientShippingAddress
74
     */
75
    protected $shippingAddress;
76
    
77
    /**
78
     * @var Coupon
79
     */
80
    protected $coupon;
81
    
82
    /**
83
     * @var OrderStatus
84
     */
85
    protected $currentStatus;
86
    
87
    /**
88
     * @var OrderSummary
89
     */
90
    protected $summary;
91
    
92
    /**
93
     * @var OrderProductTotal
94
     */
95
    protected $productTotal;
96
    
97
    /**
98
     * @var MinimumOrderAmount
99
     */
100
    protected $minimumOrderAmount;
101
    
102
    /**
103
     * @var Collection
104
     */
105
    protected $products;
106
    
107
    /**
108
     * @var Collection
109
     */
110
    protected $payments;
111
    
112
    /**
113
     * @var Collection
114
     */
115
    protected $invoices;
116
    
117
    /**
118
     * @var Collection
119
     */
120
    protected $shipments;
121
    
122
    /**
123
     * @var Collection
124
     */
125
    protected $orderStatusHistory;
126
    
127
    /**
128
     * @var Collection
129
     */
130
    protected $orderNotes;
131
    
132
    /**
133
     * @var Collection
134
     */
135
    protected $modifiers;
136
    
137
    /**
138
     * @var Shop
139
     */
140
    protected $shop;
141
    
142
    /**
143
     * @var ShippingMethod
144
     */
145
    protected $shippingMethod;
146
    
147
    /**
148
     * @var PaymentMethod
149
     */
150
    protected $paymentMethod;
151
    
152
    public function __construct()
153
    {
154
        $this->products           = new ArrayCollection();
155
        $this->modifiers          = new ArrayCollection();
156
        $this->payments           = new ArrayCollection();
157
        $this->orderStatusHistory = new ArrayCollection();
158
        $this->orderNotes         = new ArrayCollection();
159
        $this->productTotal       = new OrderProductTotal();
160
        $this->summary            = new OrderSummary();
161
        $this->clientDetails      = new ClientDetails();
162
        $this->contactDetails     = new ClientContactDetails();
163
        $this->billingAddress     = new ClientBillingAddress();
164
        $this->shippingAddress    = new ClientShippingAddress();
165
        $this->minimumOrderAmount = new MinimumOrderAmount();
166
        $this->invoices           = new ArrayCollection();
167
        $this->shipments          = new ArrayCollection();
168
    }
169
    
170
    public function getClient()
171
    {
172
        return $this->client;
173
    }
174
    
175
    public function setClient(Client $client = null)
176
    {
177
        $this->client = $client;
178
    }
179
    
180
    public function getClientDetails(): ClientDetails
181
    {
182
        return $this->clientDetails;
183
    }
184
    
185
    public function setClientDetails(ClientDetails $clientDetails)
186
    {
187
        $this->clientDetails = $clientDetails;
188
    }
189
    
190
    public function getContactDetails(): ClientContactDetails
191
    {
192
        return $this->contactDetails;
193
    }
194
    
195
    public function setContactDetails(ClientContactDetails $contactDetails)
196
    {
197
        $this->contactDetails = $contactDetails;
198
    }
199
    
200
    public function getBillingAddress(): ClientBillingAddress
201
    {
202
        return $this->billingAddress;
203
    }
204
    
205
    public function setBillingAddress(ClientBillingAddress $billingAddress)
206
    {
207
        $this->billingAddress = $billingAddress;
208
    }
209
    
210
    public function getShippingAddress(): ClientShippingAddress
211
    {
212
        return $this->shippingAddress;
213
    }
214
    
215
    public function setShippingAddress(ClientShippingAddress $shippingAddress)
216
    {
217
        $this->shippingAddress = $shippingAddress;
218
    }
219
    
220
    public function isConfirmed(): bool
221
    {
222
        return $this->confirmed;
223
    }
224
    
225
    public function setConfirmed(bool $confirmed)
226
    {
227
        $this->confirmed = $confirmed;
228
    }
229
    
230
    public function getNumber()
231
    {
232
        return $this->number;
233
    }
234
    
235
    public function setNumber(string $number)
236
    {
237
        $this->number = $number;
238
    }
239
    
240
    public function getCurrency(): string
241
    {
242
        return $this->currency;
243
    }
244
    
245
    public function setCurrency(string $currency)
246
    {
247
        $this->currency = $currency;
248
    }
249
    
250
    public function getCurrencyRate(): float
251
    {
252
        return $this->currencyRate;
253
    }
254
    
255
    public function setCurrencyRate(float $currencyRate)
256
    {
257
        $this->currencyRate = $currencyRate;
258
    }
259
    
260
    public function getSessionId(): string
261
    {
262
        return $this->sessionId;
263
    }
264
    
265
    public function setSessionId(string $sessionId)
266
    {
267
        $this->sessionId = $sessionId;
268
    }
269
    
270
    public function getCoupon()
271
    {
272
        return $this->coupon;
273
    }
274
    
275
    public function setCoupon(Coupon $coupon = null)
276
    {
277
        $this->coupon = $coupon;
278
    }
279
    
280
    public function getProducts(): Collection
281
    {
282
        return $this->products;
283
    }
284
    
285
    public function setProducts(Collection $products)
286
    {
287
        $this->products = $products;
288
    }
289
    
290
    public function getProductTotal(): OrderProductTotal
291
    {
292
        return $this->productTotal;
293
    }
294
    
295
    public function setProductTotal(OrderProductTotal $productTotal)
296
    {
297
        $this->productTotal = $productTotal;
298
    }
299
    
300
    public function hasModifier(string $name): bool
301
    {
302
        return $this->modifiers->containsKey($name);
303
    }
304
    
305
    public function removeModifier(string $name)
306
    {
307
        $this->modifiers->remove($name);
308
    }
309
    
310
    public function getModifier(string $name): OrderModifier
311
    {
312
        return $this->modifiers->get($name);
313
    }
314
    
315
    public function getModifiers(): Collection
316
    {
317
        return $this->modifiers;
318
    }
319
    
320
    public function getSummary(): OrderSummary
321
    {
322
        return $this->summary;
323
    }
324
    
325
    public function setSummary(OrderSummary $summary)
326
    {
327
        $this->summary = $summary;
328
    }
329
    
330
    public function getCurrentStatus()
331
    {
332
        return $this->currentStatus;
333
    }
334
    
335
    public function setCurrentStatus(OrderStatus $currentStatus = null)
336
    {
337
        $this->currentStatus = $currentStatus;
338
    }
339
    
340
    public function getOrderStatusHistory(): Collection
341
    {
342
        return $this->orderStatusHistory;
343
    }
344
    
345
    public function getOrderNotes(): Collection
346
    {
347
        return $this->orderNotes;
348
    }
349
    
350
    public function getComment(): string
351
    {
352
        return $this->comment;
353
    }
354
    
355
    public function setComment(string $comment)
356
    {
357
        $this->comment = $comment;
358
    }
359
    
360
    public function getPayments(): Collection
361
    {
362
        return $this->payments;
363
    }
364
    
365
    public function acceptVisitor(OrderVisitorInterface $visitor)
366
    {
367
        $visitor->visitOrder($this);
368
    }
369
    
370
    public function isEmpty(): bool
371
    {
372
        return 0 === $this->productTotal->getQuantity();
373
    }
374
    
375
    public function getShippingMethodOption()
376
    {
377
        return $this->shippingMethodOption;
378
    }
379
    
380
    public function setShippingMethodOption($shippingMethodOption)
381
    {
382
        $this->shippingMethodOption = $shippingMethodOption;
383
    }
384
    
385
    public function isConditionsAccepted(): bool
386
    {
387
        return $this->conditionsAccepted;
388
    }
389
    
390
    public function setConditionsAccepted(bool $conditionsAccepted)
391
    {
392
        $this->conditionsAccepted = $conditionsAccepted;
393
    }
394
    
395
    public function isIssueInvoice(): bool
396
    {
397
        return $this->issueInvoice;
398
    }
399
    
400
    public function setIssueInvoice(bool $issueInvoice)
401
    {
402
        $this->issueInvoice = $issueInvoice;
403
    }
404
    
405
    public function getInvoices(): Collection
406
    {
407
        return $this->invoices;
408
    }
409
    
410
    public function getShipments(): Collection
411
    {
412
        return $this->shipments;
413
    }
414
    
415
    public function getShop(): Shop
416
    {
417
        return $this->shop;
418
    }
419
    
420
    public function setShop(Shop $shop)
421
    {
422
        $this->shop = $shop;
423
    }
424
    
425
    public function getShippingMethod()
426
    {
427
        return $this->shippingMethod;
428
    }
429
    
430
    public function setShippingMethod(ShippingMethod $shippingMethod = null)
431
    {
432
        $this->shippingMethod = $shippingMethod;
433
    }
434
    
435
    public function getPaymentMethod()
436
    {
437
        return $this->paymentMethod;
438
    }
439
    
440
    public function setPaymentMethod(PaymentMethod $paymentMethod = null)
441
    {
442
        $this->paymentMethod = $paymentMethod;
443
    }
444
    
445
    public function getMinimumOrderAmount(): MinimumOrderAmount
446
    {
447
        return $this->minimumOrderAmount;
448
    }
449
    
450
    public function setMinimumOrderAmount(MinimumOrderAmount $minimumOrderAmount)
451
    {
452
        $this->minimumOrderAmount = $minimumOrderAmount;
453
    }
454
    
455
    public function hasMinimumValue(): bool
456
    {
457
        return $this->getSummary()->getGrossAmount() >= $this->minimumOrderAmount->getValue();
458
    }
459
    
460
    public function hasMethods(): bool
461
    {
462
        return $this->shippingMethod instanceof ShippingMethod && $this->paymentMethod instanceof PaymentMethod;
463
    }
464
    
465
    public function isConfirmable(): bool
466
    {
467
        return !$this->isEmpty() && $this->hasMinimumValue() && $this->hasMethods();
468
    }
469
}
470