FlashListener   A
last analyzed

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
/*
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)
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...
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