PasswordCheckListener   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 80
Duplicated Lines 3.75 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 96%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 9
dl 3
loc 80
ccs 24
cts 25
cp 0.96
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B onKernelRequest() 3 26 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Kunstmaan\AdminBundle\EventListener;
4
5
use Kunstmaan\AdminBundle\FlashMessages\FlashTypes;
6
use Symfony\Component\HttpFoundation\RedirectResponse;
7
use Symfony\Component\HttpFoundation\Session\Session;
8
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
9
use Symfony\Component\HttpKernel\Event\ResponseEvent;
10
use Symfony\Component\Routing\RouterInterface as Router;
11
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
12
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
13
use Symfony\Component\Translation\TranslatorInterface;
14
use Kunstmaan\AdminBundle\Helper\AdminRouteHelper;
15
16
/**
17
 * PasswordCheckListener to check if the user has to change his password
18
 */
19
class PasswordCheckListener
20
{
21
    /**
22
     * @var AuthorizationCheckerInterface
23
     */
24
    private $authorizationChecker;
25
26
    /**
27
     * @var TokenStorageInterface
28
     */
29
    private $tokenStorage;
30
31
    /**
32
     * @var Router
33
     */
34
    private $router;
35
36
    /**
37
     * @var Session
38
     */
39
    private $session;
40
41
    /**
42
     * @var TranslatorInterface
43
     */
44
    private $translator;
45
46
    /**
47
     * @var AdminRouteHelper
48
     */
49
    private $adminRouteHelper;
50
51
    /**
52
     * @param AuthorizationCheckerInterface $authorizationChecker
53
     * @param TokenStorageInterface         $tokenStorage
54
     * @param Router                        $router
55
     * @param Session                       $session
56
     * @param TranslatorInterface           $translator
57
     * @param AdminRouteHelper              $adminRouteHelper
58
     */
59 3
    public function __construct(AuthorizationCheckerInterface $authorizationChecker, TokenStorageInterface $tokenStorage, Router $router, Session $session, TranslatorInterface $translator, AdminRouteHelper $adminRouteHelper)
60
    {
61 3
        $this->authorizationChecker = $authorizationChecker;
62 3
        $this->tokenStorage = $tokenStorage;
63 3
        $this->router = $router;
64 3
        $this->session = $session;
65 3
        $this->translator = $translator;
66 3
        $this->adminRouteHelper = $adminRouteHelper;
67 3
    }
68
69
    /**
70
     * @param GetResponseEvent|ResponseEvent $event
71
     **/
72 3
    public function onKernelRequest($event)
73
    {
74 3 View Code Duplication
        if (!$event instanceof GetResponseEvent && !$event instanceof ResponseEvent) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
            throw new \InvalidArgumentException(\sprintf('Expected instance of type %s, %s given', \class_exists(ResponseEvent::class) ? ResponseEvent::class : GetResponseEvent::class, \is_object($event) ? \get_class($event) : \gettype($event)));
76
        }
77
78 3
        $url = $event->getRequest()->getRequestUri();
79 3
        if (!$this->adminRouteHelper->isAdminRoute($url)) {
80 1
            return;
81
        }
82
83 2
        if ($this->tokenStorage->getToken()) {
84 2
            $route = $event->getRequest()->get('_route');
85 2
            if ($this->authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED') && $route != 'fos_user_change_password') {
86 2
                $user = $this->tokenStorage->getToken()->getUser();
87 2
                if ($user->isPasswordChanged() === false) {
88 2
                    $response = new RedirectResponse($this->router->generate('fos_user_change_password'));
89 2
                    $this->session->getFlashBag()->add(
90 2
                        FlashTypes::DANGER,
91 2
                        $this->translator->trans('kuma_admin.password_check.flash.error')
92
                    );
93 2
                    $event->setResponse($response);
94
                }
95
            }
96
        }
97 2
    }
98
}
99