Completed
Push — master ( 405a3d...c81e87 )
by WEBEWEB
02:04
created

AbstractBootstrapController::getSession()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
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\HttpFoundation\Session\SessionInterface;
18
use Symfony\Component\Routing\RouterInterface;
19
use Symfony\Component\Security\Core\User\User;
20
use Symfony\Component\Translation\TranslatorInterface;
21
use WBW\Bundle\BootstrapBundle\BootstrapBundle;
22
use WBW\Bundle\BootstrapBundle\Event\NotificationEvent;
23
use WBW\Bundle\BootstrapBundle\Event\NotificationEvents;
24
use WBW\Bundle\BootstrapBundle\EventListener\KernelEventListener;
25
use WBW\Bundle\BootstrapBundle\Exception\BadUserRoleException;
26
use WBW\Bundle\BootstrapBundle\Helper\UserHelper;
27
28
/**
29
 * Abstract Bootstrap controller.
30
 *
31
 * @author webeweb <https://github.com/webeweb/>
32
 * @package WBW\Bundle\BootstrapBundle\Controller
33
 * @abstract
34
 */
35
abstract class AbstractBootstrapController extends Controller {
36
37
    /**
38
     * Get the event dispatcher.
39
     *
40
     * @return EventDispatcherInterface Returns the event dispatcher.
41
     */
42
    protected function getEventDispatcher() {
43
        return $this->get("event_dispatcher");
44
    }
45
46
    /**
47
     * Get the kernel event listener.
48
     *
49
     * @return KernelEventListener Returns the kernel event listener.
50
     */
51
    protected function getKernelEventListener() {
52
        return $this->get(KernelEventListener::SERVICE_NAME);
53
    }
54
55
    /**
56
     * Get the logger.
57
     *
58
     * @return LoggerInterface Returns the logger.
59
     */
60
    protected function getLogger() {
61
        return $this->get("logger");
62
    }
63
64
    /**
65
     * Get the router.
66
     *
67
     * @return RouterInterface Returns the router.
68
     */
69
    protected function getRouter() {
70
        return $this->get("router");
71
    }
72
73
    /**
74
     * Get the session.
75
     *
76
     * @return SessionInterface Returns the session.
77
     */
78
    protected function getSession() {
79
        return $this->get("session");
80
    }
81
82
    /**
83
     * Get the translator.
84
     *
85
     * @return TranslatorInterface Returns the translator.
86
     */
87
    protected function getTranslator() {
88
        return $this->get("translator");
89
    }
90
91
    /**
92
     * Determines if the connected user have roles or redirect.
93
     *
94
     * @param array $roles The roles.
95
     * @param bool $or OR ?
96
     * @param string $redirect The redirect.
97
     * @return bool Returns true.
98
     * @throws BadUserRoleException Throws a bad user role exception.
99
     */
100
    protected function hasRolesOrRedirect(array $roles, $or, $redirect) {
101
102
        // Get the user.
103
        $user = $this->getKernelEventListener()->getUser();
104
105
        // User have roles ?
106
        if (false === UserHelper::hasRoles($user, $roles, $or)) {
107
108
            // Throw a bad user role exception with an anonymous user if user is null.
109
            $user = null !== $user ? $user : new User("anonymous", "");
110
            throw new BadUserRoleException($user, $roles, "", $redirect);
111
        }
112
113
        // Return
114
        return true;
115
    }
116
117
    /**
118
     * Notify.
119
     *
120
     * @param string $eventName The event name.
121
     * @param string $notification The notification.
122
     * @param string $type The notification type.
123
     * @return Event Returns the event.
124
     */
125
    private function notify($eventName, $notification, $type) {
126
127
        // Get and check the event dispatcher.
128
        $eventDispatcher = $this->getEventDispatcher();
129
        if (null === $eventDispatcher || false === $eventDispatcher->hasListeners($eventName)) {
130
            return null;
131
        }
132
133
        // Log a debug trace.
134
        $this->getLogger()->debug(sprintf("Bootstrap controller dispatch a notification event with name \"%s\"", $eventName));
135
136
        // Dispatch the event.
137
        return $eventDispatcher->dispatch($eventName, new NotificationEvent($eventName, $notification, $type));
138
    }
139
140
    /**
141
     * Notify "Danger".
142
     *
143
     * @param string $notification The notification.
144
     * @return Event Returns the event.
145
     */
146
    protected function notifyDanger($notification) {
147
        return $this->notify(NotificationEvents::NOTIFICATION_DANGER, $notification, BootstrapBundle::BOOTSTRAP_DANGER);
148
    }
149
150
    /**
151
     * Notify "Info".
152
     *
153
     * @param string $notification The notification.
154
     * @return Event Returns the event.
155
     */
156
    protected function notifyInfo($notification) {
157
        return $this->notify(NotificationEvents::NOTIFICATION_INFO, $notification, BootstrapBundle::BOOTSTRAP_INFO);
158
    }
159
160
    /**
161
     * Notify "Success".
162
     *
163
     * @param string $notification The notification.
164
     * @return Event Returns the event.
165
     */
166
    protected function notifySuccess($notification) {
167
        return $this->notify(NotificationEvents::NOTIFICATION_SUCCESS, $notification, BootstrapBundle::BOOTSTRAP_SUCCESS);
168
    }
169
170
    /**
171
     * Notify "Warning".
172
     *
173
     * @param string $notification The notification.
174
     * @return Event Returns the event.
175
     */
176
    protected function notifyWarning($notification) {
177
        return $this->notify(NotificationEvents::NOTIFICATION_WARNING, $notification, BootstrapBundle::BOOTSTRAP_WARNING);
178
    }
179
180
}
181