SmsAuthenticator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 5
c 3
b 0
f 1
lcom 1
cbo 3
dl 0
loc 32
ccs 14
cts 14
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A supports() 0 5 2
A authenticate() 0 11 2
1
<?php
2
3
namespace Apiary\SmsLoginProvider;
4
5
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
6
use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
7
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
8
use Symfony\Component\Security\Core\Exception\AuthenticationException;
0 ignored issues
show
Coding Style introduced by
As per PSR2, there should be exactly one blank line after the last USE statement, 2 were found though.
Loading history...
9
10
11
class SmsAuthenticator implements AuthenticationProviderInterface
12
{
13
14
    private $code;
15
    private $logger;
16
    private $name;
17
18 18
    public function __construct($name, $code, $logger)
19
    {
20 18
        $this->code = $code;
21 18
        $this->logger = $logger;
22 18
        $this->name = $name;
23 18
    }
24
25 3
    public function supports(TokenInterface $token)
26
    {
27
        return $token instanceof UsernamePasswordToken
28 3
        && $token->getProviderKey() === $this->name;
29
    }
30
31 6
    public function authenticate(TokenInterface $token)
32
    {
33 6
        if ($this->code != $token->getCredentials()) {
34 3
            throw new AuthenticationException("Authentication code does not match");
35
        }
36 3
        $user = $token->getUser();
37
        // TODO: Provide a mechanism to get the real user object, and set user roles in token
38 3
        $token = new UsernamePasswordToken($user, $token->getCredentials(),
39 3
          $this->name, ['ROLE_USER']);
40 3
        return $token;
41
    }
42
}
43