Completed
Pull Request — master (#290)
by
unknown
41:51
created

CartViewFactory::create()   B

Complexity

Conditions 8
Paths 96

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 7.9337
c 0
b 0
f 0
cc 8
nc 96
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sylius\SyliusShopApiPlugin\Factory;
6
7
use Sylius\Component\Core\Model\AdjustmentInterface;
8
use Sylius\Component\Core\Model\OrderInterface;
9
use Sylius\Component\Core\Model\OrderItemInterface;
10
use Sylius\ShopApiPlugin\View\AdjustmentView;
11
use Sylius\ShopApiPlugin\View\CartSummaryView;
12
13
final class CartViewFactory implements CartViewFactoryInterface
14
{
15
    /** @var CartItemViewFactoryInterface */
16
    private $cartItemFactory;
17
18
    /** @var AddressViewFactoryInterface */
19
    private $addressViewFactory;
20
21
    /** @var TotalViewFactoryInterface */
22
    private $totalViewFactory;
23
24
    /** @var ShipmentViewFactoryInterface */
25
    private $shipmentViewFactory;
26
27
    /** @var PaymentViewFactoryInterface */
28
    private $paymentViewFactory;
29
30
    /** @var AdjustmentViewFactoryInterface */
31
    private $adjustmentViewFactory;
32
33
    /** @var string */
34
    private $cartSummaryViewClass;
35
36
    public function __construct(
37
        CartItemViewFactoryInterface $cartItemFactory,
38
        AddressViewFactoryInterface $addressViewFactory,
39
        TotalViewFactoryInterface $totalViewFactory,
40
        ShipmentViewFactoryInterface $shipmentViewFactory,
41
        PaymentViewFactoryInterface $paymentViewFactory,
42
        AdjustmentViewFactoryInterface $adjustmentViewFactory,
43
        string $cartSummaryViewClass
44
    ) {
45
        $this->cartItemFactory = $cartItemFactory;
46
        $this->addressViewFactory = $addressViewFactory;
47
        $this->totalViewFactory = $totalViewFactory;
48
        $this->shipmentViewFactory = $shipmentViewFactory;
49
        $this->paymentViewFactory = $paymentViewFactory;
50
        $this->adjustmentViewFactory = $adjustmentViewFactory;
51
        $this->cartSummaryViewClass = $cartSummaryViewClass;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function create(OrderInterface $cart, string $localeCode): CartSummaryView
58
    {
59
        /** @var CartSummaryView $cartView */
60
        $cartView = new $this->cartSummaryViewClass();
61
        $cartView->channel = $cart->getChannel()->getCode();
62
        $cartView->currency = $cart->getCurrencyCode();
63
        $cartView->locale = $localeCode;
64
        $cartView->checkoutState = $cart->getCheckoutState();
65
        $cartView->tokenValue = $cart->getTokenValue();
66
        $cartView->totals = $this->totalViewFactory->create($cart);
67
68
        /** @var OrderItemInterface $item */
69
        foreach ($cart->getItems() as $item) {
70
            $cartView->items[] = $this->cartItemFactory->create($item, $cart->getChannel(), $localeCode);
0 ignored issues
show
Bug introduced by
It seems like $cart->getChannel() can be null; however, create() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
71
        }
72
73
        foreach ($cart->getShipments() as $shipment) {
74
            $cartView->shipments[] = $this->shipmentViewFactory->create($shipment, $localeCode);
75
        }
76
77
        foreach ($cart->getPayments() as $payment) {
78
            $cartView->payments[] = $this->paymentViewFactory->create($payment, $localeCode);
79
        }
80
81
        /** @var AdjustmentView[] $cartDiscounts */
82
        $cartDiscounts = [];
83
        /** @var AdjustmentInterface $adjustment */
84
        foreach ($cart->getAdjustmentsRecursively(AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT) as $adjustment) {
85
            $originCode = $adjustment->getOriginCode();
86
            $additionalAmount = isset($cartDiscounts[$originCode]) ? $cartDiscounts[$originCode]->amount->current : 0;
87
88
            $cartDiscounts[$originCode] = $this->adjustmentViewFactory->create($adjustment, $additionalAmount, $cart->getCurrencyCode());
89
        }
90
91
        $cartView->cartDiscounts = $cartDiscounts;
92
93
        if (null !== $cart->getShippingAddress()) {
94
            $cartView->shippingAddress = $this->addressViewFactory->create($cart->getShippingAddress());
95
        }
96
97
        if (null !== $cart->getBillingAddress()) {
98
            $cartView->billingAddress = $this->addressViewFactory->create($cart->getBillingAddress());
99
        }
100
101
        return $cartView;
102
    }
103
}
104