Completed
Push — master ( 2afc29...29656f )
by Philip
06:07
created

AccessTokenService::remove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Dontdrinkandroot\RestBundle\Service;
4
5
use Dontdrinkandroot\RestBundle\Entity\AccessToken;
6
use Dontdrinkandroot\RestBundle\Repository\AccessTokenRepositoryInterface;
7
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
8
use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
9
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
10
use Symfony\Component\Security\Core\User\UserInterface;
11
12
/**
13
 * @author Philip Washington Sorst <[email protected]>
14
 */
15
class AccessTokenService implements AccessTokenServiceInterface
16
{
17
    /**
18
     * @var AccessTokenRepositoryInterface
19
     */
20
    private $accessTokenRepository;
21
22
    /**
23
     * @var string
24
     */
25
    private $accessTokenClass;
26
27
    /**
28
     * @var string
29
     */
30
    private $defaultExpirationDuration = '+1 month';
31
32
    /**
33
     * @var AuthenticationProviderInterface
34
     */
35
    private $authenticationManager;
36
37
    /**
38
     * @var string
39
     */
40
    private $authenticationProviderKey;
41
42 12
    public function __construct(
43
        AccessTokenRepositoryInterface $accessTokenRepository,
44
        $accessTokenClass,
45
        AuthenticationManagerInterface $authenticationManager,
46
        $authenticationProviderKey
47
    ) {
48 12
        $this->accessTokenRepository = $accessTokenRepository;
49 12
        $this->accessTokenClass = $accessTokenClass;
50 12
        $this->authenticationManager = $authenticationManager;
0 ignored issues
show
Documentation Bug introduced by
$authenticationManager is of type object<Symfony\Component...cationManagerInterface>, but the property $authenticationManager was declared to be of type object<Symfony\Component...ationProviderInterface>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
51 12
        $this->authenticationProviderKey = $authenticationProviderKey;
52 12
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 4
    public function findByToken(string $token): ?AccessToken
58
    {
59 4
        return $this->accessTokenRepository->findOneBy(['token' => $token]);
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 2
    public function createAccessToken(string $username, string $password): AccessToken
66
    {
67 2
        $usernamePasswordToken = new UsernamePasswordToken($username, $password, $this->authenticationProviderKey);
68 2
        $token = $this->authenticationManager->authenticate($usernamePasswordToken);
69 2
        $accessToken = $this->createAccessTokenForUser($token->getUser());
70
71 2
        return $accessToken;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function findUserByToken(string $token): ?UserInterface
78
    {
79
        return $this->accessTokenRepository->findUserByToken($token);
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 2
    public function listByUser(UserInterface $user): array
86
    {
87 2
        return $this->accessTokenRepository->findBy(['user' => $user]);
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 2
    public function createAccessTokenForUser(UserInterface $user): AccessToken
94
    {
95 2
        $token = bin2hex(random_bytes(32));
96
        /** @var AccessToken $accessToken */
97 2
        $accessToken = new $this->accessTokenClass;
98 2
        $accessToken->setToken($token);
99 2
        $accessToken->setUser($user);
100 2
        $accessToken->setExpiry(new \DateTime($this->defaultExpirationDuration));
101
102 2
        $accessToken = $this->accessTokenRepository->persist($accessToken);
103
104 2
        return $accessToken;
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function cleanUpExpiredTokens(): int
111
    {
112
        $accessTokens = $this->accessTokenRepository->findExpiredTokens();
113
        foreach ($accessTokens as $accessToken) {
114
            $this->accessTokenRepository->remove($accessToken);
115
        }
116
117
        return count($accessTokens);
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 2
    public function remove(AccessToken $accessToken)
124
    {
125 2
        $this->accessTokenRepository->remove($accessToken);
126 2
    }
127
128
    /**
129
     * @return string
130
     */
131
    public function getDefaultExpirationDuration()
132
    {
133
        return $this->defaultExpirationDuration;
134
    }
135
136
    /**
137
     * @param string $defaultExpirationDuration
138
     */
139
    public function setDefaultExpirationDuration($defaultExpirationDuration)
140
    {
141
        $this->defaultExpirationDuration = $defaultExpirationDuration;
142
    }
143
}
144