Completed
Push — master ( a2ccb4...2afc29 )
by Philip
05:08
created

Service/AccessTokenService.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
class AccessTokenService implements AccessTokenServiceInterface
13
{
14
    /**
15
     * @var AccessTokenRepositoryInterface
16
     */
17
    private $accessTokenRepository;
18
19
    /**
20
     * @var string
21
     */
22
    private $accessTokenClass;
23
24
    /**
25
     * @var string
26
     */
27
    private $defaultExpirationDuration = '+1 month';
28
29
    /**
30
     * @var AuthenticationProviderInterface
31
     */
32
    private $authenticationManager;
33
34
    /**
35
     * @var string
36
     */
37
    private $authenticationProviderKey;
38
39
    public function __construct(
40
        AccessTokenRepositoryInterface $accessTokenRepository,
41
        $accessTokenClass,
42
        AuthenticationManagerInterface $authenticationManager,
43
        $authenticationProviderKey
44
    ) {
45
        $this->accessTokenRepository = $accessTokenRepository;
46
        $this->accessTokenClass = $accessTokenClass;
47
        $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...
48
        $this->authenticationProviderKey = $authenticationProviderKey;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function createAccessToken(string $username, string $password): AccessToken
55
    {
56
        $usernamePasswordToken = new UsernamePasswordToken($username, $password, $this->authenticationProviderKey);
57
        $token = $this->authenticationManager->authenticate($usernamePasswordToken);
58
        $accessToken = $this->createAccessTokenForUser($token->getUser());
59
60
        return $accessToken;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function findUserByToken(string $token): ?UserInterface
67
    {
68
        return $this->accessTokenRepository->findUserByToken($token);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function listByUser(UserInterface $user): array
75
    {
76
        return $this->accessTokenRepository->findBy(['user' => $user]);
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function createAccessTokenForUser(UserInterface $user): AccessToken
83
    {
84
        $token = bin2hex(random_bytes(32));
85
        /** @var AccessToken $accessToken */
86
        $accessToken = new $this->accessTokenClass;
87
        $accessToken->setToken($token);
88
        $accessToken->setUser($user);
89
        $accessToken->setExpiry(new \DateTime($this->defaultExpirationDuration));
90
91
        $accessToken = $this->accessTokenRepository->persist($accessToken);
0 ignored issues
show
The method persist() does not seem to exist on object<Dontdrinkandroot\...kenRepositoryInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
92
93
        return $accessToken;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function cleanUpExpiredTokens(): int
100
    {
101
        $accessTokens = $this->accessTokenRepository->findExpiredTokens();
102
        foreach ($accessTokens as $accessToken) {
103
            $this->accessTokenRepository->remove($accessToken);
0 ignored issues
show
The method remove() does not seem to exist on object<Dontdrinkandroot\...kenRepositoryInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
104
        }
105
106
        return count($accessTokens);
107
    }
108
109
    /**
110
     * @return string
111
     */
112
    public function getDefaultExpirationDuration()
113
    {
114
        return $this->defaultExpirationDuration;
115
    }
116
117
    /**
118
     * @param string $defaultExpirationDuration
119
     */
120
    public function setDefaultExpirationDuration($defaultExpirationDuration)
121
    {
122
        $this->defaultExpirationDuration = $defaultExpirationDuration;
123
    }
124
}
125