AccessTokenManager   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 5
dl 0
loc 158
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getClass() 0 4 1
A findAccessTokenBy() 0 4 1
A findBy() 0 4 1
A findAlmostExpiredTokens() 0 14 1
A deleteAccessToken() 0 5 1
A hasAccessToken() 0 4 1
A getAccessToken() 0 16 3
A setAccessToken() 0 4 1
A updateAccessToken() 0 12 1
A getDoctrine() 0 4 1
A getSession() 0 4 1
1
<?php
2
3
namespace Mailxpert\APIBundle\Entity;
4
5
use Doctrine\Bundle\DoctrineBundle\Registry;
6
use Mailxpert\APIBundle\Exceptions\MailxpertAPIBundleException;
7
use Mailxpert\APIBundle\Model\AccessTokenInterface;
8
use Mailxpert\APIBundle\Model\AccessTokenManager as BaseAccessTokenManager;
9
use Mailxpert\Authentication\AccessToken as SDKAccessToken;
10
use Symfony\Component\HttpFoundation\Session\Session;
11
12
class AccessTokenManager extends BaseAccessTokenManager
13
{
14
    /**
15
     * @var string $class The AccessToken class
16
     */
17
    private $class;
18
19
    /**
20
     * @var Registry
21
     */
22
    private $doctrine;
23
    /**
24
     * @var Session
25
     */
26
    private $session;
27
28
    /**
29
     * AccessTokenManager constructor.
30
     *
31
     * @param Registry $doctrine
32
     * @param Session  $session
33
     * @param string   $class
34
     */
35
    public function __construct(Registry $doctrine, Session $session, $class)
36
    {
37
        $this->doctrine = $doctrine;
38
        $this->session = $session;
39
        $this->class = $class;
40
    }
41
42
    /**
43
     * @return string
44
     */
45
    public function getClass()
46
    {
47
        return $this->class;
48
    }
49
50
    /**
51
     * @param array $criteria
52
     *
53
     * @return AccessTokenInterface|null
54
     */
55
    public function findAccessTokenBy(array $criteria)
56
    {
57
        return $this->getDoctrine()->getRepository($this->getClass())->findOneBy($criteria);
58
    }
59
60
    /**
61
     * @param array $criteria
62
     *
63
     * @return AccessTokenInterface[]|null
64
     */
65
    public function findBy(array $criteria)
66
    {
67
        return $this->getDoctrine()->getRepository($this->getClass())->findBy($criteria);
68
    }
69
70
    /**
71
     * @param int $time
72
     *
73
     * @return AccessTokenInterface[]|null
74
     */
75
    public function findAlmostExpiredTokens($time = 604800)
76
    {
77
        $repository = $this->getDoctrine()->getRepository($this->getClass());
78
79
        $query = $repository->createQueryBuilder('a')
80
            ->where('a.refreshTokenExpireAt > :expired')
81
            ->setParameter('expired', time())
82
            ->andWhere('a.refreshTokenExpireAt < :expiration')
83
            ->setParameter('expiration', time() + $time)
84
            ->getQuery()
85
        ;
86
87
        return $query->getResult();
88
    }
89
90
    /**
91
     * @param AccessTokenInterface $accessToken
92
     */
93
    public function deleteAccessToken(AccessTokenInterface $accessToken)
94
    {
95
        $this->getDoctrine()->getManager()->remove($accessToken);
96
        $this->getDoctrine()->getManager()->flush();
97
    }
98
99
    public function hasAccessToken()
100
    {
101
        return (bool) false !== $this->getSession()->get('mailxpert/apibundle/access_token_id', false);
102
    }
103
104
    /**
105
     * @throws MailxpertAPIBundleException
106
     *
107
     * @return AccessTokenInterface
108
     */
109
    public function getAccessToken()
110
    {
111
        $accessTokenId = $this->getSession()->get('mailxpert/apibundle/access_token_id');
112
113
        if (!$accessTokenId) {
114
            throw new MailxpertAPIBundleException('No access token ID stored in the session');
115
        }
116
117
        $accessToken = $this->findAccessTokenBy(['id' => $accessTokenId]);
118
119
        if (!$accessToken) {
120
            throw new MailxpertAPIBundleException(sprintf('No access token found with ID %s', $accessTokenId));
121
        }
122
123
        return $accessToken;
124
    }
125
126
    /**
127
     * @param AccessTokenInterface $accessToken
128
     */
129
    public function setAccessToken(AccessTokenInterface $accessToken)
130
    {
131
        $this->getSession()->set('mailxpert/apibundle/access_token_id', $accessToken->getId());
132
    }
133
134
    /**
135
     * @param AccessTokenInterface $accessToken
136
     * @param SDKAccessToken       $SDKAccessToken
137
     *
138
     * @return AccessTokenInterface
139
     */
140
    public function updateAccessToken(AccessTokenInterface $accessToken, SDKAccessToken $SDKAccessToken)
141
    {
142
        $accessToken->setAccessToken($SDKAccessToken->getAccessToken());
143
        $accessToken->setExpireAt($SDKAccessToken->getExpiresAt());
144
        $accessToken->setRefreshToken($SDKAccessToken->getRefreshToken());
145
        $accessToken->setRefreshTokenExpireAt($SDKAccessToken->getRefreshTokenExpiresAt());
146
        $accessToken->setScope($SDKAccessToken->getScope());
147
148
        $this->getDoctrine()->getManager()->flush();
149
150
        return $accessToken;
151
    }
152
153
154
    /**
155
     * @return Registry
156
     */
157
    private function getDoctrine()
158
    {
159
        return $this->doctrine;
160
    }
161
162
    /**
163
     * @return Session
164
     */
165
    private function getSession()
166
    {
167
        return $this->session;
168
    }
169
}