Completed
Push — master ( 13a925...692a36 )
by Alexis
03:01
created

Controller::denyAccessUnlessGranted()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace App\Controller;
4
5
use Doctrine\ORM\EntityManager;
6
use Security\Entity\User;
7
use Silex\Application;
8
use Symfony\Component\EventDispatcher\EventDispatcher;
9
use Symfony\Component\Form\FormFactory;
10
use Symfony\Component\HttpFoundation\RedirectResponse;
11
use Symfony\Component\HttpFoundation\Session\Session;
12
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
13
use Symfony\Component\Routing\Generator\UrlGenerator;
14
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
15
use Symfony\Component\Validator\Validator\RecursiveValidator;
16
use Twig_Environment;
17
18
/**
19
 * @property EventDispatcher    dispatcher
20
 * @property RecursiveValidator validator
21
 * @property Session            session
22
 * @property Twig_Environment   twig
23
 * @property UrlGenerator       url_generator
24
 * @property string             root_dir
25
 */
26
class Controller
27
{
28
    /**
29
     * @var Application
30
     */
31
    protected $application;
32
33
    /**
34
     * Constructor.
35
     *
36
     * @param Application $app
37
     */
38
    public function __construct(Application $app)
39
    {
40
        $this->application = $app;
41
    }
42
43
    /**
44
     * Returns a new AccessDeniedException.
45
     *
46
     * @param string $message
47
     *
48
     * @return AccessDeniedException
49
     */
50
    protected function createAccessDeniedException($message = 'Access Denied.')
51
    {
52
        return new AccessDeniedException($message);
53
    }
54
55
    /**
56
     * Returns a new NotFoundHttpException.
57
     *
58
     * @param string $message
59
     *
60
     * @return NotFoundHttpException
61
     */
62
    protected function createNotFoundHttpException($message = 'Not Found')
63
    {
64
        return new NotFoundHttpException($message);
65
    }
66
67
    /**
68
     * Throws an exception unless the attributes are granted against the current authentication token.
69
     *
70
     * @param mixed  $roles
71
     * @param string $message
72
     *
73
     * @throws AccessDeniedException
74
     */
75
    protected function denyAccessUnlessGranted($roles, $message = 'Access Denied.')
76
    {
77
        if (!$this->isGranted($roles)) {
78
            $exception = $this->createAccessDeniedException($message);
79
            $exception->setAttributes($roles);
80
81
            throw $exception;
82
        }
83
    }
84
85
    /**
86
     * Gets Doctrine Entity Manager.
87
     *
88
     * @return EntityManager
89
     */
90
    public function getEntityManager()
91
    {
92
        return $this->application['orm.em'];
93
    }
94
95
    /**
96
     * Gets the Form Factory.
97
     *
98
     * @return FormFactory
99
     */
100
    public function getFormFactory()
101
    {
102
        return $this->application['form.factory'];
103
    }
104
105
    /**
106
     * Gets the project root directory.
107
     *
108
     * @return string
109
     */
110
    public function getRootDir()
111
    {
112
        return $this->application['root_dir'];
113
    }
114
115
    /**
116
     * Gets the router.
117
     *
118
     * @return UrlGenerator
119
     */
120
    public function getRouter()
121
    {
122
        return $this->application['url_generator'];
123
    }
124
125
    /**
126
     * Gets the session.
127
     *
128
     * @return Session
129
     */
130
    public function getSession()
131
    {
132
        return $this->application['session'];
133
    }
134
135
    /**
136
     * Gets the Twig service.
137
     *
138
     * @return Twig_Environment
139
     */
140
    public function getTwig()
141
    {
142
        return $this->application['twig'];
143
    }
144
145
    /**
146
     * Gets the current authenticated user or null if not logged in.
147
     *
148
     * @return User|null
149
     */
150
    public function getUser()
151
    {
152
        $user = $this->application['security.token_storage']->getToken()->getUser();
153
        if ($user instanceof User) {
154
            return $user;
155
        }
156
157
        return null;
158
    }
159
160
    /**
161
     * Redirects the user to another route.
162
     *
163
     * @param string $route
164
     * @param array $parameters
165
     * @param int $status
166
     *
167
     * @return RedirectResponse
168
     */
169
    public function redirect($route, $parameters = [], $status = 302)
170
    {
171
        return $this->application->redirect($this->path($route, $parameters), $status);
172
    }
173
174
    /**
175
     * Redirects the user to another URL.
176
     *
177
     * @param string $url The URL to redirect to
178
     * @param int $status The status code (302 by default)
179
     *
180
     * @return RedirectResponse
181
     */
182
    public function redirectTo($url, $status = 302)
183
    {
184
        return $this->application->redirect($url, $status);
185
    }
186
187
    /**
188
     * Generates a path from the given parameters.
189
     *
190
     * @param string $route
191
     * @param mixed $parameters
192
     *
193
     * @return string
194
     */
195
    public function path($route, $parameters = [])
196
    {
197
        return $this->getRouter()->generate($route, $parameters, UrlGenerator::ABSOLUTE_PATH);
198
    }
199
200
    /**
201
     * Generates an absolute URL from the given parameters.
202
     *
203
     * @param string $route
204
     * @param mixed $parameters
205
     *
206
     * @return string
207
     */
208
    public function url($route, $parameters = [])
209
    {
210
        return $this->getRouter()->generate($route, $parameters, UrlGenerator::ABSOLUTE_URL);
211
    }
212
213
    /**
214
     * Renders a twig template.
215
     *
216
     * @param string $name
217
     * @param array $context
218
     *
219
     * @return string
220
     */
221
    public function render($name, array $context = [])
222
    {
223
        return $this->getTwig()->render($name, $context);
224
    }
225
226
    /**
227
     * Adds a flash message.
228
     *
229
     * @param string $type
230
     * @param string $message
231
     */
232
    public function flash($type, $message)
233
    {
234
        $this->getSession()->getFlashBag()->add($type, $message);
235
    }
236
237
    /**
238
     * Checks if user is granted a role.
239
     *
240
     * @param string $role
241
     *
242
     * @return bool
243
     */
244
    public function isGranted($role)
245
    {
246
        return $this->application['security.authorization_checker']->isGranted($role);
247
    }
248
249
    /**
250
     * Get a service from the container
251
     *
252
     * @param string $service
253
     *
254
     * @return mixed
255
     */
256
    public function get($service)
257
    {
258
        return $this->application[$service];
259
    }
260
261
    /**
262
     * Gets a service from the container.
263
     *
264
     * @param string $property
265
     *
266
     * @return mixed
267
     */
268
    public function __get($property)
269
    {
270
        return $this->application[$property];
271
    }
272
}
273