1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Sylius\ShopApiPlugin\Factory; |
6
|
|
|
|
7
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
8
|
|
|
use Sylius\Component\Core\Model\OrderItemInterface; |
9
|
|
|
use Sylius\ShopApiPlugin\View\ItemView; |
10
|
|
|
|
11
|
|
|
final class CartItemViewFactory implements CartItemViewFactoryInterface |
12
|
|
|
{ |
13
|
|
|
/** @var ProductViewFactoryInterface */ |
14
|
|
|
private $productViewFactory; |
15
|
|
|
|
16
|
|
|
/** @var ProductVariantViewFactoryInterface */ |
17
|
|
|
private $productVariantViewFactory; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
private $cartItemViewClass; |
21
|
|
|
|
22
|
|
|
public function __construct( |
23
|
|
|
ProductViewFactoryInterface $productViewFactory, |
24
|
|
|
ProductVariantViewFactoryInterface $productVariantViewFactory, |
25
|
|
|
string $cartItemViewClass |
26
|
|
|
) { |
27
|
|
|
$this->productViewFactory = $productViewFactory; |
28
|
|
|
$this->productVariantViewFactory = $productVariantViewFactory; |
29
|
|
|
$this->cartItemViewClass = $cartItemViewClass; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
|
public function create(OrderItemInterface $item, ChannelInterface $channel, string $locale): ItemView |
36
|
|
|
{ |
37
|
|
|
/** @var ItemView $itemView */ |
38
|
|
|
$itemView = new $this->cartItemViewClass(); |
39
|
|
|
|
40
|
|
|
$itemView->id = $item->getId(); |
41
|
|
|
$itemView->quantity = $item->getQuantity(); |
42
|
|
|
$itemView->total = $item->getTotal(); |
43
|
|
|
$itemView->product = $this->productViewFactory->create($item->getProduct(), $channel, $locale); |
44
|
|
|
$itemView->product->variants = [$this->productVariantViewFactory->create($item->getVariant(), $channel, $locale)]; |
45
|
|
|
|
46
|
|
|
return $itemView; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|