1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Sylius\ShopApiPlugin\Processor; |
6
|
|
|
|
7
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
8
|
|
|
use Sylius\Component\Core\Model\ShipmentInterface; |
9
|
|
|
use Sylius\Component\Order\Model\OrderInterface as BaseOrderInterface; |
10
|
|
|
use Sylius\Component\Order\Processor\OrderProcessorInterface; |
11
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
12
|
|
|
use Sylius\Component\Shipping\Exception\UnresolvedDefaultShippingMethodException; |
13
|
|
|
use Sylius\Component\Shipping\Resolver\DefaultShippingMethodResolverInterface; |
14
|
|
|
use Webmozart\Assert\Assert; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Paweł Jędrzejewski <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
final class OrderShipmentProcessor implements OrderProcessorInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var DefaultShippingMethodResolverInterface |
23
|
|
|
*/ |
24
|
|
|
private $defaultShippingMethodResolver; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var FactoryInterface |
28
|
|
|
*/ |
29
|
|
|
private $shipmentFactory; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param DefaultShippingMethodResolverInterface $defaultShippingMethodResolver |
33
|
|
|
* @param FactoryInterface $shipmentFactory |
34
|
|
|
*/ |
35
|
|
|
public function __construct( |
36
|
|
|
DefaultShippingMethodResolverInterface $defaultShippingMethodResolver, |
37
|
|
|
FactoryInterface $shipmentFactory |
38
|
|
|
) { |
39
|
|
|
$this->defaultShippingMethodResolver = $defaultShippingMethodResolver; |
40
|
|
|
$this->shipmentFactory = $shipmentFactory; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public function process(BaseOrderInterface $order) |
47
|
|
|
{ |
48
|
|
|
/** @var OrderInterface $order */ |
49
|
|
|
Assert::isInstanceOf($order, OrderInterface::class); |
50
|
|
|
|
51
|
|
|
if ($order->isEmpty()) { |
52
|
|
|
$order->removeShipments(); |
53
|
|
|
|
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$shipment = $this->getOrderShipment($order); |
58
|
|
|
|
59
|
|
|
if (null === $shipment) { |
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
foreach ($shipment->getUnits() as $unit) { |
64
|
|
|
$shipment->removeUnit($unit); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
foreach ($order->getItemUnits() as $itemUnit) { |
68
|
|
|
if (null === $itemUnit->getShipment()) { |
69
|
|
|
$shipment->addUnit($itemUnit); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param BaseOrderInterface $order |
76
|
|
|
* |
77
|
|
|
* @return ShipmentInterface |
78
|
|
|
*/ |
79
|
|
|
private function getOrderShipment(BaseOrderInterface $order) |
80
|
|
|
{ |
81
|
|
|
if ($order->hasShipments()) { |
|
|
|
|
82
|
|
|
return $order->getShipments()->first(); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
try { |
86
|
|
|
/** @var ShipmentInterface $shipment */ |
87
|
|
|
$shipment = $this->shipmentFactory->createNew(); |
88
|
|
|
$shipment->setOrder($order); |
89
|
|
|
$shipment->setMethod($this->defaultShippingMethodResolver->getDefaultShippingMethod($shipment)); |
90
|
|
|
|
91
|
|
|
$order->addShipment($shipment); |
|
|
|
|
92
|
|
|
|
93
|
|
|
return $shipment; |
94
|
|
|
} catch (UnresolvedDefaultShippingMethodException $exception) { |
95
|
|
|
return null; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: