Completed
Push — master ( d3bb33...3bd08a )
by Janusz
132:58 queued 127:53
created

TwilioProvider::prepareAuthentication()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4.128

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 5
c 2
b 0
f 1
dl 0
loc 11
ccs 4
cts 5
cp 0.8
rs 10
cc 4
nc 3
nop 1
crap 4.128
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\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface;
11
use Symfony\Component\EventDispatcher\GenericEvent;
12
use Symfony\Component\HttpFoundation\Session\SessionInterface;
13
14
class TwilioProvider implements TwoFactorProviderInterface
15
{
16
    /** @var EventDispatcherInterface */
17
    private $dispatcher;
18
19
    /** @var SessionInterface */
20
    private $session;
21
22
    /** @var TwoFactorFormRendererInterface */
23
    private $renderer;
24
25
    /**
26
     * TwilioProvider constructor.
27
     *
28
     * @param EventDispatcherInterface $dispatcher
29
     * @param SessionInterface $session
30
     * @param TwoFactorFormRendererInterface $renderer
31
     */
32 4
    public function __construct(
33
        EventDispatcherInterface $dispatcher,
34
        SessionInterface $session,
35
        TwoFactorFormRendererInterface $renderer
36
    )
37
    {
38 4
        $this->dispatcher = $dispatcher;
39 4
        $this->session = $session;
40 4
        $this->renderer = $renderer;
41 4
    }
42
43 1
    public function beginAuthentication(AuthenticationContextInterface $context): bool
44
    {
45 1
        $user = $context->getUser();
46 1
        return $user instanceof Twilio\TwoFactorInterface && $user->isTwilioAuthEnabled();
47
    }
48
49 1
    public function prepareAuthentication($user): void
50
    {
51 1
        if ($user instanceof Twilio\TwoFactorInterface && $user->isTwilioAuthEnabled())
52
        {
53 1
            if( $this->dispatcher instanceof ContractsEventDispatcherInterface)
0 ignored issues
show
introduced by
$this->dispatcher is always a sub-type of Symfony\Contracts\EventD...ventDispatcherInterface.
Loading history...
54
            {
55 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

55
                $this->dispatcher->/** @scrutinizer ignore-call */ 
56
                                   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...
56
            }
57
            else
58
            {
59
                $this->dispatcher->dispatch('2fa.twilio.start', new GenericEvent($user));
60
            }
61
        }
62 1
    }
63
64 1
    public function validateAuthenticationCode($user, string $authenticationCode): bool
65
    {
66 1
        if ( ! $user instanceof Twilio\TwoFactorInterface || ! $this->session->has('twilio_code'))
67
        {
68 1
            return false;
69
        }
70
71 1
        if(hash_equals($this->session->get('twilio_code'), $authenticationCode))
72
        {
73 1
            $this->session->remove('twilio_code');
74 1
            return true;
75
        }
76
77 1
        return false;
78
    }
79
80 1
    public function getFormRenderer(): TwoFactorFormRendererInterface
81
    {
82 1
        return $this->renderer;
83
    }
84
}
85