Failed Conditions
Pull Request — master (#175)
by Łukasz
04:12
created

CartViewFactory::create()   C

Complexity

Conditions 7
Paths 64

Size

Total Lines 43
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 24
nc 64
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\CartSummaryView;
9
10
final class CartViewFactory implements CartViewFactoryInterface
11
{
12
    /** @var CartItemViewFactoryInterface */
13
    private $cartItemFactory;
14
15
    /** @var AddressViewFactoryInterface */
16
    private $addressViewFactory;
17
18
    /** @var TotalViewFactoryInterface */
19
    private $totalViewFactory;
20
21
    /** @var ShipmentViewFactoryInterface */
22
    private $shipmentViewFactory;
23
24
    /** @var PaymentViewFactoryInterface */
25
    private $paymentViewFactory;
26
27
    /** @var AdjustmentViewFactoryInterface */
28
    private $adjustmentViewFactory;
29
30
    public function __construct(
31
        CartItemViewFactoryInterface $cartItemFactory,
32
        AddressViewFactoryInterface $addressViewFactory,
33
        TotalViewFactoryInterface $totalViewFactory,
34
        ShipmentViewFactoryInterface $shipmentViewFactory,
35
        PaymentViewFactoryInterface $paymentViewFactory,
36
        AdjustmentViewFactoryInterface $adjustmentViewFactory
37
    ) {
38
        $this->cartItemFactory = $cartItemFactory;
39
        $this->addressViewFactory = $addressViewFactory;
40
        $this->totalViewFactory = $totalViewFactory;
41
        $this->shipmentViewFactory = $shipmentViewFactory;
42
        $this->paymentViewFactory = $paymentViewFactory;
43
        $this->adjustmentViewFactory = $adjustmentViewFactory;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function create(OrderInterface $cart, string $localeCode): CartSummaryView
50
    {
51
        $cartView = new CartSummaryView();
52
        $cartView->channel = $cart->getChannel()->getCode();
53
        $cartView->currency = $cart->getCurrencyCode();
54
        $cartView->locale = $localeCode;
55
        $cartView->checkoutState = $cart->getCheckoutState();
56
        $cartView->tokenValue = $cart->getTokenValue();
57
        $cartView->totals = $this->totalViewFactory->create($cart);
58
59
        /** @var OrderItemInterface $item */
60
        foreach ($cart->getItems() as $item) {
61
            $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...
62
        }
63
64
        foreach ($cart->getShipments() as $shipment) {
65
            $cartView->shipments[] = $this->shipmentViewFactory->create($shipment, $localeCode);
66
        }
67
68
        foreach ($cart->getPayments() as $payment) {
69
            $cartView->payments[] = $this->paymentViewFactory->create($payment, $localeCode);
70
        }
71
72
        $discounts = [];
73
        /** @var AdjustmentInterface $adjustment */
74
        foreach ($cart->getAdjustmentsRecursively(AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT) as $adjustment) {
75
            $originCode = $adjustment->getOriginCode();
76
77
            $discounts[$originCode] = $this->adjustmentViewFactory->create($adjustment, $discounts[$originCode] ?? null);
78
        }
79
80
        $cartView->discounts = $discounts;
81
82
        if (null !== $cart->getShippingAddress()) {
83
            $cartView->shippingAddress = $this->addressViewFactory->create($cart->getShippingAddress());
84
        }
85
86
        if (null !== $cart->getBillingAddress()) {
87
            $cartView->billingAddress = $this->addressViewFactory->create($cart->getBillingAddress());
88
        }
89
90
        return $cartView;
91
    }
92
}
93