1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sylius\ShopApiPlugin\Factory; |
4
|
|
|
|
5
|
|
|
use Sylius\Component\Core\Model\ShipmentInterface; |
6
|
|
|
use Sylius\Component\Core\Model\ShippingMethodInterface; |
7
|
|
|
use Sylius\Component\Registry\ServiceRegistry; |
8
|
|
|
use Sylius\Component\Shipping\Calculator\CalculatorInterface; |
9
|
|
|
use Sylius\ShopApiPlugin\View\ShippingMethodView; |
10
|
|
|
|
11
|
|
|
final class ShippingMethodViewFactory implements ShippingMethodViewFactoryInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var ServiceRegistry |
15
|
|
|
*/ |
16
|
|
|
private $calculators; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var PriceViewFactoryInterface |
20
|
|
|
*/ |
21
|
|
|
private $priceViewFactory; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param ServiceRegistry $calculators |
25
|
|
|
* @param PriceViewFactoryInterface $priceViewFactory |
26
|
|
|
*/ |
27
|
|
|
public function __construct(ServiceRegistry $calculators, PriceViewFactoryInterface $priceViewFactory) |
28
|
|
|
{ |
29
|
|
|
$this->calculators = $calculators; |
30
|
|
|
$this->priceViewFactory = $priceViewFactory; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
public function create(ShipmentInterface $shipment, $locale) |
37
|
|
|
{ |
38
|
|
|
return $this->createWithShippingMethod($shipment, $shipment->getMethod(), $locale); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function createWithShippingMethod(ShipmentInterface $shipment, ShippingMethodInterface $shippingMethod, $locale) |
45
|
|
|
{ |
46
|
|
|
/** @var CalculatorInterface $calculator */ |
47
|
|
|
$calculator = $this->calculators->get($shippingMethod->getCalculator()); |
48
|
|
|
|
49
|
|
|
$shippingMethodView = new ShippingMethodView(); |
50
|
|
|
|
51
|
|
|
$shippingMethodView->code = $shippingMethod->getCode(); |
52
|
|
|
$shippingMethodView->name = $shippingMethod->getTranslation($locale)->getName(); |
|
|
|
|
53
|
|
|
$shippingMethodView->description = $shippingMethod->getTranslation($locale)->getDescription(); |
|
|
|
|
54
|
|
|
$shippingMethodView->price = $this->priceViewFactory->create( |
55
|
|
|
$calculator->calculate($shipment, $shippingMethod->getConfiguration()) |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
return $shippingMethodView; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.