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

CartItemViewFactory   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 5
dl 0
loc 40
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A create() 0 12 1
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