Completed
Push — master ( 074f23...421643 )
by Paweł
25:11 queued 15:02
created

OrderTotalIntegrityChecker::check()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 2
eloc 10
nc 2
nop 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\ShopBundle\EventListener;
13
14
use Doctrine\Common\Persistence\ObjectManager;
15
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
16
use Sylius\Component\Core\Model\OrderInterface;
17
use Sylius\Component\Order\Processor\OrderProcessorInterface;
18
use Symfony\Component\HttpFoundation\RedirectResponse;
19
use Symfony\Component\Routing\RouterInterface;
20
use Webmozart\Assert\Assert;
21
22
/**
23
 * @author Arkadiusz Krakowiak <[email protected]>
24
 */
25
final class OrderTotalIntegrityChecker
26
{
27
    /**
28
     * @var OrderProcessorInterface
29
     */
30
    private $orderProcessors;
31
32
    /**
33
     * @var RouterInterface
34
     */
35
    private $router;
36
37
    /**
38
     * @var ObjectManager
39
     */
40
    private $manager;
41
42
    /**
43
     * @param OrderProcessorInterface $orderProcessors
44
     * @param RouterInterface $router
45
     * @param ObjectManager $manager
46
     */
47
    public function __construct(
48
        OrderProcessorInterface $orderProcessors,
49
        RouterInterface $router,
50
        ObjectManager $manager
51
    ) {
52
        $this->orderProcessors = $orderProcessors;
53
        $this->router = $router;
54
        $this->manager = $manager;
55
    }
56
57
    /**
58
     * @param ResourceControllerEvent $event
59
     */
60
    public function check(ResourceControllerEvent $event)
61
    {
62
        /** @var OrderInterface $order */
63
        $order = $event->getSubject();
64
65
        Assert::isInstanceOf($order, OrderInterface::class);
66
67
        $oldTotal = $order->getTotal();
68
        $this->orderProcessors->process($order);
69
70
        if ($order->getTotal() !== $oldTotal) {
71
            $event->stop('sylius.order.total_integrity', ResourceControllerEvent::TYPE_ERROR);
72
            $event->setResponse(new RedirectResponse($this->router->generate('sylius_shop_checkout_complete')));
73
74
            $this->manager->persist($order);
75
            $this->manager->flush();
76
        }
77
    }
78
}
79