Completed
Push — master ( 5d6a37...2d4cb5 )
by WEBEWEB
02:10
created

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