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