1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Black\Bundle\PageBundle\Listener; |
4
|
|
|
|
5
|
|
|
use Black\Page\WebPageDomainEvents; |
6
|
|
|
use Symfony\Component\EventDispatcher\Event; |
7
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
8
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
9
|
|
|
use Symfony\Component\Translation\Translator; |
10
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class FlashNoticeListener |
14
|
|
|
*/ |
15
|
|
|
class FlashNoticeListener implements EventSubscriberInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var Session |
19
|
|
|
*/ |
20
|
|
|
protected $session; |
21
|
|
|
/** |
22
|
|
|
* @var Translator |
23
|
|
|
*/ |
24
|
|
|
protected $translator; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected static $successMessages = [ |
30
|
|
|
WebPageDomainEvents::WEBPAGE_DOMAIN_CREATED => 'black_page.flash.success.created', |
31
|
|
|
WebPageDomainEvents::WEBPAGE_DOMAIN_DEPUBLISHED => 'black_page.flash.success.depublished', |
32
|
|
|
WebPageDomainEvents::WEBPAGE_DOMAIN_PUBLISHED => 'black_page.flash.success.published', |
33
|
|
|
WebPageDomainEvents::WEBPAGE_DOMAIN_REMOVED => 'black_page.flash.success.removed', |
34
|
|
|
WebPageDomainEvents::WEBPAGE_DOMAIN_WRITE => 'black_page.flash.success.write', |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param Session $session |
39
|
|
|
* @param TranslatorInterface $translator |
40
|
|
|
*/ |
41
|
|
|
public function __construct(Session $session, TranslatorInterface $translator) |
42
|
|
|
{ |
43
|
|
|
$this->session = $session; |
44
|
|
|
$this->translator = $translator; |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
|
|
public static function getSubscribedEvents() |
51
|
|
|
{ |
52
|
|
|
return [ |
53
|
|
|
WebPageDomainEvents::WEBPAGE_DOMAIN_CREATED => 'addSuccessFlash', |
54
|
|
|
WebPageDomainEvents::WEBPAGE_DOMAIN_DEPUBLISHED => 'addSuccessFlash', |
55
|
|
|
WebPageDomainEvents::WEBPAGE_DOMAIN_PUBLISHED => 'addSuccessFlash', |
56
|
|
|
WebPageDomainEvents::WEBPAGE_DOMAIN_REMOVED => 'addSuccessFlash', |
57
|
|
|
WebPageDomainEvents::WEBPAGE_DOMAIN_WRITE => 'addSuccessFlash', |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param Event $event |
63
|
|
|
*/ |
64
|
|
|
public function addSuccessFlash(Event $event) |
65
|
|
|
{ |
66
|
|
|
if (!isset(self::$successMessages[$event->getName()])) { |
|
|
|
|
67
|
|
|
throw new \InvalidArgumentException('This event does not correspond to a known flash message'); |
68
|
|
|
} |
69
|
|
|
$this->session->getFlashBag()->add('success', $this->trans(self::$successMessages[$event->getName()])); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param $message |
74
|
|
|
* @param array $params |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
private function trans($message, array $params = array()) |
78
|
|
|
{ |
79
|
|
|
return $this->translator->trans($message, $params, 'flash'); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..