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)]; |
|
|
|
|
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
|
|
|
|
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.