Completed
Branch develop (656ecc)
by Janusz
10:43 queued 06:18
created

TriggerController::defaultAction()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 12
nc 3
nop 1
1
<?php
2
3
namespace FlyingColours\TwilioTwoFactorBundle\Controller;
4
5
use FlyingColours\TwilioTwoFactorBundle\Model\Twilio\TwoFactorInterface;
6
use Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorToken;
7
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
8
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
9
use Symfony\Component\EventDispatcher\GenericEvent;
10
use Symfony\Component\Form\FormInterface;
11
use Symfony\Component\HttpFoundation\RedirectResponse;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
14
use Symfony\Component\Routing\RouterInterface;
15
16
class TriggerController
17
{
18
    /** @var RouterInterface */
19
    private $router;
20
21
    /** @var EventDispatcherInterface */
22
    private $dispatcher;
23
24
    /** @var EngineInterface */
25
    private $templating;
26
27
    /** @var FormInterface */
28
    private $form;
29
30
    /** @var TwoFactorToken */
31
    private $token;
32
33
    /**
34
     * TriggerController constructor.
35
     *
36
     * @param RouterInterface $router
37
     * @param EventDispatcherInterface $dispatcher
38
     * @param EngineInterface $templating
39
     * @param FormInterface $form
40
     * @param TwoFactorToken $token
41
     */
42
    public function __construct(
43
        RouterInterface $router,
44
        EventDispatcherInterface $dispatcher,
45
        EngineInterface $templating,
46
        FormInterface $form,
47
        TwoFactorToken $token
48
    )
49
    {
50
        $this->router = $router;
51
        $this->dispatcher = $dispatcher;
52
        $this->templating = $templating;
53
        $this->form = $form;
54
        $this->token = $token;
55
    }
56
57
    public function defaultAction(Request $request)
58
    {
59
        $this->form->setData($this->token->getUser());
60
61
        $this->form->handleRequest($request);
62
63
        if ($this->form->isValid())
64
        {
65
            /** @var TwoFactorInterface $user */
66
            $user = $this->form->getData();
67
68
            $event = $this->dispatcher->dispatch('twilio.auth.triggered', new GenericEvent($user));
69
70
            $route = $event->hasArgument('route')
0 ignored issues
show
Bug introduced by
The method hasArgument() does not exist on Symfony\Component\EventDispatcher\Event. It seems like you code against a sub-type of Symfony\Component\EventDispatcher\Event such as Symfony\Component\EventDispatcher\GenericEvent. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
            $route = $event->/** @scrutinizer ignore-call */ hasArgument('route')
Loading history...
71
                ? $event->getArgument('route')
0 ignored issues
show
Bug introduced by
The method getArgument() does not exist on Symfony\Component\EventDispatcher\Event. It seems like you code against a sub-type of Symfony\Component\EventDispatcher\Event such as Symfony\Component\EventDispatcher\GenericEvent. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
                ? $event->/** @scrutinizer ignore-call */ getArgument('route')
Loading history...
72
                : '2fa_login'
73
            ;
74
75
            return new RedirectResponse($this->router->generate($route));
76
        }
77
78
        return $this->templating->renderResponse(
79
            '@FlyingColoursTwilioTwoFactor/trigger/default.html.twig',
80
            [ 'form' => $this->form->createView() ]
81
        );
82
    }
83
}
84