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\ApiBundle\EventListener; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
15
|
|
|
use Sylius\Component\Core\Model\OrderItemInterface; |
16
|
|
|
use Sylius\Component\Order\Model\OrderInterface; |
17
|
|
|
use Sylius\Component\Order\Processor\OrderProcessorInterface; |
18
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent; |
19
|
|
|
use Webmozart\Assert\Assert; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Łukasz Chruściel <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
final class CartChangeListener |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var OrderProcessorInterface |
28
|
|
|
*/ |
29
|
|
|
private $orderProcessor; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ObjectManager |
33
|
|
|
*/ |
34
|
|
|
private $objectManager; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param OrderProcessorInterface $orderProcessor |
38
|
|
|
* @param ObjectManager $objectManager |
39
|
|
|
*/ |
40
|
|
|
public function __construct(OrderProcessorInterface $orderProcessor, ObjectManager $objectManager) |
41
|
|
|
{ |
42
|
|
|
$this->orderProcessor = $orderProcessor; |
43
|
|
|
$this->objectManager = $objectManager; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param GenericEvent $event |
48
|
|
|
*/ |
49
|
|
|
public function recalculateOrderOnAdd(GenericEvent $event) |
50
|
|
|
{ |
51
|
|
|
$item = $event->getSubject(); |
52
|
|
|
Assert::isInstanceOf($item, OrderItemInterface::class); |
53
|
|
|
$order = $item->getOrder(); |
54
|
|
|
|
55
|
|
|
$this->orderProcessor->process($order); |
56
|
|
|
|
57
|
|
|
$this->objectManager->persist($order); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param GenericEvent $event |
62
|
|
|
*/ |
63
|
|
|
public function recalculateOrderOnDelete(GenericEvent $event) |
64
|
|
|
{ |
65
|
|
|
$item = $event->getSubject(); |
66
|
|
|
Assert::isInstanceOf($item, OrderItemInterface::class); |
67
|
|
|
|
68
|
|
|
/** @var OrderInterface $order */ |
69
|
|
|
$order = $item->getOrder(); |
70
|
|
|
$order->removeItem($item); |
71
|
|
|
|
72
|
|
|
$this->orderProcessor->process($order); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|