Completed
Push — master ( b0a6d2...faed70 )
by Philip
09:15
created

AccessTokenService::createAccessToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 6
cts 7
cp 0.8571
rs 9.4285
cc 2
eloc 7
nc 2
nop 2
crap 2.0116
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\Token\UsernamePasswordToken;
9
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
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 AuthenticationManagerInterface
34
     */
35
    private $authenticationManager;
36
37
    /**
38
     * @var string
39
     */
40
    private $authenticationProviderKey;
41
42 18
    public function __construct(
43
        AccessTokenRepositoryInterface $accessTokenRepository,
44
        $accessTokenClass,
45
        AuthenticationManagerInterface $authenticationManager,
46
        $authenticationProviderKey
47
    ) {
48 18
        $this->accessTokenRepository = $accessTokenRepository;
49 18
        $this->accessTokenClass = $accessTokenClass;
50 18
        $this->authenticationManager = $authenticationManager;
51 18
        $this->authenticationProviderKey = $authenticationProviderKey;
52 18
    }
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
        if (null === $username) {
68
            throw new BadCredentialsException();
69
        }
70
71 2
        $usernamePasswordToken = new UsernamePasswordToken($username, $password, $this->authenticationProviderKey);
72 2
        $token = $this->authenticationManager->authenticate($usernamePasswordToken);
73 2
        $accessToken = $this->createAccessTokenForUser($token->getUser());
74
75 2
        return $accessToken;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 2
    public function findUserByToken(string $token): ?UserInterface
82
    {
83 2
        return $this->accessTokenRepository->findUserByToken($token);
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 6
    public function listByUser(UserInterface $user): array
90
    {
91 6
        return $this->accessTokenRepository->findBy(['user' => $user]);
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 2
    public function createAccessTokenForUser(UserInterface $user): AccessToken
98
    {
99 2
        $token = bin2hex(random_bytes(32));
100
        /** @var AccessToken $accessToken */
101 2
        $accessToken = new $this->accessTokenClass;
102 2
        $accessToken->setToken($token);
103 2
        $accessToken->setUser($user);
104 2
        $accessToken->setExpiry(new \DateTime($this->defaultExpirationDuration));
105
106 2
        $accessToken = $this->accessTokenRepository->persist($accessToken);
107
108 2
        return $accessToken;
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 4
    public function cleanUpExpiredTokens(): int
115
    {
116 4
        $accessTokens = $this->accessTokenRepository->findExpiredTokens();
117 4
        foreach ($accessTokens as $accessToken) {
118 4
            $this->accessTokenRepository->remove($accessToken);
119
        }
120
121 4
        return count($accessTokens);
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127 2
    public function remove(AccessToken $accessToken)
128
    {
129 2
        $this->accessTokenRepository->remove($accessToken);
130 2
    }
131
132
    /**
133
     * @return string
134
     */
135
    public function getDefaultExpirationDuration()
136
    {
137
        return $this->defaultExpirationDuration;
138
    }
139
140
    /**
141
     * @param string $defaultExpirationDuration
142
     */
143
    public function setDefaultExpirationDuration($defaultExpirationDuration)
144
    {
145
        $this->defaultExpirationDuration = $defaultExpirationDuration;
146
    }
147
}
148