ShippingMethodViewFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A create() 0 4 1
A createWithShippingMethod() 0 22 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sylius\ShopApiPlugin\Factory;
6
7
use Sylius\Component\Core\Model\ShipmentInterface;
8
use Sylius\Component\Core\Model\ShippingMethodInterface;
9
use Sylius\Component\Registry\ServiceRegistry;
10
use Sylius\Component\Shipping\Calculator\CalculatorInterface;
11
use Sylius\ShopApiPlugin\View\ShippingMethodView;
12
13
final class ShippingMethodViewFactory implements ShippingMethodViewFactoryInterface
14
{
15
    /** @var ServiceRegistry */
16
    private $calculatorRegistry;
17
18
    /** @var PriceViewFactoryInterface */
19
    private $priceViewFactory;
20
21
    /** @var string */
22
    private $shippingMethodViewClass;
23
24
    public function __construct(
25
        ServiceRegistry $calculatorRegistry,
26
        PriceViewFactoryInterface $priceViewFactory,
27
        string $shippingMethodViewClass
28
    ) {
29
        $this->calculatorRegistry = $calculatorRegistry;
30
        $this->priceViewFactory = $priceViewFactory;
31
        $this->shippingMethodViewClass = $shippingMethodViewClass;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function create(ShipmentInterface $shipment, string $locale, string $currency): ShippingMethodView
38
    {
39
        return $this->createWithShippingMethod($shipment, $shipment->getMethod(), $locale, $currency);
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function createWithShippingMethod(
46
        ShipmentInterface $shipment,
47
        ShippingMethodInterface $shippingMethod,
48
        string $locale,
49
        string $currency
50
    ): ShippingMethodView {
51
        /** @var CalculatorInterface $calculator */
52
        $calculator = $this->calculatorRegistry->get($shippingMethod->getCalculator());
53
54
        /** @var ShippingMethodView $shippingMethodView */
55
        $shippingMethodView = new $this->shippingMethodViewClass();
56
57
        $shippingMethodView->code = $shippingMethod->getCode();
58
        $shippingMethodView->name = $shippingMethod->getTranslation($locale)->getName();
59
        $shippingMethodView->description = $shippingMethod->getTranslation($locale)->getDescription();
60
        $shippingMethodView->price = $this->priceViewFactory->create(
61
            $calculator->calculate($shipment, $shippingMethod->getConfiguration()),
62
            $currency
63
        );
64
65
        return $shippingMethodView;
66
    }
67
}
68