HandleSamlRequestSubscriber::onRequest()   B
last analyzed

Complexity

Conditions 10
Paths 7

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 10

Importance

Changes 0
Metric Value
cc 10
eloc 12
c 0
b 0
f 0
nc 7
nop 1
dl 0
loc 24
ccs 13
cts 13
cp 1
crap 10
rs 7.6666

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 App\EventSubscriber;
4
5
use Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorToken;
6
use SchulIT\LightSamlIdpBundle\RequestStorage\RequestStorageInterface;
7
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8
use Symfony\Component\HttpFoundation\RedirectResponse;
9
use Symfony\Component\HttpKernel\Event\RequestEvent;
10
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
11
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
12
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
13
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
14
15
/**
16
 * Listener which checks whether there is a SAMLRequest pending from before login. If so, the listener redirects to the
17
 * SSO controller in order to send the SAMLResponse (and redirect the user to the requested service).
18
 */
19
class HandleSamlRequestSubscriber implements EventSubscriberInterface {
20
21
    public function __construct(private TokenStorageInterface $tokenStorage, private RequestStorageInterface $samlRequestStorage, private UrlGeneratorInterface $urlGenerator)
22
    {
23
    }
24
25
    public function onRequest(RequestEvent $event) {
26
        $request = $event->getRequest();
27 20
        $route = $request->get('_route');
28 20
29 20
        if(!$event->isMainRequest()) {
30 20
            // prevent loops
31 20
            return;
32
        }
33 18
34 18
        /** @var TokenInterface|null $token */
35 18
        $token = $this->tokenStorage->getToken();
36
37 18
        if($route === 'idp_saml') {
38
            $event->stopPropagation(); // stop other events from possibly redirecting
39 2
        }
40
41
        if($token === null || $token->getUser() === null || $token instanceof TwoFactorToken || $route === 'idp_saml' || $route === 'show_privacy_policy') {
42
            // prevent loops
43 17
            return;
44
        }
45 17
46 2
        if($this->samlRequestStorage->has() && $event->hasResponse() === false) {
47
            $response = new RedirectResponse($this->urlGenerator->generate('idp_saml'));
48
            $event->setResponse($response);
49 17
        }
50
    }
51 13
52
    public static function getSubscribedEvents(): array {
53
        return [
54 9
            RequestEvent::class => ['onRequest', -5]
55 2
        ];
56
    }
57
}