TakeCustomerAccountListener   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
c 2
b 0
f 0
lcom 1
cbo 1
dl 0
loc 62
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A take() 0 20 1
A getSubscribedEvents() 0 6 1
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\Cart\CartCreateEvent;
21
use Thelia\Core\Event\Customer\CustomerLoginEvent;
22
use Thelia\Core\Event\DefaultActionEvent;
23
use Thelia\Core\Event\TheliaEvents;
24
use Thelia\Core\HttpFoundation\Request;
25
use Thelia\Core\Security\AccessManager;
26
use Thelia\Core\Security\SecurityContext;
27
use Thelia\Model\AdminLog;
28
29
/**
30
 * Class TakeCustomerAccountListener
31
 * @package TakeCustomerAccount\EventListener
32
 * @author Gilles Bourgeat <[email protected]>
33
 */
34
class TakeCustomerAccountListener implements EventSubscriberInterface
35
{
36
    /** @var EventDispatcherInterface */
37
    protected $eventDispatcher;
38
39
    /** @var SecurityContext */
40
    protected $securityContext;
41
42
    /** @var Request */
43
    protected $request;
44
45
    /**
46
     * @param EventDispatcherInterface $eventDispatcher
47
     * @param SecurityContext $securityContext
48
     * @param Request $request
49
     */
50
    public function __construct(
51
        EventDispatcherInterface $eventDispatcher,
52
        SecurityContext $securityContext,
53
        Request $request
54
    ) {
55
        $this->eventDispatcher = $eventDispatcher;
56
57
        $this->securityContext = $securityContext;
58
59
        $this->request = $request;
60
    }
61
62
    /**
63
     * @param TakeCustomerAccountEvent $event
64
     */
65
    public function take(TakeCustomerAccountEvent $event)
66
    {
67
        $this->eventDispatcher->dispatch(TheliaEvents::CUSTOMER_LOGOUT, new DefaultActionEvent());
68
69
        $this->eventDispatcher->dispatch(
70
            TheliaEvents::CUSTOMER_LOGIN,
71
            new CustomerLoginEvent($event->getCustomer())
72
        );
73
74
        $newCartEvent = new CartCreateEvent();
75
        $this->eventDispatcher->dispatch(TheliaEvents::CART_CREATE_NEW, $newCartEvent);
76
77
        AdminLog::append(
78
            TakeCustomerAccount::MODULE_DOMAIN,
79
            AccessManager::VIEW,
80
            'Took control of the customer account "' . $event->getCustomer()->getId() . '"',
81
            $this->request,
82
            $this->securityContext->getAdminUser()
83
        );
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public static function getSubscribedEvents()
90
    {
91
        return array(
92
            TakeCustomerAccountEvents::TAKE_CUSTOMER_ACCOUNT => ['take', 128]
93
        );
94
    }
95
}
96