Completed
Push — master ( 3a35cb...dd73dd )
by WEBEWEB
01:56
created

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