Test Failed
Pull Request — master (#19)
by Flo
02:25
created

AbstractController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Class AbstractController
4
 *
5
 * @package Faulancer\AbstractController
6
 * @author Florian Knapp <[email protected]>
7
 */
8
namespace Faulancer\Controller;
9
10
use Faulancer\Exception\RouteInvalidException;
11
use Faulancer\Http\Request;
12
use Faulancer\Http\Response;
13
use Faulancer\Service\AuthenticatorService;
14
use Faulancer\Service\Config;
15
use Faulancer\Service\DbService;
16
use Faulancer\Service\HttpService;
17
use Faulancer\Service\SessionManagerService;
18
use Faulancer\ServiceLocator\ServiceInterface;
19
use Faulancer\Session\SessionManager;
20
use Faulancer\View\ViewController;
21
use Faulancer\ServiceLocator\ServiceLocator;
22
23
/**
24
 * Class AbstractController
25
 */
26
abstract class AbstractController
27
{
28
29
    /**
30
     * Holds the views per controller request
31
     * @var array
32
     */
33
    private $viewArray = [];
34
35
    /**
36
     * @var Request
37
     */
38
    protected $request;
39
40
    /**
41
     * AbstractController constructor.
42
     * @param Request $request
43
     */
44
    public function __construct(Request $request)
45
    {
46
        $this->request = $request;
47
    }
48
49
    /**
50
     * Returns the service locator
51
     *
52
     * @return ServiceLocator
53
     */
54
    public function getServiceLocator() :ServiceLocator
55
    {
56
        return ServiceLocator::instance();
57
    }
58
59
    /**
60
     * Returns the session manager
61
     *
62
     * @return SessionManagerService|ServiceInterface
63
     */
64
    public function getSessionManager() :SessionManagerService
65
    {
66
        return $this->getServiceLocator()->get(SessionManagerService::class);
67
    }
68
69
    /**
70
     * Returns the view controller
71
     *
72
     * @return ViewController
73
     */
74
    public function getView() :ViewController
75
    {
76
        $calledClass = get_called_class();
77
78
        if (in_array($calledClass, array_keys($this->viewArray))) {
79
            return $this->viewArray[$calledClass];
80
        }
81
82
        $viewController = new ViewController();
83
        $this->viewArray[$calledClass] = $viewController;
84
85
        return $viewController;
86
    }
87
88
    /**
89
     * Returns the orm/entity manager
90
     *
91
     * @return DbService|ServiceInterface
92
     */
93
    public function getDb() :DbService
94
    {
95
        return $this->getServiceLocator()->get(DbService::class);
96
    }
97
98
    /**
99
     * Render view with given template
100
     *
101
     * @param  string $template
102
     * @param  array $variables
103
     * @return Response
104
     */
105
    public function render(string $template = '', $variables = []) :Response
106
    {
107
        return new Response($this->getView()->setTemplate($template)->setVariables($variables)->render());
108
    }
109
110
    /**
111
     * Set required authentication
112
     *
113
     * @param array $role
114
     * @return bool
115
     */
116
    public function requireAuth($role) :bool
117
    {
118
        /** @var AuthenticatorService $authenticator */
119
        $authenticator = $this->getServiceLocator()->get(AuthenticatorService::class);
120
121
        if ($authenticator->isAuthenticated($role) === false) {
122
            return $authenticator->redirectToAuthentication();
123
        }
124
125
        return true;
126
    }
127
128
    /**
129
     * @param string $uri
130
     * @return bool
131
     */
132
    public function redirect(string $uri) :bool
133
    {
134
        /** @var HttpService $httpService */
135
        $httpService = $this->getServiceLocator()->get(HttpService::class);
136
        return $httpService->redirect($uri);
137
    }
138
139
    /**
140
     * @param string $key
141
     * @param string $message
142
     */
143
    public function setFlashMessage(string $key, string $message)
144
    {
145
        $sessionManager = $this->getSessionManager();
146
        $sessionManager->setFlashMessage($key, $message);
147
    }
148
149
    /**
150
     * @param $key
151
     * @return string|null
152
     */
153
    public function getFlashMessage(string $key)
154
    {
155
        $sessionManager = $this->getSessionManager();
156
        return $sessionManager->getFlashMessage($key);
157
    }
158
159
    /**
160
     * @param string $name
161
     * @param array  $parameters
162
     * @param bool   $absolute
163
     *
164
     * @return string
165
     * @throws RouteInvalidException
166
     */
167
    public function route(string $name, array $parameters = [], $absolute = false)
168
    {
169
        /** @var Config $config */
170
        $config = $this->getServiceLocator()->get(Config::class);
171
        $routes = $config->get('routes');
172
173 View Code Duplication
        foreach ($routes as $routeName => $routeConfig) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
174
175
            if ($routeName === $name) {
176
                $path = preg_replace('|/\((.*)\)|', '', $routeConfig['path']);
177
                break;
178
            }
179
180
        }
181
182
        if (empty($path)) {
183
            throw new RouteInvalidException('No route for name "' . $name . '" found');
184
        }
185
186
        if (!empty($parameters)) {
187
            $path = $path . '/' . implode('/', $parameters);
188
        }
189
190
        if ($absolute) {
191
            $path = $this->getRequest()->getScheme() . $this->getRequest()->getHost() . $path;
192
        }
193
194
        return $path;
195
    }
196
197
    /**
198
     * @return Request
199
     */
200
    public function getRequest() :Request
201
    {
202
        return $this->request;
203
    }
204
205
}