Failed Conditions
Push — master ( f11c87...92f16b )
by Łukasz
02:49 queued 02:41
created

CartViewFactory::create()   C

Complexity

Conditions 8
Paths 96

Size

Total Lines 45
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 25
nc 96
nop 2
1
<?php
2
3
namespace Sylius\ShopApiPlugin\Factory;
4
5
use Sylius\Component\Core\Model\AdjustmentInterface;
6
use Sylius\Component\Core\Model\OrderInterface;
7
use Sylius\Component\Core\Model\OrderItemInterface;
8
use Sylius\ShopApiPlugin\View\AdjustmentView;
9
use Sylius\ShopApiPlugin\View\CartSummaryView;
10
11
final class CartViewFactory implements CartViewFactoryInterface
12
{
13
    /** @var CartItemViewFactoryInterface */
14
    private $cartItemFactory;
15
16
    /** @var AddressViewFactoryInterface */
17
    private $addressViewFactory;
18
19
    /** @var TotalViewFactoryInterface */
20
    private $totalViewFactory;
21
22
    /** @var ShipmentViewFactoryInterface */
23
    private $shipmentViewFactory;
24
25
    /** @var PaymentViewFactoryInterface */
26
    private $paymentViewFactory;
27
28
    /** @var AdjustmentViewFactoryInterface */
29
    private $adjustmentViewFactory;
30
31
    public function __construct(
32
        CartItemViewFactoryInterface $cartItemFactory,
33
        AddressViewFactoryInterface $addressViewFactory,
34
        TotalViewFactoryInterface $totalViewFactory,
35
        ShipmentViewFactoryInterface $shipmentViewFactory,
36
        PaymentViewFactoryInterface $paymentViewFactory,
37
        AdjustmentViewFactoryInterface $adjustmentViewFactory
38
    ) {
39
        $this->cartItemFactory = $cartItemFactory;
40
        $this->addressViewFactory = $addressViewFactory;
41
        $this->totalViewFactory = $totalViewFactory;
42
        $this->shipmentViewFactory = $shipmentViewFactory;
43
        $this->paymentViewFactory = $paymentViewFactory;
44
        $this->adjustmentViewFactory = $adjustmentViewFactory;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function create(OrderInterface $cart, string $localeCode): CartSummaryView
51
    {
52
        $cartView = new CartSummaryView();
53
        $cartView->channel = $cart->getChannel()->getCode();
54
        $cartView->currency = $cart->getCurrencyCode();
55
        $cartView->locale = $localeCode;
56
        $cartView->checkoutState = $cart->getCheckoutState();
57
        $cartView->tokenValue = $cart->getTokenValue();
58
        $cartView->totals = $this->totalViewFactory->create($cart);
59
60
        /** @var OrderItemInterface $item */
61
        foreach ($cart->getItems() as $item) {
62
            $cartView->items[] = $this->cartItemFactory->create($item, $cart->getChannel(), $localeCode);
0 ignored issues
show
Compatibility introduced by
$cart->getChannel() of type object<Sylius\Component\...Model\ChannelInterface> is not a sub-type of object<Sylius\Component\...Model\ChannelInterface>. It seems like you assume a child interface of the interface Sylius\Component\Channel\Model\ChannelInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
63
        }
64
65
        foreach ($cart->getShipments() as $shipment) {
66
            $cartView->shipments[] = $this->shipmentViewFactory->create($shipment, $localeCode);
67
        }
68
69
        foreach ($cart->getPayments() as $payment) {
70
            $cartView->payments[] = $this->paymentViewFactory->create($payment, $localeCode);
71
        }
72
73
        /** @var AdjustmentView[] $cartDiscounts */
74
        $cartDiscounts = [];
75
        /** @var AdjustmentInterface $adjustment */
76
        foreach ($cart->getAdjustmentsRecursively(AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT) as $adjustment) {
77
            $originCode = $adjustment->getOriginCode();
78
            $additionalAmount = isset($cartDiscounts[$originCode]) ? $cartDiscounts[$originCode]->amount->current : 0;
79
80
            $cartDiscounts[$originCode] = $this->adjustmentViewFactory->create($adjustment, $additionalAmount);
81
        }
82
83
        $cartView->cartDiscounts = $cartDiscounts;
84
85
        if (null !== $cart->getShippingAddress()) {
86
            $cartView->shippingAddress = $this->addressViewFactory->create($cart->getShippingAddress());
87
        }
88
89
        if (null !== $cart->getBillingAddress()) {
90
            $cartView->billingAddress = $this->addressViewFactory->create($cart->getBillingAddress());
91
        }
92
93
        return $cartView;
94
    }
95
}
96