Completed
Push — currency-cookie ( 8ef560...28b724 )
by Kamil
24:47
created

AddToCartListener   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 2
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 36
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A recalculateOrder() 0 10 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\Processor\OrderProcessorInterface;
17
use Symfony\Component\EventDispatcher\GenericEvent;
18
use Webmozart\Assert\Assert;
19
20
/**
21
 * @author Łukasz Chruściel <[email protected]>
22
 */
23
final class AddToCartListener
24
{
25
    /**
26
     * @var OrderProcessorInterface
27
     */
28
    private $orderProcessor;
29
30
    /**
31
     * @var ObjectManager
32
     */
33
    private $objectManager;
34
35
    /**
36
     * @param OrderProcessorInterface $orderProcessor
37
     * @param ObjectManager $objectManager
38
     */
39
    public function __construct(OrderProcessorInterface $orderProcessor, ObjectManager $objectManager)
40
    {
41
        $this->orderProcessor = $orderProcessor;
42
        $this->objectManager = $objectManager;
43
    }
44
45
    /**
46
     * @param GenericEvent $event
47
     */
48
    public function recalculateOrder(GenericEvent $event)
49
    {
50
        $item = $event->getSubject();
51
        Assert::isInstanceOf($item, OrderItemInterface::class);
52
        $order = $item->getOrder();
53
54
        $this->orderProcessor->process($order);
55
56
        $this->objectManager->persist($order);
57
    }
58
}
59