Completed
Push — feature/rewrite ( 88e863...f6b662 )
by Alexandre
01:55
created

FlashNoticeListener   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 67
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getSubscribedEvents() 0 10 1
A addSuccessFlash() 0 7 2
A trans() 0 4 1
1
<?php
2
3
namespace Black\Bundle\PageBundle\Infrastructure\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;
0 ignored issues
show
Documentation Bug introduced by
It seems like $translator of type object<Symfony\Component...on\TranslatorInterface> is incompatible with the declared type object<Symfony\Component\Translation\Translator> of property $translator.

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..

Loading history...
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()])) {
0 ignored issues
show
Bug introduced by
The method getName() does not seem to exist on object<Symfony\Component\EventDispatcher\Event>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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()]));
0 ignored issues
show
Bug introduced by
The method getName() does not seem to exist on object<Symfony\Component\EventDispatcher\Event>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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