Completed
Push — master ( 6f429a...4083cc )
by Alexis
06:02
created

Controller   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 132
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createForm() 0 4 1
A getDispatcher() 0 4 1
A getEntityManager() 0 4 1
A getSession() 0 4 1
A getUserManager() 0 4 1
A getUser() 0 15 3
A path() 0 4 1
A redirect() 0 4 1
A render() 0 4 1
1
<?php
2
3
namespace AWurth\SilexUser\Controller;
4
5
use AWurth\SilexUser\Entity\UserInterface;
6
use AWurth\SilexUser\Entity\UserManagerInterface;
7
use Doctrine\ORM\EntityManager;
8
use Silex\Application;
9
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
10
use Symfony\Component\Form\FormInterface;
11
use Symfony\Component\HttpFoundation\RedirectResponse;
12
use Symfony\Component\HttpFoundation\Session\SessionInterface;
13
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
14
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
15
16
class Controller
17
{
18
    /**
19
     * @var Application
20
     */
21
    protected $application;
22
23
    /**
24
     * Constructor.
25
     *
26
     * @param Application $app
27
     */
28
    public function __construct(Application $app)
29
    {
30
        $this->application = $app;
31
    }
32
33
    /**
34
     * Creates a new form.
35
     *
36
     * @param string $type
37
     * @param mixed $data
38
     * @param array $options
39
     *
40
     * @return FormInterface
41
     */
42
    public function createForm($type, $data = null, array $options = [])
43
    {
44
        return $this->application['form.factory']->create($type, $data, $options);
45
    }
46
47
    /**
48
     * Gets the event dispatcher.
49
     *
50
     * @return EventDispatcherInterface
51
     */
52
    public function getDispatcher()
53
    {
54
        return $this->application['dispatcher'];
55
    }
56
57
    /**
58
     * Gets Doctrine Entity Manager.
59
     *
60
     * @return EntityManager
61
     */
62
    public function getEntityManager()
63
    {
64
        return $this->application['orm.em'];
65
    }
66
67
    /**
68
     * Gets the session.
69
     *
70
     * @return SessionInterface
71
     */
72
    public function getSession()
73
    {
74
        return $this->application['session'];
75
    }
76
77
    /**
78
     * Gets the user manager.
79
     *
80
     * @return UserManagerInterface
81
     */
82
    public function getUserManager()
83
    {
84
        return $this->application['silex_user.user_manager'];
85
    }
86
87
    /**
88
     * Gets a user from the Security Token Storage.
89
     *
90
     * @return UserInterface|null
91
     */
92
    public function getUser()
93
    {
94
        /** @var TokenInterface $token */
95
        $token = $this->application['security.token_storage']->getToken();
96
        if (null === $token) {
97
            return null;
98
        }
99
100
        $user = $token->getUser();
101
        if (!is_object($user)) {
102
            return null;
103
        }
104
105
        return $user;
106
    }
107
108
    /**
109
     * Generates a path from the given parameters.
110
     *
111
     * @param string      $route      The name of the route
112
     * @param mixed       $parameters An array of parameters
113
     *
114
     * @return string The generated path
115
     */
116
    public function path($route, $parameters = [])
117
    {
118
        return $this->application['url_generator']->generate($route, $parameters, UrlGeneratorInterface::ABSOLUTE_PATH);
119
    }
120
121
    /**
122
     * Redirects the user to another route.
123
     *
124
     * @param string $route The route to redirect to
125
     * @param array $parameters An array of parameters
126
     * @param int $status The status code (302 by default)
127
     *
128
     * @return RedirectResponse
129
     */
130
    public function redirect($route, $parameters = [], $status = 302)
131
    {
132
        return $this->application->redirect($this->path($route, $parameters), $status);
133
    }
134
135
    /**
136
     * Renders a template.
137
     *
138
     * @param string $name The template name
139
     * @param array $context An array of parameters to pass to the template
140
     *
141
     * @return string
142
     */
143
    public function render($name, array $context = [])
144
    {
145
        return $this->application['twig']->render($name, $context);
146
    }
147
}
148