Passed
Pull Request — master (#1)
by Igor
04:39
created

OrderViewFactory::create()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 52
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 31
c 1
b 0
f 0
nc 16
nop 1
dl 0
loc 52
rs 9.1128

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\CustomerInterface;
13
use Sylius\Component\Core\Model\OrderInterface;
14
use Sylius\Component\Core\Model\PaymentInterface;
15
use Sylius\Component\Core\Model\ShipmentInterface;
16
use Webmozart\Assert\Assert;
17
18
class OrderViewFactory implements OrderViewFactoryInterface
19
{
20
    /** @var CustomerViewFactoryInterface */
21
    protected $customerViewFactory;
22
23
    /** @var AddressViewFactoryInterface */
24
    protected $addressViewFactory;
25
26
    /** @var ShipmentViewFactoryInterface */
27
    protected $shipmentViewFactory;
28
29
    /** @var PaymentViewFactoryInterface */
30
    protected $paymentViewFactory;
31
32
    /** @var string */
33
    protected $orderViewClass;
34
35
    public function __construct(
36
        CustomerViewFactoryInterface $customerViewFactory,
37
        AddressViewFactoryInterface $addressViewFactory,
38
        ShipmentViewFactoryInterface $shipmentViewFactory,
39
        PaymentViewFactoryInterface $paymentViewFactory,
40
        string $orderViewClass
41
    ) {
42
        $this->customerViewFactory = $customerViewFactory;
43
        $this->addressViewFactory = $addressViewFactory;
44
        $this->shipmentViewFactory = $shipmentViewFactory;
45
        $this->paymentViewFactory = $paymentViewFactory;
46
        $this->orderViewClass = $orderViewClass;
47
    }
48
49
    public function create(OrderInterface $order): OrderView
50
    {
51
        $channel = $order->getChannel();
52
        Assert::notNull($channel);
53
54
        $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

54
        /** @scrutinizer ignore-call */ 
55
        $locale = $channel->getDefaultLocale();
Loading history...
55
        Assert::notNull($locale);
56
57
        $localeCode = $locale->getCode();
58
        Assert::notNull($localeCode);
59
60
        $checkoutCompletedAt = $order->getCheckoutCompletedAt();
61
        Assert::notNull($checkoutCompletedAt);
62
63
        /** @var CustomerInterface|null $customer */
64
        $customer = $order->getCustomer();
65
        Assert::notNull($customer);
66
67
        /** @var OrderView $orderView */
68
        $orderView = new $this->orderViewClass();
69
        $orderView->id = $order->getId();
70
        $orderView->number = $order->getNumber();
71
        $orderView->channel = $channel->getCode();
72
        $orderView->currencyCode = $order->getCurrencyCode();
73
        $orderView->localeCode = $localeCode;
74
        $orderView->state = $order->getState();
75
        $orderView->checkoutState = $order->getCheckoutState();
76
        $orderView->checkoutCompletedAt = $checkoutCompletedAt->format('c');
77
        $orderView->paymentState = $order->getPaymentState();
78
79
        /** @var ShipmentInterface $shipment */
80
        foreach ($order->getShipments() as $shipment) {
81
            $orderView->shipments[] = $this->shipmentViewFactory->create($shipment, $localeCode);
82
        }
83
84
        /** @var PaymentInterface $payment */
85
        foreach ($order->getPayments() as $payment) {
86
            $orderView->payments[] = $this->paymentViewFactory->create($payment, $localeCode);
87
        }
88
89
        if (null !== $order->getShippingAddress()) {
90
            $orderView->shippingAddress = $this->addressViewFactory->create($order->getShippingAddress());
91
        }
92
93
        if (null !== $order->getBillingAddress()) {
94
            $orderView->billingAddress = $this->addressViewFactory->create($order->getBillingAddress());
95
        }
96
97
        $orderView->total = $order->getTotal();
98
        $orderView->customer = $this->customerViewFactory->create($customer);
99
100
        return $orderView;
101
    }
102
}
103