Completed
Push — template-events ( d018fc...2e9f01 )
by Kamil
65:59 queued 42:59
created

CartChangeListener   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
lcom 1
cbo 5
dl 0
loc 51
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A recalculateOrderOnAdd() 0 10 1
A recalculateOrderOnDelete() 0 11 1
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