Completed
Push — master ( 27322f...ead5e5 )
by Yann
02:16
created

Manager/TokenManager.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\TokenUsedException;
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
47
     * @param TokenRepositoryInterface    $repository
48
     * @param InformationGuesserInterface $informationGuesser
49
     * @param UserManagerInterface        $userManager
50
     * @param EventDispatcher             $eventDispatcher
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 (TokenUsedException $exception) {
82 1
            $this->eventDispatcher->tokenUsed($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)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $at. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
112
    {
113
        @trigger_error(
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)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $at. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
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->isUsed()) {
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