Completed
Push — master ( d87b58...45003b )
by Kamil
125:34 queued 104:29
created

ShopBundle/EventListener/SessionCartSubscriber.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
declare(strict_types=1);
13
14
namespace Sylius\Bundle\ShopBundle\EventListener;
15
16
use Sylius\Component\Core\Storage\CartStorageInterface;
17
use Sylius\Component\Order\Context\CartContextInterface;
18
use Sylius\Component\Order\Context\CartNotFoundException;
19
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
20
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
21
use Symfony\Component\HttpKernel\KernelEvents;
22
23
final class SessionCartSubscriber implements EventSubscriberInterface
24
{
25
    /**
26
     * @var CartContextInterface
27
     */
28
    private $cartContext;
29
30
    /**
31
     * @var CartStorageInterface
32
     */
33
    private $cartStorage;
34
35
    /**
36
     * @param CartContextInterface $cartContext
37
     * @param CartStorageInterface $cartStorage
38
     */
39
    public function __construct(CartContextInterface $cartContext, CartStorageInterface $cartStorage)
40
    {
41
        $this->cartContext = $cartContext;
42
        $this->cartStorage = $cartStorage;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public static function getSubscribedEvents(): array
49
    {
50
        return [
51
            KernelEvents::RESPONSE => ['onKernelResponse'],
52
        ];
53
    }
54
55
    /**
56
     * @param FilterResponseEvent $event
57
     */
58
    public function onKernelResponse(FilterResponseEvent $event): void
59
    {
60
        if (!$event->isMasterRequest()) {
61
            return;
62
        }
63
64
        $session = $event->getRequest()->getSession();
65
        if ($session && !$session->isStarted()) {
66
            return;
67
        }
68
69
        try {
70
            $cart = $this->cartContext->getCart();
71
        } catch (CartNotFoundException $exception) {
72
            return;
73
        }
74
75
        if (null !== $cart && null !== $cart->getId() && null !== $cart->getChannel()) {
76
            $this->cartStorage->setForChannel($cart->getChannel(), $cart);
0 ignored issues
show
$cart of type object<Sylius\Component\...r\Model\OrderInterface> is not a sub-type of object<Sylius\Component\...e\Model\OrderInterface>. It seems like you assume a child interface of the interface Sylius\Component\Order\Model\OrderInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
77
        }
78
    }
79
}
80