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

ShipmentViewFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 1
nc 1
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Setono\SyliusLagersystemPlugin\Factory\Shipping;
6
7
use Setono\SyliusLagersystemPlugin\Factory\Order\OrderViewFactoryInterface;
8
use Setono\SyliusLagersystemPlugin\View\Shipping\ShipmentView;
9
use Sylius\Component\Core\Model\OrderInterface;
10
use Sylius\Component\Core\Model\ShipmentInterface;
11
use Sylius\Component\Shipping\Model\ShipmentUnitInterface;
12
use Webmozart\Assert\Assert;
13
14
class ShipmentViewFactory implements ShipmentViewFactoryInterface
15
{
16
    /** @var ShippingMethodViewFactoryInterface */
17
    protected $shippingMethodViewFactory;
18
19
    /** @var ShipmentUnitViewFactoryInterface */
20
    protected $shipmentUnitViewFactory;
21
22
    /** @var OrderViewFactoryInterface */
23
    protected $orderViewFactory;
24
25
    /** @var string */
26
    protected $shipmentViewClass;
27
28
    public function __construct(
29
        ShippingMethodViewFactoryInterface $shippingMethodViewFactory,
30
        ShipmentUnitViewFactoryInterface $shipmentUnitViewFactory,
31
        OrderViewFactoryInterface $orderViewFactory,
32
        string $shipmentViewClass
33
    ) {
34
        $this->shippingMethodViewFactory = $shippingMethodViewFactory;
35
        $this->shipmentUnitViewFactory = $shipmentUnitViewFactory;
36
        $this->orderViewFactory = $orderViewFactory;
37
        $this->shipmentViewClass = $shipmentViewClass;
38
    }
39
40
    public function create(ShipmentInterface $shipment, string $locale): ShipmentView
41
    {
42
        /** @var OrderInterface|null $order */
43
        $order = $shipment->getOrder();
44
        Assert::notNull($order);
45
46
        $shippedAt = $shipment->getShippedAt();
47
48
        /** @var ShipmentView $shipmentView */
49
        $shipmentView = new $this->shipmentViewClass();
50
        $shipmentView->id = $shipment->getId();
51
        $shipmentView->state = $shipment->getState();
52
        $shipmentView->method = $this->shippingMethodViewFactory->create($shipment, $locale);
53
54
        /** @var ShipmentUnitInterface $shipmentUnit */
55
        foreach ($shipment->getUnits() as $shipmentUnit) {
56
            $shipmentView->units[] = $this->shipmentUnitViewFactory->create($shipmentUnit, $locale);
57
        }
58
59
        $shipmentView->order = $this->orderViewFactory->create($order);
60
        $shipmentView->tracking = $shipment->getTracking();
61
        $shipmentView->shippedAt = null !== $shippedAt ? $shippedAt->format('c') : null;
62
63
        return $shipmentView;
64
    }
65
}
66