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