1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the awurth/silex-user package. |
5
|
|
|
* |
6
|
|
|
* (c) Alexis Wurth <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace AWurth\Silex\User\EventListener; |
13
|
|
|
|
14
|
|
|
use AWurth\Silex\User\Event\Events; |
15
|
|
|
use InvalidArgumentException; |
16
|
|
|
use Symfony\Component\EventDispatcher\Event; |
17
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
18
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
19
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
20
|
|
|
|
21
|
|
|
class FlashListener implements EventSubscriberInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected static $successMessages = [ |
27
|
|
|
Events::REGISTRATION_COMPLETED => 'silex_user.registration.flash.user_created' |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Session |
32
|
|
|
*/ |
33
|
|
|
protected $session; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var TranslatorInterface |
37
|
|
|
*/ |
38
|
|
|
protected $translator; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Constructor. |
42
|
|
|
* |
43
|
|
|
* @param Session $session |
44
|
|
|
* @param TranslatorInterface $translator |
45
|
|
|
*/ |
46
|
|
|
public function __construct(Session $session, TranslatorInterface $translator) |
47
|
|
|
{ |
48
|
|
|
$this->session = $session; |
49
|
|
|
$this->translator = $translator; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public static function getSubscribedEvents() |
56
|
|
|
{ |
57
|
|
|
return [ |
58
|
|
|
Events::REGISTRATION_COMPLETED => 'addSuccessFlash' |
59
|
|
|
]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Adds a success flash message. |
64
|
|
|
* |
65
|
|
|
* @param Event $event |
66
|
|
|
* @param string $eventName |
67
|
|
|
*/ |
68
|
|
|
public function addSuccessFlash(Event $event, $eventName) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
if (!isset(self::$successMessages[$eventName])) { |
71
|
|
|
throw new InvalidArgumentException('This event does not correspond to a known flash message'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->session->getFlashBag()->add('success', $this->translator->trans(self::$successMessages[$eventName])); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.