Completed
Push — master ( 13edef...d5b56b )
by Jeroen
85:52 queued 71:18
created

PasswordCheckListener::onKernelRequest()   B

Complexity

Conditions 10
Paths 5

Size

Total Lines 22

Duplication

Lines 3
Ratio 13.64 %

Code Coverage

Tests 12
CRAP Score 10

Importance

Changes 0
Metric Value
dl 3
loc 22
ccs 12
cts 12
cp 1
rs 7.6666
c 0
b 0
f 0
cc 10
nc 5
nop 1
crap 10

How to fix   Complexity   

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 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 1
     */
59
    public function __construct(AuthorizationCheckerInterface $authorizationChecker, TokenStorageInterface $tokenStorage, Router $router, Session $session, TranslatorInterface $translator, AdminRouteHelper $adminRouteHelper)
60 1
    {
61 1
        $this->authorizationChecker = $authorizationChecker;
62 1
        $this->tokenStorage = $tokenStorage;
63 1
        $this->router = $router;
64 1
        $this->session = $session;
65 1
        $this->translator = $translator;
66 1
        $this->adminRouteHelper = $adminRouteHelper;
67
    }
68
69
    /**
70
     * @param GetResponseEvent|ResponseEvent $event
71 1
     **/
72
    public function onKernelRequest($event)
73 1
    {
74 1 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 1
            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 1
        }
77 1
78 1
        $url = $event->getRequest()->getRequestUri();
79 1
        if ($this->tokenStorage->getToken() && $this->adminRouteHelper->isAdminRoute($url)) {
80 1
            $route = $event->getRequest()->get('_route');
81 1
            if ($this->authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED') && $route != 'fos_user_change_password') {
82 1
                $user = $this->tokenStorage->getToken()->getUser();
83
                if ($user->isPasswordChanged() === false) {
84 1
                    $response = new RedirectResponse($this->router->generate('fos_user_change_password'));
85
                    $this->session->getFlashBag()->add(
86
                        FlashTypes::ERROR,
87
                        $this->translator->trans('kuma_admin.password_check.flash.error')
88 1
                    );
89
                    $event->setResponse($response);
90
                }
91
            }
92
        }
93
    }
94
}
95