Completed
Push — master ( 6812a9...b3ff48 )
by WEBEWEB
02:08
created

AbstractBootstrapController::notifyDanger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
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\Security\Core\User\User;
19
use Symfony\Component\Translation\TranslatorInterface;
20
use WBW\Bundle\BootstrapBundle\BootstrapBundle;
21
use WBW\Bundle\BootstrapBundle\Event\NotificationEvent;
22
use WBW\Bundle\BootstrapBundle\Event\NotificationEvents;
23
use WBW\Bundle\BootstrapBundle\EventListener\KernelEventListener;
24
use WBW\Bundle\BootstrapBundle\Exception\BadUserRoleException;
25
use WBW\Bundle\BootstrapBundle\Helper\UserHelper;
26
27
/**
28
 * Abstract Bootstrap controller.
29
 *
30
 * @author webeweb <https://github.com/webeweb/>
31
 * @package WBW\Bundle\BootstrapBundle\Controller
32
 * @abstract
33
 */
34
abstract class AbstractBootstrapController extends Controller {
35
36
    /**
37
     * Get the event dispatcher.
38
     *
39
     * @return EventDispatcherInterface Returns the event dispatcher.
40
     */
41
    protected function getEventDispatcher() {
42
        return $this->get("event_dispatcher");
43
    }
44
45
    /**
46
     * Get the kernel event listener.
47
     *
48
     * @return KernelEventListener Returns the kernel event listener.
49
     */
50
    protected function getKernelEventListener() {
51
        return $this->get(KernelEventListener::SERVICE_NAME);
52
    }
53
54
    /**
55
     * Get the logger.
56
     *
57
     * @return LoggerInterface Returns the logger.
58
     */
59
    protected function getLogger() {
60
        return $this->get("logger");
61
    }
62
63
    /**
64
     * Get the router.
65
     *
66
     * @return RouterInterface Returns the router.
67
     */
68
    protected function getRouter() {
69
        return $this->get("router");
70
    }
71
72
    /**
73
     * Get the translator.
74
     *
75
     * @return TranslatorInterface Returns the translator.
76
     */
77
    protected function getTranslator() {
78
        return $this->get("translator");
79
    }
80
81
    /**
82
     * Determines if the connected user have roles or redirect.
83
     *
84
     * @param array $roles The roles.
85
     * @param boolean $or OR ?
86
     * @param string $redirect The redirect.
87
     * @return boolean Returns true.
88
     * @throws BadUserRoleException Throws a bad user role exception.
89
     */
90
    protected function hasRolesOrRedirect(array $roles, $or, $redirect) {
91
92
        // Get the user.
93
        $user = $this->getKernelEventListener()->getUser();
94
95
        // User have roles ?
96
        if (false === UserHelper::hasRoles($user, $roles, $or)) {
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->getKernelEventListener()->getUser() on line 93 can be null; however, WBW\Bundle\BootstrapBund...\UserHelper::hasRoles() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
97
98
            // Throw a bad user role exception with an anonymous user if user is null.
99
            $user = null !== $user ? $user : new User("anonymous", "");
100
            throw new BadUserRoleException($user, $roles, "", $redirect);
101
        }
102
103
        // Return
104
        return true;
105
    }
106
107
    /**
108
     * Notify.
109
     *
110
     * @param string $eventName The event name.
111
     * @param string $notification The notification.
112
     * @param string $type The notification type.
113
     * @return Event Returns the event.
114
     */
115
    private function notify($eventName, $notification, $type) {
116
117
        // Get and check the event dispatcher.
118
        $eventDispatcher = $this->getEventDispatcher();
119
        if (null === $eventDispatcher || false === $eventDispatcher->hasListeners($eventName)) {
120
            return null;
121
        }
122
123
        // Log a debug trace.
124
        $this->getLogger()->debug(sprintf("Bootstrap controller dispatch a notification event with name \"%s\"", $eventName));
125
126
        // Dispatch the event.
127
        return $eventDispatcher->dispatch($eventName, new NotificationEvent($eventName, $notification, $type));
128
    }
129
130
    /**
131
     * Notify "Danger".
132
     *
133
     * @param string $notification The notification.
134
     * @return Event Returns the event.
135
     */
136
    protected function notifyDanger($notification) {
137
        return $this->notify(NotificationEvents::NOTIFICATION_DANGER, $notification, BootstrapBundle::BOOTSTRAP_DANGER);
138
    }
139
140
    /**
141
     * Notify "Info".
142
     *
143
     * @param string $notification The notification.
144
     * @return Event Returns the event.
145
     */
146
    protected function notifyInfo($notification) {
147
        return $this->notify(NotificationEvents::NOTIFICATION_INFO, $notification, BootstrapBundle::BOOTSTRAP_INFO);
148
    }
149
150
    /**
151
     * Notify "Success".
152
     *
153
     * @param string $notification The notification.
154
     * @return Event Returns the event.
155
     */
156
    protected function notifySuccess($notification) {
157
        return $this->notify(NotificationEvents::NOTIFICATION_SUCCESS, $notification, BootstrapBundle::BOOTSTRAP_SUCCESS);
158
    }
159
160
    /**
161
     * Notify "Warning".
162
     *
163
     * @param string $notification The notification.
164
     * @return Event Returns the event.
165
     */
166
    protected function notifyWarning($notification) {
167
        return $this->notify(NotificationEvents::NOTIFICATION_WARNING, $notification, BootstrapBundle::BOOTSTRAP_WARNING);
168
    }
169
170
}
171