TokenManager::get()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 22
c 0
b 0
f 0
ccs 13
cts 13
cp 1
rs 9.568
cc 4
nc 4
nop 2
crap 4
1
<?php
2
3
namespace Yokai\SecurityTokenBundle\Manager;
4
5
use DateTime;
6
use Yokai\SecurityTokenBundle\Entity\Token;
7
use Yokai\SecurityTokenBundle\EventDispatcher;
8
use Yokai\SecurityTokenBundle\Exception\TokenExpiredException;
9
use Yokai\SecurityTokenBundle\Exception\TokenNotFoundException;
10
use Yokai\SecurityTokenBundle\Exception\TokenConsumedException;
11
use Yokai\SecurityTokenBundle\Factory\TokenFactoryInterface;
12
use Yokai\SecurityTokenBundle\InformationGuesser\InformationGuesserInterface;
13
use Yokai\SecurityTokenBundle\Repository\TokenRepositoryInterface;
14
15
/**
16
 * @author Yann Eugoné <[email protected]>
17
 */
18
class TokenManager implements TokenManagerInterface
19
{
20
    /**
21
     * @var TokenFactoryInterface
22
     */
23
    private $factory;
24
25
    /**
26
     * @var TokenRepositoryInterface
27
     */
28
    private $repository;
29
30
    /**
31
     * @var InformationGuesserInterface
32
     */
33
    private $informationGuesser;
34
35
    /**
36
     * @var UserManagerInterface
37
     */
38
    private $userManager;
39
40
    /**
41
     * @var EventDispatcher
42
     */
43
    private $eventDispatcher;
44
45
    /**
46
     * @param TokenFactoryInterface       $factory            The token factory
47
     * @param TokenRepositoryInterface    $repository         The token repository
48
     * @param InformationGuesserInterface $informationGuesser The information guesser
49
     * @param UserManagerInterface        $userManager        The user manager
50
     * @param EventDispatcher             $eventDispatcher    The event dispatcher
51
     */
52 7
    public function __construct(
53
        TokenFactoryInterface $factory,
54
        TokenRepositoryInterface $repository,
55
        InformationGuesserInterface $informationGuesser,
56
        UserManagerInterface $userManager,
57
        EventDispatcher $eventDispatcher
58
    ) {
59 7
        $this->factory = $factory;
60 7
        $this->repository = $repository;
61 7
        $this->informationGuesser = $informationGuesser;
62 7
        $this->userManager = $userManager;
63 7
        $this->eventDispatcher = $eventDispatcher;
64 7
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69 4
    public function get($purpose, $value)
70
    {
71
        try {
72 4
            $token = $this->repository->get($value, $purpose);
73 3
        } catch (TokenNotFoundException $exception) {
74 1
            $this->eventDispatcher->tokenNotFound($purpose, $value);
75
76 1
            throw $exception;
77 2
        } catch (TokenExpiredException $exception) {
78 1
            $this->eventDispatcher->tokenExpired($purpose, $value);
79
80 1
            throw $exception;
81 1
        } catch (TokenConsumedException $exception) {
82 1
            $this->eventDispatcher->tokenAlreadyConsumed($purpose, $value);
83
84 1
            throw $exception;
85
        }
86
87 1
        $this->eventDispatcher->tokenRetrieved($token);
88
89 1
        return $token;
90
    }
91
92
    /**
93
     * @inheritdoc
94
     */
95 1
    public function create($purpose, $user, array $payload = [])
96
    {
97 1
        $event = $this->eventDispatcher->createToken($purpose, $user, $payload);
98
99 1
        $token = $this->factory->create($user, $purpose, $event->getPayload());
100
101 1
        $this->repository->create($token);
102
103 1
        $this->eventDispatcher->tokenCreated($token);
104
105 1
        return $token;
106
    }
107
108
    /**
109
     * @inheritdoc
110
     */
111
    public function setUsed(Token $token, DateTime $at = null)
112
    {
113
        @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
114
            'The '.__METHOD__
115
            .' method is deprecated since version 2.2 and will be removed in 3.0. Use the consume() method instead.',
116
            E_USER_DEPRECATED
117
        );
118
119
        $this->consume($token, $at);
120
    }
121
122
    /**
123
     * @inheritDoc
124
     */
125 1
    public function consume(Token $token, DateTime $at = null)
126
    {
127 1
        $event = $this->eventDispatcher->consumeToken($token, $at, $this->informationGuesser->get());
128
129 1
        $token->consume($event->getInformation(), $at);
130
131 1
        $this->repository->update($token);
132
133 1
        $this->eventDispatcher->tokenConsumed($token);
134 1
        if ($token->isConsumed()) {
135 1
            $this->eventDispatcher->tokenTotallyConsumed($token);
136
        }
137 1
    }
138
139
    /**
140
     * @inheritdoc
141
     */
142 1
    public function getUser(Token $token)
143
    {
144 1
        return $this->userManager->get($token->getUserClass(), $token->getUserId());
145
    }
146
}
147