|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sylius package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Paweł Jędrzejewski |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sylius\Bundle\CoreBundle\Validator\Constraints; |
|
13
|
|
|
|
|
14
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
|
15
|
|
|
use Sylius\Component\Shipping\Checker\ShippingMethodEligibilityCheckerInterface; |
|
16
|
|
|
use Symfony\Component\Validator\Constraint; |
|
17
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
|
18
|
|
|
use Webmozart\Assert\Assert; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @author Arkadiusz Krakowiak <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
final class OrderShippingMethodEligibilityValidator extends ConstraintValidator |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var ShippingMethodEligibilityCheckerInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $methodEligibilityChecker; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param ShippingMethodEligibilityCheckerInterface $methodEligibilityChecker |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(ShippingMethodEligibilityCheckerInterface $methodEligibilityChecker) |
|
|
|
|
|
|
34
|
|
|
{ |
|
35
|
|
|
$this->methodEligibilityChecker = $methodEligibilityChecker; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param OrderInterface $value |
|
|
|
|
|
|
40
|
|
|
* |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
|
|
public function validate($order, Constraint $constraint) |
|
44
|
|
|
{ |
|
45
|
|
|
Assert::isInstanceOf($order, OrderInterface::class); |
|
46
|
|
|
|
|
47
|
|
|
$shipments = $order->getShipments(); |
|
48
|
|
|
if ($shipments->isEmpty()) { |
|
49
|
|
|
return; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
foreach ($shipments as $shipment) { |
|
53
|
|
|
if (!$this->methodEligibilityChecker->isEligible($shipment, $shipment->getMethod())) { |
|
54
|
|
|
$this->context->addViolation( |
|
55
|
|
|
$constraint->message, |
|
56
|
|
|
['%shippingMethodName%' => $shipment->getMethod()->getName()] |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.