Completed
Pull Request — master (#10)
by Jan
14:59
created

TokenManager::removeToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 2
cts 2
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
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 18
    public function __construct(TokenEncoder $encoder, ManagerRegistry $doctrine)
28
    {
29 18
        $this->encoder = $encoder;
30 18
        $this->doctrine = $doctrine;
31 18
    }
32
33
    /**
34
     * Returns a token instance.
35
     *
36
     * @param UserInterface $user
37
     * @param int           $ttl
38
     *
39
     * @return Token
40
     */
41 6
    public function createToken($user, $ttl = 3600)
42
    {
43 6
        $expires = time() + (int) $ttl;
44
45 6
        $hash = $this->getEncoder()->generateTokenValue(
46 3
            get_class($user),
47 6
            $user->getUsername(),
48 6
            $user->getPassword(),
49
            $expires
50 3
        );
51
52 6
        $token = new Token();
53 6
        $token->setHash($hash);
54 6
        $token->setExpiresAt(new \DateTime('@' . $expires));
55
56 6
        $this->updateToken($token);
57
58 6
        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 6
    public function updateToken(Token $token, $andFlush = true)
90
    {
91 6
        $this->doctrine->getManager()->persist($token);
92 6
        if ($andFlush) {
93 6
            $this->doctrine->getManager()->flush();
94 3
        }
95 6
    }
96
97
    /**
98
     * Removes a Token.
99
     *
100
     * @param Token $token
101
     */
102 4
    public function removeToken(Token $token)
103
    {
104 4
        $this->doctrine->getManager()->remove($token);
105
        $this->doctrine->getManager()->flush();
106
    }
107
108
    /**
109
     * @param Token $token
110 6
     *
111
     * @return bool
112 6
     */
113
    public function isExpired(Token $token)
114
    {
115
        return new \DateTime() > $token->getExpiresAt();
116
    }
117
118 6
    /**
119
     * @return TokenEncoder
120 6
     */
121
    public function getEncoder()
122
    {
123
        return $this->encoder;
124
    }
125
126
    /**
127
     * @return ObjectRepository
128
     */
129
    protected function getRepository()
130
    {
131
        return $this->doctrine->getRepository('TreeHouseKeystoneBundle:Token');
132
    }
133
}
134