Completed
Pull Request — master (#15)
by Yann
02:04
created

TokenManager::get()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 13
cts 13
cp 1
rs 8.9197
c 0
b 0
f 0
cc 4
eloc 14
nc 4
nop 2
crap 4
1
<?php
2
3
namespace Yokai\SecurityTokenBundle\Manager;
4
5
use DateTime;
6
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
7
use Yokai\SecurityTokenBundle\Entity\Token;
8
use Yokai\SecurityTokenBundle\Event\ConsumeTokenEvent;
9
use Yokai\SecurityTokenBundle\Event\CreateTokenEvent;
10
use Yokai\SecurityTokenBundle\Event\TokenConsumedEvent;
11
use Yokai\SecurityTokenBundle\Event\TokenCreatedEvent;
12
use Yokai\SecurityTokenBundle\Event\TokenExpiredEvent;
13
use Yokai\SecurityTokenBundle\Event\TokenNotFoundEvent;
14
use Yokai\SecurityTokenBundle\Event\TokenRetrievedEvent;
15
use Yokai\SecurityTokenBundle\Event\TokenTotallyConsumedEvent;
16
use Yokai\SecurityTokenBundle\Event\TokenUsedEvent;
17
use Yokai\SecurityTokenBundle\Exception\TokenExpiredException;
18
use Yokai\SecurityTokenBundle\Exception\TokenNotFoundException;
19
use Yokai\SecurityTokenBundle\Exception\TokenUsedException;
20
use Yokai\SecurityTokenBundle\Factory\TokenFactoryInterface;
21
use Yokai\SecurityTokenBundle\InformationGuesser\InformationGuesserInterface;
22
use Yokai\SecurityTokenBundle\Repository\TokenRepositoryInterface;
23
use Yokai\SecurityTokenBundle\TokenEvents;
24
25
/**
26
 * @author Yann Eugoné <[email protected]>
27
 */
28
class TokenManager implements TokenManagerInterface
29
{
30
    /**
31
     * @var TokenFactoryInterface
32
     */
33
    private $factory;
34
35
    /**
36
     * @var TokenRepositoryInterface
37
     */
38
    private $repository;
39
40
    /**
41
     * @var InformationGuesserInterface
42
     */
43
    private $informationGuesser;
44
45
    /**
46
     * @var UserManagerInterface
47
     */
48
    private $userManager;
49
50
    /**
51
     * @var EventDispatcherInterface
52
     */
53
    private $eventDispatcher;
54
55
    /**
56
     * @param TokenFactoryInterface       $factory
57
     * @param TokenRepositoryInterface    $repository
58
     * @param InformationGuesserInterface $informationGuesser
59
     * @param UserManagerInterface        $userManager
60
     * @param EventDispatcherInterface    $eventDispatcher
61
     */
62 7
    public function __construct(
63
        TokenFactoryInterface $factory,
64
        TokenRepositoryInterface $repository,
65
        InformationGuesserInterface $informationGuesser,
66
        UserManagerInterface $userManager,
67
        EventDispatcherInterface $eventDispatcher
68
    ) {
69 7
        $this->factory = $factory;
70 7
        $this->repository = $repository;
71 7
        $this->informationGuesser = $informationGuesser;
72 7
        $this->userManager = $userManager;
73 7
        $this->eventDispatcher = $eventDispatcher;
74 7
    }
75
76
    /**
77
     * @inheritdoc
78
     */
79 4
    public function get($purpose, $value)
80
    {
81
        try {
82 4
            $token = $this->repository->get($value, $purpose);
83 3
        } catch (TokenNotFoundException $exception) {
84 1
            $this->eventDispatcher->dispatch(TokenEvents::TOKEN_NOT_FOUND, new TokenNotFoundEvent($purpose, $value));
85
86 1
            throw $exception;
87 2
        } catch (TokenExpiredException $exception) {
88 1
            $this->eventDispatcher->dispatch(TokenEvents::TOKEN_EXPIRED, new TokenExpiredEvent($purpose, $value));
89
90 1
            throw $exception;
91 1
        } catch (TokenUsedException $exception) {
92 1
            $this->eventDispatcher->dispatch(TokenEvents::TOKEN_USED, new TokenUsedEvent($purpose, $value));
93
94 1
            throw $exception;
95
        }
96
97 1
        $this->eventDispatcher->dispatch(TokenEvents::TOKEN_RETRIEVED, new TokenRetrievedEvent($token));
98
99 1
        return $token;
100
    }
101
102
    /**
103
     * @inheritdoc
104
     */
105 1
    public function create($purpose, $user, array $payload = [])
106
    {
107 1
        $event = new CreateTokenEvent($purpose, $user, $payload);
108 1
        $this->eventDispatcher->dispatch(TokenEvents::CREATE_TOKEN, $event);
109
110 1
        $token = $this->factory->create($user, $purpose, $event->getPayload());
111
112 1
        $this->repository->create($token);
113
114 1
        $this->eventDispatcher->dispatch(TokenEvents::TOKEN_CREATED, new TokenCreatedEvent($token));
115
116 1
        return $token;
117
    }
118
119
    /**
120
     * @inheritdoc
121
     */
122
    public function setUsed(Token $token, DateTime $at = null)
123
    {
124
        @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...
125
            'The '.__METHOD__
126
            .' method is deprecated since version 2.2 and will be removed in 3.0. Use the consume() method instead.',
127
            E_USER_DEPRECATED
128
        );
129
130
        $this->consume($token, $at);
131
    }
132
133
    /**
134
     * @inheritDoc
135
     */
136 1
    public function consume(Token $token, DateTime $at = null)
137
    {
138 1
        $event = new ConsumeTokenEvent($token, $at, $this->informationGuesser->get());
139 1
        $this->eventDispatcher->dispatch(TokenEvents::CONSUME_TOKEN, $event);
140
141 1
        $token->consume($event->getInformation(), $at);
142
143 1
        $this->repository->update($token);
144
145 1
        $this->eventDispatcher->dispatch(TokenEvents::TOKEN_CONSUMED, new TokenConsumedEvent($token));
146 1
        if ($token->isUsed()) {
147 1
            $this->eventDispatcher->dispatch(
148 1
                TokenEvents::TOKEN_TOTALLY_CONSUMED,
149 1
                new TokenTotallyConsumedEvent($token)
150
            );
151
        }
152 1
    }
153
154
    /**
155
     * @inheritdoc
156
     */
157 1
    public function getUser(Token $token)
158
    {
159 1
        return $this->userManager->get($token->getUserClass(), $token->getUserId());
160
    }
161
}
162