Completed
Push — 4.0 ( 268f2c...88f012 )
by Hideki
05:48 queued 10s
created

src/Eccube/EventListener/SecurityListener.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 EC-CUBE
5
 *
6
 * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.ec-cube.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\EventListener;
15
16
use Doctrine\ORM\EntityManagerInterface;
17
use Eccube\Entity\Customer;
18
use Eccube\Entity\Member;
19
use Eccube\Service\CartService;
20
use Eccube\Service\PurchaseFlow\PurchaseContext;
21
use Eccube\Service\PurchaseFlow\PurchaseFlow;
22
use Eccube\Service\OrderHelper;
23
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
24
use Symfony\Component\HttpFoundation\RequestStack;
25
use Symfony\Component\Security\Core\AuthenticationEvents;
26
use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent;
27
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
28
use Symfony\Component\Security\Http\SecurityEvents;
29
30
class SecurityListener implements EventSubscriberInterface
31
{
32
    protected $em;
33
34
    protected $cartService;
35
36
    protected $purchaseFlow;
37
38
    protected $requestStack;
39 40
40
    public function __construct(
41
        EntityManagerInterface $em,
42
        CartService $cartService,
43
        PurchaseFlow $cartPurchaseFlow,
44
        RequestStack $requestStack
45 40
    ) {
46 40
        $this->em = $em;
47 40
        $this->cartService = $cartService;
48 40
        $this->purchaseFlow = $cartPurchaseFlow;
49
        $this->requestStack = $requestStack;
50
    }
51
52
    /**
53
     * @param InteractiveLoginEvent $event
54 1
     */
55
    public function onInteractiveLogin(InteractiveLoginEvent $event)
56
    {
57 1
        $user = $event
58 1
            ->getAuthenticationToken()
59
            ->getUser();
60 1
61 1
        if ($user instanceof Member) {
62 1
            $user->setLoginDate(new \DateTime());
63 1
            $this->em->persist($user);
64
            $this->em->flush();
65
        } elseif ($user instanceof Customer) {
66
            $this->cartService->mergeFromPersistedCart();
67
            foreach ($this->cartService->getCarts() as $Cart) {
68
                $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart, $user));
69
            }
70
            $this->cartService->save();
71
            if (count($this->cartService->getCarts()) > 1) {
72
                // カートが分割されていればメッセージを表示
73
                $event->getRequest()->getSession()->set(OrderHelper::SESSION_CART_DIVIDE_FLAG, true);
74
            }
75
        }
76
    }
77
78
    /**
79
     * @param AuthenticationFailureEvent $event
80
     */
81
    public function onAuthenticationFailure(AuthenticationFailureEvent $event)
0 ignored issues
show
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
82
    {
83
        $request = $this->requestStack->getCurrentRequest();
84
        $request->getSession()->set('_security.login_memory', (bool) $request->request->get('login_memory', 0));
85
    }
86
87
    /**
88
     * Returns an array of event names this subscriber wants to listen to.
89
     *
90
     * The array keys are event names and the value can be:
91
     *
92
     * * The method name to call (priority defaults to 0)
93
     * * An array composed of the method name to call and the priority
94
     * * An array of arrays composed of the method names to call and respective
95
     *   priorities, or 0 if unset
96
     *
97
     * For instance:
98
     *
99
     * * array('eventName' => 'methodName')
100
     * * array('eventName' => array('methodName', $priority))
101
     * * array('eventName' => array(array('methodName1', $priority), array('methodName2'))
102
     *
103
     * @return array The event names to listen to
104 1
     */
105
    public static function getSubscribedEvents()
106
    {
107 1
        return [
108 1
            SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
109
            AuthenticationEvents::AUTHENTICATION_FAILURE => 'onAuthenticationFailure',
110
        ];
111
    }
112
}
113