Passed
Pull Request — master (#2)
by Igor
08:27
created

ShipmentUnitViewFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Setono\SyliusLagersystemPlugin\Factory\Shipping;
6
7
use Setono\SyliusLagersystemPlugin\Factory\Product\ProductVariantViewFactoryInterface;
8
use Setono\SyliusLagersystemPlugin\View\Shipping\ShipmentUnitView;
9
use Sylius\Component\Core\Model\OrderItemUnitInterface;
10
use Sylius\Component\Core\Model\ProductVariantInterface;
11
use Sylius\Component\Shipping\Model\ShipmentUnitInterface;
12
use Webmozart\Assert\Assert;
13
14
class ShipmentUnitViewFactory implements ShipmentUnitViewFactoryInterface
15
{
16
    /** @var ProductVariantViewFactoryInterface */
17
    protected $shippableViewFactory;
18
19
    /** @var string */
20
    protected $shipmentUnitViewClass;
21
22
    public function __construct(
23
        ProductVariantViewFactoryInterface $shippableViewFactory,
24
        string $shipmentUnitViewClass
25
    ) {
26
        $this->shippableViewFactory = $shippableViewFactory;
27
        $this->shipmentUnitViewClass = $shipmentUnitViewClass;
28
    }
29
30
    public function create(ShipmentUnitInterface $shipmentUnit, string $locale): ShipmentUnitView
31
    {
32
        /** @var ProductVariantInterface|null $shippable */
33
        $shippable = $shipmentUnit->getShippable();
34
        Assert::notNull($shippable);
35
36
        /** @var ShipmentUnitView $shipmentUnitView */
37
        $shipmentUnitView = new $this->shipmentUnitViewClass();
38
        $shipmentUnitView->shippable = $this->shippableViewFactory->create($shippable, $locale);
39
40
        if ($shipmentUnit instanceof OrderItemUnitInterface) {
41
            $shipmentUnitView->total = $shipmentUnit->getTotal();
42
            $shipmentUnitView->adjustmentsTotal = $shipmentUnit->getAdjustmentsTotal();
43
        }
44
45
        return $shipmentUnitView;
46
    }
47
}
48