Completed
Push — master ( 1816db...8d2d15 )
by Alexis
06:17
created

Controller::getRouter()   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 AWurth\SilexUser\Entity\UserInterface;
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\Routing\Generator\UrlGenerator;
13
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
14
use Symfony\Component\Validator\Validator\RecursiveValidator;
15
use Twig_Environment;
16
17
/**
18
 * @property EventDispatcher    dispatcher
19
 * @property RecursiveValidator validator
20
 * @property Session            session
21
 * @property Twig_Environment   twig
22
 * @property UrlGenerator       url_generator
23
 */
24
class Controller
25
{
26
    /**
27
     * @var Application
28
     */
29
    protected $application;
30
31
    /**
32
     * Constructor.
33
     *
34
     * @param Application $app
35
     */
36
    public function __construct(Application $app)
37
    {
38
        $this->application = $app;
39
    }
40
41
    /**
42
     * Gets Doctrine Entity Manager.
43
     *
44
     * @return EntityManager
45
     */
46
    public function getEntityManager()
47
    {
48
        return $this->application['orm.em'];
49
    }
50
51
    /**
52
     * Gets the Form Factory.
53
     *
54
     * @return FormFactory
55
     */
56
    public function getFormFactory()
57
    {
58
        return $this->application['form.factory'];
59
    }
60
61
    /**
62
     * Gets the router.
63
     *
64
     * @return UrlGenerator
65
     */
66
    public function getRouter()
67
    {
68
        return $this->application['url_generator'];
69
    }
70
71
    /**
72
     * Gets the session.
73
     *
74
     * @return Session
75
     */
76
    public function getSession()
77
    {
78
        return $this->application['session'];
79
    }
80
81
    /**
82
     * Gets the Twig service.
83
     *
84
     * @return Twig_Environment
85
     */
86
    public function getTwig()
87
    {
88
        return $this->application['twig'];
89
    }
90
91
    /**
92
     * Redirects the user to another route.
93
     *
94
     * @param string $route
95
     * @param array $parameters
96
     * @param int $status
97
     *
98
     * @return RedirectResponse
99
     */
100
    public function redirect($route, $parameters = [], $status = 302)
101
    {
102
        return $this->application->redirect($this->path($route, $parameters), $status);
103
    }
104
105
    /**
106
     * Redirects the user to another URL.
107
     *
108
     * @param string $url The URL to redirect to
109
     * @param int $status The status code (302 by default)
110
     *
111
     * @return RedirectResponse
112
     */
113
    public function redirectTo($url, $status = 302)
114
    {
115
        return $this->application->redirect($url, $status);
116
    }
117
118
    /**
119
     * Generates a path from the given parameters.
120
     *
121
     * @param string $route
122
     * @param mixed $parameters
123
     *
124
     * @return string
125
     */
126
    public function path($route, $parameters = [])
127
    {
128
        return $this->getRouter()->generate($route, $parameters, UrlGenerator::ABSOLUTE_PATH);
129
    }
130
131
    /**
132
     * Generates an absolute URL from the given parameters.
133
     *
134
     * @param string $route
135
     * @param mixed $parameters
136
     *
137
     * @return string
138
     */
139
    public function url($route, $parameters = [])
140
    {
141
        return $this->getRouter()->generate($route, $parameters, UrlGenerator::ABSOLUTE_URL);
142
    }
143
144
    /**
145
     * Renders a twig template.
146
     *
147
     * @param string $name
148
     * @param array $context
149
     *
150
     * @return string
151
     */
152
    public function render($name, array $context = [])
153
    {
154
        return $this->application['twig']->render($name, $context);
155
    }
156
157
    /**
158
     * Adds a flash message.
159
     *
160
     * @param string $type
161
     * @param string $message
162
     */
163
    public function flash($type, $message)
164
    {
165
        $this->getSession()->getFlashBag()->add($type, $message);
166
    }
167
168
    /**
169
     * Gets the current authenticated user.
170
     *
171
     * @return UserInterface|null
172
     */
173
    public function getUser()
174
    {
175
        /** @var TokenInterface $token */
176
        $token = $this->application['security.token_storage']->getToken();
177
178
        return null !== $token ? $token->getUser() : null;
179
    }
180
181
    /**
182
     * Checks if user is granted a role.
183
     *
184
     * @param string $role
185
     *
186
     * @return bool
187
     */
188
    public function isGranted($role)
189
    {
190
        return $this->application['security.authorization_checker']->isGranted($role);
191
    }
192
193
    /**
194
     * Get a service from the container
195
     *
196
     * @param string $service
197
     *
198
     * @return mixed
199
     */
200
    public function get($service)
201
    {
202
        return $this->application[$service];
203
    }
204
205
    /**
206
     * Gets a service from the container.
207
     *
208
     * @param string $property
209
     *
210
     * @return mixed
211
     */
212
    public function __get($property)
213
    {
214
        return $this->application[$property];
215
    }
216
}
217