Completed
Push — master ( b15ea3...332a47 )
by WEBEWEB
01:49
created

AbstractBootstrapController::getTranslator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 3
c 1
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of the bootstrap-bundle package.
5
 *
6
 * (c) 2018 WEBEWEB
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 WBW\Bundle\BootstrapBundle\Controller;
13
14
use Psr\Log\LoggerInterface;
15
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
16
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
17
use Symfony\Component\Translation\TranslatorInterface;
18
use WBW\Bundle\BootstrapBundle\BootstrapBundle;
19
use WBW\Bundle\BootstrapBundle\Event\NotificationEvent;
20
use WBW\Bundle\BootstrapBundle\Event\NotificationEvents;
21
22
/**
23
 * Abstract Bootstrap controller.
24
 *
25
 * @author webeweb <https://github.com/webeweb/>
26
 * @package WBW\Bundle\BootstrapBundle\Controller
27
 * @abstract
28
 */
29
abstract class AbstractBootstrapController extends Controller {
30
31
    /**
32
     * Get the event dispatcher.
33
     *
34
     * @return EventDispatcherInterface Returns the event dispatcher.
35
     */
36
    protected function getEventDispatcher() {
37
        return $this->get("event_dispatcher");
38
    }
39
40
    /**
41
     * Get the logger.
42
     *
43
     * @return LoggerInterface Returns the logger.
44
     */
45
    protected function getLogger() {
46
        return $this->get("logger");
47
    }
48
49
    /**
50
     * Get the translator.
51
     *
52
     * @return TranslatorInterface Returns the translator.
53
     */
54
    protected function getTranslator() {
55
        return $this->get("translator");
56
    }
57
58
    /**
59
     * Notify.
60
     *
61
     * @param string $eventName The event name.
62
     * @param string $notification The notification.
63
     * @param string $type The notification type.
64
     * @return void
65
     */
66
    private function notify($eventName, $notification, $type) {
67
68
        // Get the event dispatcher.
69
        $eventDispatcher = $this->getEventDispatcher();
70
71
        // Check the event dispatcher.
72
        if (null === $eventDispatcher) {
73
            return;
74
        }
75
        if (false === $eventDispatcher->hasListeners($eventName)) {
76
            return;
77
        }
78
79
        // Dispatch the event.
80
        $eventDispatcher->dispatch($eventName, new NotificationEvent($eventName, $notification, $type));
81
    }
82
83
    /**
84
     * Notify "Danger".
85
     *
86
     * @param string $notification The notification.
87
     * @return void
88
     */
89
    protected function notifyDanger($notification) {
90
        $this->notify(NotificationEvents::NOTIFICATION_DANGER, $notification, BootstrapBundle::BOOTSTRAP_DANGER);
91
    }
92
93
    /**
94
     * Notify "Info".
95
     *
96
     * @param string $notification The notification.
97
     * @return void
98
     */
99
    protected function notifyInfo($notification) {
100
        $this->notify(NotificationEvents::NOTIFICATION_INFO, $notification, BootstrapBundle::BOOTSTRAP_INFO);
101
    }
102
103
    /**
104
     * Notify "Success".
105
     *
106
     * @param string $notification The notification.
107
     * @return void
108
     */
109
    protected function notifySuccess($notification) {
110
        $this->notify(NotificationEvents::NOTIFICATION_SUCCESS, $notification, BootstrapBundle::BOOTSTRAP_SUCCESS);
111
    }
112
113
    /**
114
     * Notify "Warning".
115
     *
116
     * @param string $notification The notification.
117
     * @return void
118
     */
119
    protected function notifyWarning($notification) {
120
        $this->notify(NotificationEvents::NOTIFICATION_WARNING, $notification, BootstrapBundle::BOOTSTRAP_WARNING);
121
    }
122
123
}
124