GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — develop ( 851100...f799d7 )
by Borut
15:15
created

SecurityEventsListener::onSwitchUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 53
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 53
rs 9.5797
cc 2
eloc 38
nc 2
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Application\EventListener;
4
5
use Application\Entity\UserActionEntity;
6
use Symfony\Component\Security\Http\SecurityEvents;
7
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8
use Silex\Application;
9
10
/**
11
 * @author Borut Balažek <[email protected]>
12
 */
13
class SecurityEventsListener implements EventSubscriberInterface
14
{
15
    protected $app;
16
17
    public function __construct(Application $app)
18
    {
19
        $this->app = $app;
20
    }
21
22
    public function onInteractiveLogin($event)
23
    {
24
        $app = $this->app;
25
26
        $token = $event->getAuthenticationToken();
27
        $user = $token->getUser();
28
29
        $userActionEntity = new UserActionEntity();
30
        $userActionEntity
31
            ->setUser($user)
32
            ->setKey('user.login')
33
            ->setType('event')
34
            ->setMessage('User has been logged in!')
35
            ->setIp($app['request']->getClientIp())
36
            ->setUserAgent($app['request']->headers->get('User-Agent'))
37
        ;
38
39
        $app['orm.em']->persist($userActionEntity);
40
        $app['orm.em']->flush();
41
    }
42
43
    public function onSwitchUser($event)
44
    {
45
        $app = $this->app;
46
47
        $user = $app['security']->getToken()->getUser();
48
        $targetUser = $event->getTargetUser();
49
50
        if ($app['security']->isGranted('ROLE_PREVIOUS_ADMIN')) {
51
            $targetUser = $app['orm.em']
52
                ->find(
53
                    'Application\Entity\UserEntity',
54
                    $targetUser->getId()
55
                )
56
            ;
57
            $userActionEntity = new UserActionEntity();
58
            $userActionEntity
59
                ->setUser($targetUser)
60
                ->setKey('user.switch.back')
61
                ->setType('event')
62
                ->setMessage(
63
                    'User has switched back to own user (from user with ID "'.$user->getId().'")!'
64
                )
65
                ->setData(array(
66
                    'user_id' => $targetUser->getId(),
67
                    'from_user_id' => $user->getId(),
68
                ))
69
                ->setIp($app['request']->getClientIp())
70
                ->setUserAgent($app['request']->headers->get('User-Agent'))
71
            ;
72
73
            $app['orm.em']->persist($userActionEntity);
74
            $app['orm.em']->flush();
75
        } else {
76
            $userActionEntity = new UserActionEntity();
77
            $userActionEntity
78
                ->setUser($user)
79
                ->setKey('user.switch')
80
                ->setType('event')
81
                ->setMessage(
82
                    'User has switched to user with ID "'.$targetUser->getId().'"!'
83
                )
84
                ->setData(array(
85
                    'user_id' => $user->getId(),
86
                    'to_user_id' => $targetUser->getId(),
87
                ))
88
                ->setIp($app['request']->getClientIp())
89
                ->setUserAgent($app['request']->headers->get('User-Agent'))
90
            ;
91
92
            $app['orm.em']->persist($userActionEntity);
93
            $app['orm.em']->flush();
94
        }
95
    }
96
97
    public static function getSubscribedEvents()
98
    {
99
        return array(
100
            SecurityEvents::INTERACTIVE_LOGIN => array('onInteractiveLogin'),
101
            SecurityEvents::SWITCH_USER => array('onSwitchUser'),
102
        );
103
    }
104
}
105