Failed Conditions
Pull Request — master (#175)
by Łukasz
03:07
created

CartViewFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 9
dl 0
loc 84
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
C create() 0 44 8
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
        $cartDiscounts = [];
73
        /** @var AdjustmentInterface $adjustment */
74
        foreach ($cart->getAdjustmentsRecursively(AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT) as $adjustment) {
75
            $originCode = $adjustment->getOriginCode();
76
            $additionalAmount = isset($cartDiscounts[$originCode]) ? $cartDiscounts[$originCode]->amount->current : 0;
77
78
            $cartDiscounts[$originCode] = $this->adjustmentViewFactory->create($adjustment, $additionalAmount);
79
        }
80
81
        $cartView->cartDiscounts = $cartDiscounts;
82
83
        if (null !== $cart->getShippingAddress()) {
84
            $cartView->shippingAddress = $this->addressViewFactory->create($cart->getShippingAddress());
85
        }
86
87
        if (null !== $cart->getBillingAddress()) {
88
            $cartView->billingAddress = $this->addressViewFactory->create($cart->getBillingAddress());
89
        }
90
91
        return $cartView;
92
    }
93
}
94