Completed
Push — master ( 5022d5...4886b8 )
by Alexis
06:42
created

Controller::getEnv()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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