Completed
Push — master ( 1af01d...b9fdcf )
by WEBEWEB
01:21
created

AbstractController::translate()   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 4
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\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
    protected function dispatchEvent($eventName, BaseEvent $event) {
51
        $this->getLogger()->debug(sprintf('A controller dispatch an event with name "%s"', $eventName, ["_controller" => get_class($this), "_event" => get_class($event)]));
52
        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
    protected function getContainer() {
61
        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
    protected function getEventDispatcher() {
70
        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
    protected function getFormHelper() {
79
        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
    protected function getKernelEventListener() {
88
        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
    protected function getLogger() {
97
        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
    protected function getRepositoryHelper() {
106
        return $this->get(RepositoryHelper::SERVICE_NAME);
107
    }
108
109
    /**
110
     * Get the repository report helper.
111
     *
112
     * @return RepositoryHelper|null Returns the repository helper in case of success, null otherwise.
113
     * @deprecated since 2.15.0, use {@see WBW\Bundle\CoreBundle\Controller\AbstractController::getRepositoryHelper()} instead.
114
     */
115
    protected function getRepositoryReportHelper() {
116
        return $this->get(RepositoryReportHelper::SERVICE_NAME);
117
    }
118
119
    /**
120
     * Get the router.
121
     *
122
     * @return RouterInterface|null Returns the router in case of success, null otherwise.
123
     */
124
    protected function getRouter() {
125
        return $this->get("router");
126
    }
127
128
    /**
129
     * Get the session.
130
     *
131
     * @return SessionInterface|null Returns the session in case of success, null otherwise.
132
     */
133
    protected function getSession() {
134
        return $this->get("session");
135
    }
136
137
    /**
138
     * Get the translator.
139
     *
140
     * @return TranslatorInterface|null Returns the translator in case of success, null otherwise.
141
     */
142
    protected function getTranslator() {
143
        return $this->get("translator");
144
    }
145
146
    /**
147
     * Determines if the connected user have roles or redirect.
148
     *
149
     * @param array $roles The roles.
150
     * @param bool $or OR ?
151
     * @param string $redirectUrl The redirect URL.
152
     * @param string $originUrl The origin URL.
153
     * @return bool Returns true.
154
     * @throws BadUserRoleException Throws a bad user role exception.
155
     */
156
    protected function hasRolesOrRedirect(array $roles, $or, $redirectUrl, $originUrl = "") {
157
158
        $user = $this->getKernelEventListener()->getUser();
159
        if (false === UserHelper::hasRoles($user, $roles, $or)) {
160
161
            // Throw a bad user role exception with an anonymous user if user is null.
162
            $user = null !== $user ? $user : new User("anonymous", "");
163
            throw new BadUserRoleException($user, $roles, $redirectUrl, $originUrl);
164
        }
165
166
        return true;
167
    }
168
169
    /**
170
     * Notify.
171
     *
172
     * @param string $eventName The event name.
173
     * @param NotificationInterface $notification The notification.
174
     * @return NotificationEvent|null Returns the event in case of success, null otherwise.
175
     */
176
    protected function notify($eventName, NotificationInterface $notification) {
177
        return $this->dispatchEvent($eventName, new NotificationEvent($eventName, $notification));
178
    }
179
180
    /**
181
     * Toast.
182
     *
183
     * @param string $eventName The event name.
184
     * @param ToastInterface $toast The toast.
185
     * @return ToastEvent|null Returns the event in case of success, null otherwise.
186
     */
187
    protected function toast($eventName, ToastInterface $toast) {
188
        return $this->dispatchEvent($eventName, new ToastEvent($eventName, $toast));
189
    }
190
191
    /**
192
     * Translate.
193
     *
194
     * @param string $id The id.
195
     * @param array $parameters The parameters.
196
     * @param string|null $domain The domain.
197
     * @param string|null $locale The locale.
198
     * @return string Returns the translation in case of success, $id otherwise.
199
     */
200
    protected function translate($id, array $parameters = [], $domain = null, $locale = null) {
201
        return $this->getTranslator()->trans($id, $parameters, $domain, $locale);
202
    }
203
}
204