Completed
Push — master ( 8f87a6...68c0aa )
by Gilles
02:02
created

TakeCustomerAccountListener::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/*************************************************************************************/
3
/*      This file is part of the module FeatureType                                */
4
/*                                                                                   */
5
/*      Copyright (c) OpenStudio                                                     */
6
/*      email : [email protected]                                                       */
7
/*      web : http://www.thelia.net                                                  */
8
/*                                                                                   */
9
/*      For the full copyright and license information, please view the LICENSE.txt  */
10
/*      file that was distributed with this source code.                             */
11
/*************************************************************************************/
12
13
namespace TakeCustomerAccount\EventListener;
14
15
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17
use TakeCustomerAccount\Event\TakeCustomerAccountEvent;
18
use TakeCustomerAccount\Event\TakeCustomerAccountEvents;
19
use TakeCustomerAccount\TakeCustomerAccount;
20
use Thelia\Core\Event\Customer\CustomerLoginEvent;
21
use Thelia\Core\Event\DefaultActionEvent;
22
use Thelia\Core\Event\TheliaEvents;
23
use Thelia\Core\HttpFoundation\Request;
24
use Thelia\Core\Security\AccessManager;
25
use Thelia\Core\Security\SecurityContext;
26
use Thelia\Model\AdminLog;
27
28
/**
29
 * Class TakeCustomerAccountListener
30
 * @package TakeCustomerAccount\EventListener
31
 * @author Gilles Bourgeat <[email protected]>
32
 */
33
class TakeCustomerAccountListener implements EventSubscriberInterface
34
{
35
    /** @var EventDispatcherInterface */
36
    protected $eventDispatcher;
37
38
    /** @var SecurityContext */
39
    protected $securityContext;
40
41
    /** @var Request */
42
    protected $request;
43
44
    /**
45
     * @param EventDispatcherInterface $eventDispatcher
46
     * @param SecurityContext $securityContext
47
     * @param Request $request
48
     */
49
    public function __construct(
50
        EventDispatcherInterface $eventDispatcher,
51
        SecurityContext $securityContext,
52
        Request $request
53
    ) {
54
        $this->eventDispatcher = $eventDispatcher;
55
56
        $this->securityContext = $securityContext;
57
58
        $this->request = $request;
59
    }
60
61
    /**
62
     * @param TakeCustomerAccountEvent $event
63
     */
64
    public function take(TakeCustomerAccountEvent $event)
65
    {
66
        $this->eventDispatcher->dispatch(TheliaEvents::CUSTOMER_LOGOUT, new DefaultActionEvent());
67
68
        $this->request->getSession()->getSessionCart($this->eventDispatcher)->clear();
69
70
        $this->eventDispatcher->dispatch(
71
            TheliaEvents::CUSTOMER_LOGIN,
72
            new CustomerLoginEvent($event->getCustomer())
73
        );
74
75
        AdminLog::append(
76
            TakeCustomerAccount::MODULE_DOMAIN,
77
            AccessManager::VIEW,
78
            'Took control of the customer account "' . $event->getCustomer()->getId() . '"',
79
            $this->request,
80
            $this->securityContext->getAdminUser()
81
        );
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public static function getSubscribedEvents()
88
    {
89
        return array(
90
            TakeCustomerAccountEvents::TAKE_CUSTOMER_ACCOUNT => ['take', 128]
91
        );
92
    }
93
}
94