Completed
Pull Request — master (#10)
by Jan
06:46 queued 04:52
created

TokenManager::removeToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace TreeHouse\KeystoneBundle\Manager;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use Doctrine\Common\Persistence\ObjectRepository;
7
use Symfony\Component\Security\Core\User\UserInterface;
8
use TreeHouse\KeystoneBundle\Entity\Token;
9
use TreeHouse\KeystoneBundle\Security\Encoder\TokenEncoder;
10
11
class TokenManager
12
{
13
    /**
14
     * @var TokenEncoder
15
     */
16
    protected $encoder;
17
18
    /**
19
     * @var ManagerRegistry
20
     */
21
    protected $doctrine;
22
23
    /**
24
     * @param TokenEncoder    $encoder
25
     * @param ManagerRegistry $doctrine
26
     */
27 22
    public function __construct(TokenEncoder $encoder, ManagerRegistry $doctrine)
28
    {
29 22
        $this->encoder = $encoder;
30 22
        $this->doctrine = $doctrine;
31 22
    }
32
33
    /**
34
     * Returns a token instance.
35
     *
36
     * @param UserInterface $user
37
     * @param int           $ttl
38
     *
39
     * @return Token
40
     */
41 8
    public function createToken($user, $ttl = 3600)
42
    {
43 8
        $expires = time() + (int) $ttl;
44
45 8
        $hash = $this->getEncoder()->generateTokenValue(
46 4
            get_class($user),
47 8
            $user->getUsername(),
48 8
            $user->getPassword(),
49
            $expires
50 4
        );
51
52 8
        $token = new Token();
53 8
        $token->setHash($hash);
54 8
        $token->setExpiresAt(new \DateTime('@' . $expires));
55
56 8
        $this->updateToken($token);
57
58 8
        return $token;
59
    }
60
61
    /**
62
     * @param $criteria
63
     *
64
     * @return Token|null
65
     */
66
    public function findOneBy($criteria)
67
    {
68
        return $this->getRepository()->findOneBy($criteria);
69
    }
70
71
    /**
72
     * Finds a token by id (which is itself a token).
73
     *
74
     * @param string $token
75
     *
76
     * @return Token|null
77
     */
78 6
    public function findById($token)
79
    {
80 6
        return $this->getRepository()->find($token);
81
    }
82
83
    /**
84
     * Updates a Token.
85
     *
86
     * @param Token   $token
87
     * @param Boolean $andFlush Whether to flush the changes (default true)
88
     */
89 8
    public function updateToken(Token $token, $andFlush = true)
90
    {
91 8
        $this->doctrine->getManager()->persist($token);
92 8
        if ($andFlush) {
93 8
            $this->doctrine->getManager()->flush();
94 4
        }
95 8
    }
96
97
    /**
98
     * Removes a Token.
99
     *
100
     * @param Token $token
101
     */
102
    public function removeToken(Token $token)
103
    {
104
        $this->doctrine->getManager()->remove($token);
105
        $this->doctrine->getManager()->flush();
106
    }
107
108
    /**
109
     * @param Token $token
110
     *
111
     * @return bool
112
     */
113 6
    public function isExpired(Token $token)
114
    {
115 6
        return new \DateTime() > $token->getExpiresAt();
116
    }
117
118
    /**
119
     * @return TokenEncoder
120
     */
121 8
    public function getEncoder()
122
    {
123 8
        return $this->encoder;
124
    }
125
126
    /**
127
     * @return ObjectRepository
128
     */
129 6
    protected function getRepository()
130
    {
131 6
        return $this->doctrine->getRepository('TreeHouseKeystoneBundle:Token');
132
    }
133
}
134