FlashListener::getSubscribedEvents()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
ccs 0
cts 3
cp 0
crap 2
1
<?php
2
3
namespace Azine\EmailUpdateConfirmationBundle\EventListener;
4
5
use Azine\EmailUpdateConfirmationBundle\AzineEmailUpdateConfirmationEvents;
6
use Symfony\Component\EventDispatcher\Event;
7
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8
use Symfony\Component\HttpFoundation\Session\Session;
9
use Symfony\Component\Translation\TranslatorInterface;
10
11
class FlashListener implements EventSubscriberInterface
12
{
13
    /**
14
     * @var string[]
15
     */
16
    private static $successMessages = array(
17
        AzineEmailUpdateConfirmationEvents::EMAIL_UPDATE_SUCCESS => 'email_update.flash.success',
18
        AzineEmailUpdateConfirmationEvents::EMAIL_UPDATE_INITIALIZE => 'email_update.flash.info',
19
    );
20
21
    /**
22
     * @var Session
23
     */
24
    private $session;
25
    /**
26
     * @var TranslatorInterface
27
     */
28
    private $translator;
29
30
    /**
31
     * FlashListener constructor.
32
     *
33
     * @param Session             $session
34
     * @param TranslatorInterface $translator
35
     */
36 2
    public function __construct(Session $session, TranslatorInterface $translator)
37
    {
38 2
        $this->session = $session;
39 2
        $this->translator = $translator;
40 2
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public static function getSubscribedEvents()
46
    {
47
        return array(
48
            AzineEmailUpdateConfirmationEvents::EMAIL_UPDATE_SUCCESS => 'addSuccessFlash',
49
            AzineEmailUpdateConfirmationEvents::EMAIL_UPDATE_INITIALIZE => 'addInfoFlash',
50
        );
51
    }
52
53
    /**
54
     * @param Event  $event
55
     * @param string $eventName
56
     */
57 1
    public function addSuccessFlash(Event $event, $eventName)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

57
    public function addSuccessFlash(/** @scrutinizer ignore-unused */ Event $event, $eventName)

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

Loading history...
58
    {
59 1
        if (!isset(self::$successMessages[$eventName])) {
60
            throw new \InvalidArgumentException('This event does not correspond to a known flash message');
61
        }
62 1
        $this->session->getFlashBag()->add('success', $this->trans(self::$successMessages[$eventName]));
63 1
    }
64
65
    /**
66
     * @param Event  $event
67
     * @param string $eventName
68
     */
69 1
    public function addInfoFlash(Event $event, $eventName)
70
    {
71 1
        if (!isset(self::$successMessages[$eventName])) {
72
            throw new \InvalidArgumentException('This event does not correspond to a known flash message');
73
        }
74
75 1
        $this->session->getFlashBag()->add('info', $this->trans(self::$successMessages[$eventName]));
76 1
    }
77
78
    /**
79
     * @param string$message
80
     * @param array $params
81
     *
82
     * @return string
83
     */
84 2
    private function trans($message, array $params = array())
85
    {
86 2
        return $this->translator->trans($message, $params);
87
    }
88
}
89