Completed
Push — master ( f3c5a2...9c66df )
by WEBEWEB
02:13
created

AbstractController::toast()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the core-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\CoreBundle\Controller;
13
14
use Psr\Log\LoggerInterface;
15
use Symfony\Component\DependencyInjection\Container;
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\CoreBundle\Component\BaseEvent;
22
use WBW\Bundle\CoreBundle\Event\NotificationEvent;
23
use WBW\Bundle\CoreBundle\Event\ToastEvent;
24
use WBW\Bundle\CoreBundle\EventDispatcher\EventDispatcherHelper;
25
use WBW\Bundle\CoreBundle\EventListener\KernelEventListener;
26
use WBW\Bundle\CoreBundle\Exception\BadUserRoleException;
27
use WBW\Bundle\CoreBundle\Helper\FormHelper;
28
use WBW\Bundle\CoreBundle\Helper\UserHelper;
29
use WBW\Bundle\CoreBundle\Notification\NotificationInterface;
30
use WBW\Bundle\CoreBundle\Toast\ToastInterface;
31
32
/**
33
 * Abstract controller.
34
 *
35
 * @author webeweb <https://github.com/webeweb/>
36
 * @package WBW\Bundle\CoreBundle\Controller
37
 * @abstract
38
 */
39
abstract class AbstractController extends BaseController {
40
41
    /**
42
     * Dispatch an event.
43
     *
44
     * @param string $eventName The event name.
45
     * @param BaseEvent $event The event.
46
     * @return BaseEvent|null Returns the event in case of success, null otherwise.
47
     */
48
    protected function dispatchEvent($eventName, BaseEvent $event) {
49
        return EventDispatcherHelper::dispatch($this->getEventDispatcher(), $eventName, $event);
50
    }
51
52
    /**
53
     * Get the container.
54
     *
55
     * @return Container Returns the container.
56
     */
57
    protected function getContainer() {
58
        return $this->get("service_container");
59
    }
60
61
    /**
62
     * Get the event dispatcher.
63
     *
64
     * @return EventDispatcherInterface Returns the event dispatcher.
65
     */
66
    protected function getEventDispatcher() {
67
        return $this->get("event_dispatcher");
68
    }
69
70
    /**
71
     * Get the form helper.
72
     *
73
     * @return FormHelper Returns the form helper.
74
     */
75
    protected function getFormHelper() {
76
        return $this->get(FormHelper::SERVICE_NAME);
77
    }
78
79
    /**
80
     * Get the kernel event listener.
81
     *
82
     * @return KernelEventListener Returns the kernel event listener.
83
     */
84
    protected function getKernelEventListener() {
85
        return $this->get(KernelEventListener::SERVICE_NAME);
86
    }
87
88
    /**
89
     * Get the logger.
90
     *
91
     * @return LoggerInterface Returns the logger.
92
     */
93
    protected function getLogger() {
94
        return $this->get("logger");
95
    }
96
97
    /**
98
     * Get the router.
99
     *
100
     * @return RouterInterface Returns the router.
101
     */
102
    protected function getRouter() {
103
        return $this->get("router");
104
    }
105
106
    /**
107
     * Get the session.
108
     *
109
     * @return SessionInterface Returns the session.
110
     */
111
    protected function getSession() {
112
        return $this->get("session");
113
    }
114
115
    /**
116
     * Get the translator.
117
     *
118
     * @return TranslatorInterface Returns the translator.
119
     */
120
    protected function getTranslator() {
121
        return $this->get("translator");
122
    }
123
124
    /**
125
     * Determines if the connected user have roles or redirect.
126
     *
127
     * @param array $roles The roles.
128
     * @param bool $or OR ?
129
     * @param string $redirectUrl The redirect URL.
130
     * @param string $originUrl The origin URL.
131
     * @return bool Returns true.
132
     * @throws BadUserRoleException Throws a bad user role exception.
133
     */
134
    protected function hasRolesOrRedirect(array $roles, $or, $redirectUrl, $originUrl = "") {
135
136
        $user = $this->getKernelEventListener()->getUser();
137
        if (false === UserHelper::hasRoles($user, $roles, $or)) {
138
139
            // Throw a bad user role exception with an anonymous user if user is null.
140
            $user = null !== $user ? $user : new User("anonymous", "");
141
            throw new BadUserRoleException($user, $roles, $redirectUrl, $originUrl);
142
        }
143
144
        return true;
145
    }
146
147
    /**
148
     * Notify.
149
     *
150
     * @param string $eventName The event name.
151
     * @param NotificationInterface $notification The notification.
152
     * @return NotificationEvent|null Returns the event in case of success, null otherwise.
153
     */
154
    protected function notify($eventName, NotificationInterface $notification) {
155
        $this->getLogger()->debug(sprintf("Core controller dispatch a notification event with name \"%s\"", $eventName));
156
        return $this->dispatchEvent($eventName, new NotificationEvent($eventName, $notification));
157
    }
158
159
    /**
160
     * Toast.
161
     *
162
     * @param string $eventName The event name.
163
     * @param ToastInterface $toast The toast.
164
     * @return ToastEvent|null Returns the event in case of success, null otherwise.
165
     */
166
    protected function toast($eventName, ToastInterface $toast) {
167
        $this->getLogger()->debug(sprintf("Core controller dispatch a toast event with name \"%s\"", $eventName));
168
        return $this->dispatchEvent($eventName, new ToastEvent($eventName, $toast));
169
    }
170
}
171