ItemViewFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 42
rs 10
c 1
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A create() 0 21 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Setono\SyliusLagersystemPlugin\Factory\Order;
6
7
use Setono\SyliusLagersystemPlugin\Factory\Product\ProductVariantViewFactoryInterface;
8
use Setono\SyliusLagersystemPlugin\View\Order\ItemView;
9
use Sylius\Component\Core\Model\OrderItemInterface;
10
use Sylius\Component\Core\Model\OrderItemUnitInterface;
11
12
class ItemViewFactory implements ItemViewFactoryInterface
13
{
14
    /** @var ProductVariantViewFactoryInterface */
15
    protected $productVariantViewFactory;
16
17
    /** @var ItemUnitViewFactoryInterface */
18
    protected $itemUnitViewFactory;
19
20
    /** @var string */
21
    protected $itemViewClass;
22
23
    public function __construct(
24
        ProductVariantViewFactoryInterface $productVariantViewFactory,
25
        ItemUnitViewFactoryInterface $itemUnitViewFactory,
26
        string $itemViewClass
27
    ) {
28
        $this->productVariantViewFactory = $productVariantViewFactory;
29
        $this->itemUnitViewFactory = $itemUnitViewFactory;
30
        $this->itemViewClass = $itemViewClass;
31
    }
32
33
    public function create(OrderItemInterface $item, string $localeCode): ItemView
34
    {
35
        /** @var ItemView $itemView */
36
        $itemView = new $this->itemViewClass();
37
        $itemView->id = $item->getId();
38
        $itemView->quantity = $item->getQuantity();
39
        $itemView->total = $item->getTotal();
40
        $itemView->variant = $this->productVariantViewFactory->create(
41
            $item->getVariant(),
0 ignored issues
show
Bug introduced by
It seems like $item->getVariant() can also be of type null; however, parameter $variant of Setono\SyliusLagersystem...toryInterface::create() does only seem to accept Sylius\Component\Core\Mo...ProductVariantInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
            /** @scrutinizer ignore-type */ $item->getVariant(),
Loading history...
42
            $localeCode
43
        );
44
45
        /** @var OrderItemUnitInterface $itemUnit */
46
        foreach ($item->getUnits() as $itemUnit) {
47
            $itemView->units[] = $this->itemUnitViewFactory->create(
48
                $itemUnit,
49
                $localeCode
50
            );
51
        }
52
53
        return $itemView;
54
    }
55
}
56