Completed
Push — master ( 8d10db...a4c05f )
by Łukasz
10s
created

CartViewFactory::create()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 37
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 25
nc 8
nop 2
1
<?php
2
3
namespace Sylius\ShopApiPlugin\Factory;
4
5
use Sylius\Component\Core\Model\OrderInterface;
6
use Sylius\Component\Core\Model\OrderItemInterface;
7
use Sylius\ShopApiPlugin\View\CartSummaryView;
8
use Sylius\ShopApiPlugin\View\ItemView;
9
use Sylius\ShopApiPlugin\View\TotalsView;
10
11
final class CartViewFactory implements CartViewFactoryInterface
12
{
13
    /**
14
     * @var ProductViewFactoryInterface
15
     */
16
    private $productViewFactory;
17
18
    /**
19
     * @var ProductVariantViewFactoryInterface
20
     */
21
    private $productVariantViewFactory;
22
23
    /**
24
     * @var AddressViewFactoryInterface
25
     */
26
    private $addressViewFactory;
27
28
    /**
29
     * @param ProductViewFactoryInterface $productViewFactory
30
     * @param ProductVariantViewFactoryInterface $productVariantViewFactory
31
     * @param AddressViewFactoryInterface $addressViewFactory
32
     */
33
    public function __construct(
34
        ProductViewFactoryInterface $productViewFactory,
35
        ProductVariantViewFactoryInterface $productVariantViewFactory,
36
        AddressViewFactoryInterface $addressViewFactory
37
    ) {
38
        $this->productViewFactory = $productViewFactory;
39
        $this->productVariantViewFactory = $productVariantViewFactory;
40
        $this->addressViewFactory = $addressViewFactory;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function create(OrderInterface $cart, $localeCode)
47
    {
48
        $cartView = new CartSummaryView();
49
        $cartView->channel = $cart->getChannel()->getCode();
50
        $cartView->currency = $cart->getCurrencyCode();
51
        $cartView->locale = $localeCode;
52
        $cartView->checkoutState = $cart->getCheckoutState();
53
        $cartView->tokenValue = $cart->getTokenValue();
54
        $cartView->totals = new TotalsView();
55
        $cartView->totals->promotion = 0;
56
        $cartView->totals->items = $cart->getItemsTotal();
57
        $cartView->totals->shipping = $cart->getShippingTotal();
58
        $cartView->totals->taxes = $cart->getTaxTotal();
59
60
        /** @var OrderItemInterface $item */
61
        foreach ($cart->getItems() as $item) {
62
            $itemView = new ItemView();
63
64
            $itemView->id = $item->getId();
65
            $itemView->quantity = $item->getQuantity();
66
            $itemView->total = $item->getTotal();
67
            $itemView->product = $this->productViewFactory->create($item->getProduct(), $localeCode);
68
            $itemView->product->variants = [$this->productVariantViewFactory->create($item->getVariant(), $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...
69
70
            $cartView->items[] = $itemView;
71
        }
72
73
        if (null !== $cart->getShippingAddress()) {
74
            $cartView->shippingAddress = $this->addressViewFactory->create($cart->getShippingAddress());
75
        }
76
77
        if (null !== $cart->getBillingAddress()) {
78
            $cartView->billingAddress = $this->addressViewFactory->create($cart->getBillingAddress());
79
        }
80
81
        return $cartView;
82
    }
83
}
84