SmsAuthenticator::authenticate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4286
cc 2
eloc 7
nc 2
nop 1
crap 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