Passed
Pull Request — master (#1)
by Igor
05:32
created

OrderViewFactory::create()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 50
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 31
nc 16
nop 1
dl 0
loc 50
rs 9.1128
c 1
b 0
f 0
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 Webmozart\Assert\Assert;
15
16
class OrderViewFactory implements OrderViewFactoryInterface
17
{
18
    /** @var CustomerViewFactoryInterface */
19
    protected $customerViewFactory;
20
21
    /** @var AddressViewFactoryInterface */
22
    protected $addressViewFactory;
23
24
    /** @var ShipmentViewFactoryInterface */
25
    protected $shipmentViewFactory;
26
27
    /** @var PaymentViewFactoryInterface */
28
    protected $paymentViewFactory;
29
30
    /** @var string */
31
    protected $orderViewClass;
32
33
    public function __construct(
34
        CustomerViewFactoryInterface $customerViewFactory,
35
        AddressViewFactoryInterface $addressViewFactory,
36
        ShipmentViewFactoryInterface $shipmentViewFactory,
37
        PaymentViewFactoryInterface $paymentViewFactory,
38
        string $orderViewClass
39
    ) {
40
        $this->customerViewFactory = $customerViewFactory;
41
        $this->addressViewFactory = $addressViewFactory;
42
        $this->shipmentViewFactory = $shipmentViewFactory;
43
        $this->paymentViewFactory = $paymentViewFactory;
44
        $this->orderViewClass = $orderViewClass;
45
    }
46
47
    public function create(OrderInterface $order): OrderView
48
    {
49
        $channel = $order->getChannel();
50
        Assert::notNull($channel);
51
52
        $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

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