Completed
Push — master ( ed21b7...06ef41 )
by WEBEWEB
09:11 queued 07:18
created

AbstractController   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 8
dl 0
loc 124
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getContainer() 0 3 1
A getEventDispatcher() 0 3 1
A getFormHelper() 0 3 1
A getKernelEventListener() 0 3 1
A getLogger() 0 3 1
A getRouter() 0 3 1
A getSession() 0 3 1
A getTranslator() 0 3 1
A hasRolesOrRedirect() 0 16 3
A notify() 0 14 3
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\Bundle\FrameworkBundle\Controller\Controller;
16
use Symfony\Component\DependencyInjection\Container;
17
use Symfony\Component\EventDispatcher\Event;
18
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
19
use Symfony\Component\HttpFoundation\Session\SessionInterface;
20
use Symfony\Component\Routing\RouterInterface;
21
use Symfony\Component\Security\Core\User\User;
22
use Symfony\Component\Translation\TranslatorInterface;
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 Controller {
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...e\Controller\Controller has been deprecated with message: since Symfony 4.2, use {@see AbstractController} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
38
39
    /**
40
     * Get the container.
41
     *
42
     * @return Container Returns the container.
43
     */
44
    protected function getContainer() {
45
        return $this->get("service_container");
46
    }
47
48
    /**
49
     * Get the event dispatcher.
50
     *
51
     * @return EventDispatcherInterface Returns the event dispatcher.
52
     */
53
    protected function getEventDispatcher() {
54
        return $this->get("event_dispatcher");
55
    }
56
57
    /**
58
     * Get the form helper.
59
     *
60
     * @return FormHelper Returns the form helper.
61
     */
62
    protected function getFormHelper() {
63
        return $this->get(FormHelper::SERVICE_NAME);
64
    }
65
66
    /**
67
     * Get the kernel event listener.
68
     *
69
     * @return KernelEventListener Returns the kernel event listener.
70
     */
71
    protected function getKernelEventListener() {
72
        return $this->get(KernelEventListener::SERVICE_NAME);
73
    }
74
75
    /**
76
     * Get the logger.
77
     *
78
     * @return LoggerInterface Returns the logger.
79
     */
80
    protected function getLogger() {
81
        return $this->get("logger");
82
    }
83
84
    /**
85
     * Get the router.
86
     *
87
     * @return RouterInterface Returns the router.
88
     */
89
    protected function getRouter() {
90
        return $this->get("router");
91
    }
92
93
    /**
94
     * Get the session.
95
     *
96
     * @return SessionInterface Returns the session.
97
     */
98
    protected function getSession() {
99
        return $this->get("session");
100
    }
101
102
    /**
103
     * Get the translator.
104
     *
105
     * @return TranslatorInterface Returns the translator.
106
     */
107
    protected function getTranslator() {
108
        return $this->get("translator");
109
    }
110
111
    /**
112
     * Determines if the connected user have roles or redirect.
113
     *
114
     * @param array $roles The roles.
115
     * @param bool $or OR ?
116
     * @param string $redirectUrl The redirect URL.
117
     * @param string $originUrl The origin URL.
118
     * @return bool Returns true.
119
     * @throws BadUserRoleException Throws a bad user role exception.
120
     */
121
    protected function hasRolesOrRedirect(array $roles, $or, $redirectUrl, $originUrl = "") {
122
123
        // Get the user.
124
        $user = $this->getKernelEventListener()->getUser();
125
126
        // User have roles ?
127
        if (false === UserHelper::hasRoles($user, $roles, $or)) {
128
129
            // Throw a bad user role exception with an anonymous user if user is null.
130
            $user = null !== $user ? $user : new User("anonymous", "");
131
            throw new BadUserRoleException($user, $roles, $redirectUrl, $originUrl);
132
        }
133
134
        // Return
135
        return true;
136
    }
137
138
    /**
139
     * Notify.
140
     *
141
     * @param string $eventName The event name.
142
     * @param NotificationInterface $notification The notification.
143
     * @return Event Returns the event.
144
     */
145
    protected function notify($eventName, NotificationInterface $notification) {
146
147
        // Get and check the event dispatcher.
148
        $eventDispatcher = $this->getEventDispatcher();
149
        if (null === $eventDispatcher || false === $eventDispatcher->hasListeners($eventName)) {
150
            return null;
151
        }
152
153
        // Log a debug trace.
154
        $this->getLogger()->debug(sprintf("Core controller dispatch a notification event with name \"%s\"", $eventName));
155
156
        // Dispatch the event.
157
        return $eventDispatcher->dispatch($eventName, new NotificationEvent($eventName, $notification));
158
    }
159
160
}
161