|
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
|
|
|
|