Test Setup Failed
Pull Request — master (#83)
by
unknown
18:11
created

RefreshTokenManager::revokeTokens()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 4
nop 2
crap 12
1
<?php
2
3
/*
4
 * This file is part of the GesdinetJWTRefreshTokenBundle package.
5
 *
6
 * (c) Gesdinet <http://www.gesdinet.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Gesdinet\JWTRefreshTokenBundle\Doctrine;
13
14
use Doctrine\Common\Persistence\ObjectManager;
15
use Gesdinet\JWTRefreshTokenBundle\Model\RefreshTokenManager as BaseRefreshTokenManager;
16
use Gesdinet\JWTRefreshTokenBundle\Model\RefreshTokenInterface;
17
18
class RefreshTokenManager extends BaseRefreshTokenManager
19
{
20
    protected $objectManager;
21
    protected $class;
22
    protected $repository;
23
    protected $maxTokenByUser;
24
25
    /**
26
     * Constructor.
27
     *
28
     * @param ObjectManager $om
29
     * @param string        $class
30 7
     * @param string        $maxTokenByUser
31
     */
32 7
    public function __construct(ObjectManager $om, $class, $maxTokenByUser)
33 7
    {
34 7
        $this->objectManager = $om;
35 7
        $this->repository = $om->getRepository($class);
36 7
        $metadata = $om->getClassMetadata($class);
37
        $this->class = $metadata->getName();
38
        $this->maxTokenByUser = $maxTokenByUser;
39
    }
40
41
    /**
42
     * @param string $refreshToken
43 1
     *
44
     * @return RefreshTokenInterface
45 1
     */
46
    public function get($refreshToken)
47
    {
48
        return $this->repository->findOneBy(array('refreshToken' => $refreshToken));
49
    }
50
51
    /**
52
     * @param string $username
53 1
     *
54
     * @return RefreshTokenInterface
55 1
     */
56
    public function getLastFromUsername($username)
57
    {
58
        return $this->repository->findOneBy(array('username' => $username), array('valid' => 'DESC'));
0 ignored issues
show
Unused Code introduced by
The call to ObjectRepository::findOneBy() has too many arguments starting with array('valid' => 'DESC').

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
59
    }
60
61
    /**
62 1
     * @param RefreshTokenInterface $refreshToken
63
     * @param bool|true             $andFlush
64 1
     */
65
    public function save(RefreshTokenInterface $refreshToken, $andFlush = true)
66 1
    {
67 1
        $offset = $this->maxTokenByUser;
68 1
        if (!$andFlush)
69 1
            $offset--;
70
        $username = $refreshToken->getUsername();
71
        $this->objectManager->persist($refreshToken);
72
73
        if ($andFlush) {
74
            $this->objectManager->flush();
75 1
        }
76
77 1
        $tokens = $this->repository->findBy(['username' => $username], ['valid' => 'DESC'], 1000, $offset);
78
79 1
        $this->revokeTokens($tokens, $andFlush);
80 1
    }
81 1
82 1
    /**
83
     * @param RefreshTokenInterface $refreshToken
84
     * @param bool                  $andFlush
85
     */
86
    public function delete(RefreshTokenInterface $refreshToken, $andFlush = true)
87
    {
88
        $this->objectManager->remove($refreshToken);
89
90 1
        if ($andFlush) {
91
            $this->objectManager->flush();
92 1
        }
93
    }
94 1
95 1
    /**
96 1
     * @param \DateTime $datetime
97
     * @param bool      $andFlush
98 1
     *
99 1
     * @return RefreshTokenInterface[]
100 1
     */
101
    public function revokeAllInvalid($datetime = null, $andFlush = true)
102 1
    {
103
        $invalidTokens = $this->repository->findInvalid($datetime);
104
105
        return $this->revokeTokens($invalidTokens, $andFlush);
106
    }
107
108
    /**
109
    * @param RefreshTokenInterface[] $tokens
110 1
    * @param bool                    $andFlush
111
    *
112 1
    * @return RefreshTokenInterface[]
113
    */
114
    public function revokeTokens($tokens, $andFlush)
115
    {
116
        foreach ($tokens as $token) {
117
            $this->objectManager->remove($token);
118
        }
119
120
        if ($andFlush) {
121
            $this->objectManager->flush();
122
        }
123
124
        return $tokens;
125
    }
126
127
    /**
128
     * @param string $username
129
     * @param bool   $andFlush
130
     *
131
     * @return RefreshTokenInterface[]
132
     */
133
    public function revokeAllTokenByUsername($username, $andFlush = true)
134
    {
135
        $tokens = $this->repository->findBy(['username' => $username]);
136
137
        return $this->revokeTokens($tokens, $andFlush);
138
    }
139
140
    /**
141
     * Returns the RefreshToken fully qualified class name.
142
     *
143
     * @return string
144
     */
145
    public function getClass()
146
    {
147
        return $this->class;
148
    }
149
}
150