1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sylius\ShopApiPlugin\Handler; |
4
|
|
|
|
5
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
6
|
|
|
use Sylius\Component\Core\Repository\OrderRepositoryInterface; |
7
|
|
|
use Sylius\Component\Order\Processor\OrderProcessorInterface; |
8
|
|
|
use Sylius\Component\Promotion\Repository\PromotionCouponRepositoryInterface; |
9
|
|
|
use Sylius\ShopApiPlugin\Command\AddCoupon; |
10
|
|
|
use Webmozart\Assert\Assert; |
11
|
|
|
|
12
|
|
|
final class AddCouponHandler |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var OrderRepositoryInterface |
16
|
|
|
*/ |
17
|
|
|
private $orderRepository; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var PromotionCouponRepositoryInterface |
21
|
|
|
*/ |
22
|
|
|
private $couponRepository; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var OrderProcessorInterface |
26
|
|
|
*/ |
27
|
|
|
private $orderProcessor; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param OrderRepositoryInterface $orderRepository |
31
|
|
|
* @param PromotionCouponRepositoryInterface $couponRepository |
32
|
|
|
* @param OrderProcessorInterface $orderProcessor |
33
|
|
|
*/ |
34
|
|
|
public function __construct( |
35
|
|
|
OrderRepositoryInterface $orderRepository, |
36
|
|
|
PromotionCouponRepositoryInterface $couponRepository, |
37
|
|
|
OrderProcessorInterface $orderProcessor |
38
|
|
|
) { |
39
|
|
|
$this->orderRepository = $orderRepository; |
40
|
|
|
$this->couponRepository = $couponRepository; |
41
|
|
|
$this->orderProcessor = $orderProcessor; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param AddCoupon $addCoupon |
46
|
|
|
*/ |
47
|
|
|
public function handle(AddCoupon $addCoupon) |
48
|
|
|
{ |
49
|
|
|
/** @var OrderInterface $cart */ |
50
|
|
|
$cart = $this->orderRepository->findOneBy(['tokenValue' => $addCoupon->orderToken()]); |
51
|
|
|
|
52
|
|
|
Assert::notNull($cart, sprintf('Cart with token %s has not been found.', $addCoupon->orderToken())); |
53
|
|
|
|
54
|
|
|
$coupon = $this->couponRepository->findOneBy(['code' => $addCoupon->couponCode()]); |
55
|
|
|
|
56
|
|
|
Assert::notNull($coupon, sprintf('Coupon with code %s has not been found.', $addCoupon->couponCode())); |
57
|
|
|
|
58
|
|
|
$cart->setPromotionCoupon($coupon); |
|
|
|
|
59
|
|
|
|
60
|
|
|
$this->orderProcessor->process($cart); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.