Completed
Push — master ( 6f429a...4083cc )
by Alexis
06:02
created

FlashListener   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 56
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getSubscribedEvents() 0 6 1
A addSuccessFlash() 0 8 2
1
<?php
2
3
namespace AWurth\SilexUser\EventListener;
4
5
use AWurth\SilexUser\Event\Events;
6
use InvalidArgumentException;
7
use Symfony\Component\EventDispatcher\Event;
8
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9
use Symfony\Component\HttpFoundation\Session\Session;
10
use Symfony\Component\Translation\TranslatorInterface;
11
12
class FlashListener implements EventSubscriberInterface
13
{
14
    /**
15
     * @var array
16
     */
17
    protected static $successMessages = [
18
        Events::REGISTRATION_COMPLETED => 'registration.flash.user_created'
19
    ];
20
21
    /**
22
     * @var Session
23
     */
24
    protected $session;
25
26
    /**
27
     * @var TranslatorInterface
28
     */
29
    protected $translator;
30
31
    /**
32
     * Constructor.
33
     *
34
     * @param Session             $session
35
     * @param TranslatorInterface $translator
36
     */
37
    public function __construct(Session $session, TranslatorInterface $translator)
38
    {
39
        $this->session = $session;
40
        $this->translator = $translator;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public static function getSubscribedEvents()
47
    {
48
        return [
49
            Events::REGISTRATION_COMPLETED => 'addSuccessFlash'
50
        ];
51
    }
52
53
    /**
54
     * Adds a success flash message.
55
     *
56
     * @param Event  $event
57
     * @param string $eventName
58
     */
59
    public function addSuccessFlash(Event $event, $eventName)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
60
    {
61
        if (!isset(self::$successMessages[$eventName])) {
62
            throw new InvalidArgumentException('This event does not correspond to a known flash message');
63
        }
64
65
        $this->session->getFlashBag()->add('success', $this->translator->trans(self::$successMessages[$eventName]));
66
    }
67
}
68