forked from yokai-php/security-token-bundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventDispatcher.php
194 lines (170 loc) · 4.7 KB
/
EventDispatcher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
namespace Yokai\SecurityTokenBundle;
use DateTime;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Yokai\SecurityTokenBundle\Entity\Token;
use Yokai\SecurityTokenBundle\Event\ConsumeTokenEvent;
use Yokai\SecurityTokenBundle\Event\CreateTokenEvent;
use Yokai\SecurityTokenBundle\Event\TokenAlreadyConsumedEvent;
use Yokai\SecurityTokenBundle\Event\TokenConsumedEvent;
use Yokai\SecurityTokenBundle\Event\TokenCreatedEvent;
use Yokai\SecurityTokenBundle\Event\TokenExpiredEvent;
use Yokai\SecurityTokenBundle\Event\TokenNotFoundEvent;
use Yokai\SecurityTokenBundle\Event\TokenRetrievedEvent;
use Yokai\SecurityTokenBundle\Event\TokenTotallyConsumedEvent;
use Yokai\SecurityTokenBundle\Event\TokenUsedEvent;
/**
* @author Yann Eugoné <eugone.yann@gmail.com>
*/
class EventDispatcher
{
/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;
/**
* @param EventDispatcherInterface $eventDispatcher
*/
public function __construct(EventDispatcherInterface $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}
/**
* @param string $purpose
* @param string $value
* @param array $payload
*
* @return CreateTokenEvent
*/
public function createToken($purpose, $value, array $payload)
{
$this->eventDispatcher->dispatch(
TokenEvents::CREATE_TOKEN,
$event = new CreateTokenEvent($purpose, $value, $payload)
);
return $event;
}
/**
* @param Token $token
*
* @return TokenCreatedEvent
*/
public function tokenCreated(Token $token)
{
$this->eventDispatcher->dispatch(
TokenEvents::TOKEN_CREATED,
$event = new TokenCreatedEvent($token)
);
return $event;
}
/**
* @param Token $token
* @param DateTime|null $at
* @param array $information
*
* @return ConsumeTokenEvent
*/
public function consumeToken(Token $token, DateTime $at = null, array $information = [])
{
$this->eventDispatcher->dispatch(
TokenEvents::CONSUME_TOKEN,
$event = new ConsumeTokenEvent($token, $at, $information)
);
return $event;
}
/**
* @param Token $token
*
* @return TokenConsumedEvent
*/
public function tokenConsumed(Token $token)
{
$this->eventDispatcher->dispatch(
TokenEvents::TOKEN_CONSUMED,
$event = new TokenConsumedEvent($token)
);
return $event;
}
/**
* @param Token $token
*
* @return TokenTotallyConsumedEvent
*/
public function tokenTotallyConsumed(Token $token)
{
$this->eventDispatcher->dispatch(
TokenEvents::TOKEN_TOTALLY_CONSUMED,
$event = new TokenTotallyConsumedEvent($token)
);
return $event;
}
/**
* @param string $purpose
* @param string $value
*
* @return TokenNotFoundEvent
*/
public function tokenNotFound($purpose, $value)
{
$this->eventDispatcher->dispatch(
TokenEvents::TOKEN_NOT_FOUND,
$event = new TokenNotFoundEvent($purpose, $value)
);
return $event;
}
/**
* @param string $purpose
* @param string $value
*
* @return TokenExpiredEvent
*/
public function tokenExpired($purpose, $value)
{
$this->eventDispatcher->dispatch(
TokenEvents::TOKEN_EXPIRED,
$event = new TokenExpiredEvent($purpose, $value)
);
return $event;
}
/**
* @deprecated since 2.3 to be removed in 3.0. Use tokenAlreadyConsumed instead.
* @param string $purpose
* @param string $value
*
* @return TokenUsedEvent
*/
public function tokenUsed($purpose, $value)
{
@trigger_error(
__METHOD__.' is deprecated. Use '.__CLASS__.'::tokenAlreadyConsumed instead.'
);
return $this->tokenAlreadyConsumed($purpose, $value);
}
/**
* @param string $purpose
* @param string $value
*
* @return TokenAlreadyConsumedEvent
*/
public function tokenAlreadyConsumed($purpose, $value)
{
$this->eventDispatcher->dispatch(
TokenEvents::TOKEN_ALREADY_CONSUMED,
$event = new TokenAlreadyConsumedEvent($purpose, $value)
);
return $event;
}
/**
* @param Token $token
*
* @return TokenRetrievedEvent
*/
public function tokenRetrieved(Token $token)
{
$this->eventDispatcher->dispatch(
TokenEvents::TOKEN_RETRIEVED,
$event = new TokenRetrievedEvent($token)
);
return $event;
}
}