Completed
Push — master ( 663845...79fb76 )
by Igor
18s queued 13s
created

OrderViewFactory::create()   B

Complexity

Conditions 8
Paths 96

Size

Total Lines 74
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 45
nc 96
nop 1
dl 0
loc 74
rs 7.9555
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Setono\SyliusLagersystemPlugin\Factory\Order;
6
7
use Setono\SyliusLagersystemPlugin\Factory\Address\AddressViewFactoryInterface;
8
use Setono\SyliusLagersystemPlugin\Factory\Customer\CustomerViewFactoryInterface;
9
use Setono\SyliusLagersystemPlugin\Factory\PaymentViewFactoryInterface;
10
use Setono\SyliusLagersystemPlugin\Factory\ShipmentViewFactoryInterface;
11
use Setono\SyliusLagersystemPlugin\View\Order\OrderView;
12
use Sylius\Component\Core\Model\AdjustmentInterface;
13
use Sylius\Component\Core\Model\CustomerInterface;
14
use Sylius\Component\Core\Model\OrderInterface;
15
use Sylius\Component\Core\Model\OrderItemInterface;
16
use Sylius\Component\Core\Model\PaymentInterface;
17
use Sylius\Component\Core\Model\ShipmentInterface;
18
use Webmozart\Assert\Assert;
19
20
class OrderViewFactory implements OrderViewFactoryInterface
21
{
22
    /** @var CustomerViewFactoryInterface */
23
    protected $customerViewFactory;
24
25
    /** @var AddressViewFactoryInterface */
26
    protected $addressViewFactory;
27
28
    /** @var ShipmentViewFactoryInterface */
29
    protected $shipmentViewFactory;
30
31
    /** @var PaymentViewFactoryInterface */
32
    protected $paymentViewFactory;
33
34
    /** @var AdjustmentViewFactoryInterface */
35
    protected $adjustmentViewFactory;
36
37
    /** @var ItemViewFactoryInterface */
38
    protected $itemFactory;
39
40
    /** @var string */
41
    protected $orderViewClass;
42
43
    public function __construct(
44
        CustomerViewFactoryInterface $customerViewFactory,
45
        AddressViewFactoryInterface $addressViewFactory,
46
        ShipmentViewFactoryInterface $shipmentViewFactory,
47
        PaymentViewFactoryInterface $paymentViewFactory,
48
        AdjustmentViewFactoryInterface $adjustmentViewFactory,
49
        ItemViewFactoryInterface $itemFactory,
50
        string $orderViewClass
51
    ) {
52
        $this->customerViewFactory = $customerViewFactory;
53
        $this->addressViewFactory = $addressViewFactory;
54
        $this->shipmentViewFactory = $shipmentViewFactory;
55
        $this->paymentViewFactory = $paymentViewFactory;
56
        $this->adjustmentViewFactory = $adjustmentViewFactory;
57
        $this->itemFactory = $itemFactory;
58
        $this->orderViewClass = $orderViewClass;
59
    }
60
61
    public function create(OrderInterface $order): OrderView
62
    {
63
        $channel = $order->getChannel();
64
        Assert::notNull($channel);
65
66
        $locale = $channel->getDefaultLocale();
0 ignored issues
show
Bug introduced by
The method getDefaultLocale() does not exist on Sylius\Component\Channel\Model\ChannelInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Sylius\Component\Channel\Model\Channel. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
        /** @scrutinizer ignore-call */ 
67
        $locale = $channel->getDefaultLocale();
Loading history...
67
        Assert::notNull($locale);
68
69
        $localeCode = $locale->getCode();
70
        Assert::notNull($localeCode);
71
72
        $checkoutCompletedAt = $order->getCheckoutCompletedAt();
73
        Assert::notNull($checkoutCompletedAt);
74
75
        /** @var CustomerInterface|null $customer */
76
        $customer = $order->getCustomer();
77
        Assert::notNull($customer);
78
79
        /** @var OrderView $orderView */
80
        $orderView = new $this->orderViewClass();
81
        $orderView->id = $order->getId();
82
        $orderView->number = $order->getNumber();
83
        $orderView->channel = $channel->getCode();
84
        $orderView->currencyCode = $order->getCurrencyCode();
85
        $orderView->localeCode = $localeCode;
86
        $orderView->state = $order->getState();
87
        $orderView->checkoutState = $order->getCheckoutState();
88
        $orderView->checkoutCompletedAt = $checkoutCompletedAt->format('c');
89
        $orderView->shippingState = $order->getShippingState();
90
        $orderView->paymentState = $order->getPaymentState();
91
92
        /** @var OrderItemInterface $item */
93
        foreach ($order->getItems() as $item) {
94
            $orderView->items[] = $this->itemFactory->create(
95
                $item,
96
                $localeCode
97
            );
98
        }
99
100
        /** @var ShipmentInterface $shipment */
101
        foreach ($order->getShipments() as $shipment) {
102
            $orderView->shipments[] = $this->shipmentViewFactory->create($shipment, $localeCode);
103
        }
104
105
        /** @var PaymentInterface $payment */
106
        foreach ($order->getPayments() as $payment) {
107
            $orderView->payments[] = $this->paymentViewFactory->create($payment, $localeCode);
108
        }
109
110
        if (null !== $order->getShippingAddress()) {
111
            $orderView->shippingAddress = $this->addressViewFactory->create($order->getShippingAddress());
112
        }
113
114
        if (null !== $order->getBillingAddress()) {
115
            $orderView->billingAddress = $this->addressViewFactory->create($order->getBillingAddress());
116
        }
117
118
        $adjustments = [];
119
        /** @var AdjustmentInterface $adjustment */
120
        foreach ($order->getAdjustmentsRecursively() as $adjustment) {
121
            $originCode = $adjustment->getOriginCode();
122
            $additionalAmount = isset($adjustments[$originCode]) ? $adjustments[$originCode]->amount : 0;
123
124
            $adjustments[$originCode] = $this->adjustmentViewFactory->create(
125
                $adjustment,
126
                $additionalAmount
127
            );
128
        }
129
        $orderView->adjustments = $adjustments;
130
        $orderView->adjustmentsTotal = $order->getAdjustmentsTotalRecursively();
131
        $orderView->total = $order->getTotal();
132
        $orderView->customer = $this->customerViewFactory->create($customer);
133
134
        return $orderView;
135
    }
136
}
137