Completed
Push — master ( a4c05f...87489a )
by Łukasz
11s
created

CartItemViewFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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