Completed
Push — develop ( dccd3f...462ea3 )
by Janusz
09:26
created

TwilioProvider::beginAuthentication()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 2
Metric Value
eloc 2
c 3
b 0
f 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace FlyingColours\TwilioTwoFactorBundle\Security\TwoFactor\Provider;
4
5
use Scheb\TwoFactorBundle\Security\TwoFactor\AuthenticationContextInterface;
6
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\TwoFactorFormRendererInterface;
7
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\TwoFactorProviderInterface;
8
use FlyingColours\TwilioTwoFactorBundle\Model\Twilio;
9
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
10
use Symfony\Component\EventDispatcher\GenericEvent;
11
use Symfony\Component\HttpFoundation\Session\SessionInterface;
12
13
class TwilioProvider implements TwoFactorProviderInterface
14
{
15
    /** @var EventDispatcherInterface */
16
    private $dispatcher;
17
18
    /** @var SessionInterface */
19
    private $session;
20
21
    /** @var TwoFactorFormRendererInterface */
22
    private $renderer;
23
24
    /**
25
     * TwilioProvider constructor.
26
     *
27
     * @param EventDispatcherInterface $dispatcher
28
     * @param SessionInterface $session
29
     * @param TwoFactorFormRendererInterface $renderer
30
     */
31 4
    public function __construct(
32
        EventDispatcherInterface $dispatcher,
33
        SessionInterface $session,
34
        TwoFactorFormRendererInterface $renderer
35
    )
36
    {
37 4
        $this->dispatcher = $dispatcher;
38 4
        $this->session = $session;
39 4
        $this->renderer = $renderer;
40 4
    }
41
42 1
    public function beginAuthentication(AuthenticationContextInterface $context): bool
43
    {
44 1
        $user = $context->getUser();
45 1
        return $user instanceof Twilio\TwoFactorInterface && $user->isTwilioAuthEnabled();
46
    }
47
48 1
    public function prepareAuthentication($user): void
49
    {
50 1
        if ($user instanceof Twilio\TwoFactorInterface && $user->isTwilioAuthEnabled())
51
        {
52 1
            $this->dispatcher->dispatch(new GenericEvent($user), '2fa.twilio.start');
0 ignored issues
show
Unused Code introduced by
The call to Symfony\Contracts\EventD...erInterface::dispatch() has too many arguments starting with '2fa.twilio.start'. ( Ignorable by Annotation )

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

52
            $this->dispatcher->/** @scrutinizer ignore-call */ 
53
                               dispatch(new GenericEvent($user), '2fa.twilio.start');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
53
        }
54 1
    }
55
56 1
    public function validateAuthenticationCode($user, string $authenticationCode): bool
57
    {
58 1
        if ( ! $user instanceof Twilio\TwoFactorInterface || ! $this->session->has('twilio_code'))
59
        {
60 1
            return false;
61
        }
62
63 1
        if(hash_equals($this->session->get('twilio_code'), $authenticationCode))
64
        {
65 1
            $this->session->remove('twilio_code');
66 1
            return true;
67
        }
68
69 1
        return false;
70
    }
71
72 1
    public function getFormRenderer(): TwoFactorFormRendererInterface
73
    {
74 1
        return $this->renderer;
75
    }
76
}
77