Test Failed
Pull Request — master (#19)
by Flo
04:47
created

AbstractController::route()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 25
Code Lines 12

Duplication

Lines 8
Ratio 32 %

Importance

Changes 0
Metric Value
dl 8
loc 25
rs 8.439
c 0
b 0
f 0
cc 5
eloc 12
nc 9
nop 2
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
     * @return string
163
     * @throws RouteInvalidException
164
     */
165
    public function route(string $name, array $parameters = [])
166
    {
167
        /** @var Config $config */
168
        $config = $this->getServiceLocator()->get(Config::class);
169
        $routes = $config->get('routes');
170
171 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...
172
173
            if ($routeName === $name) {
174
                $path = preg_replace('|/\((.*)\)|', '', $routeConfig['path']);
175
                break;
176
            }
177
178
        }
179
180
        if (empty($path)) {
181
            throw new RouteInvalidException('No route for name "' . $name . '" found');
182
        }
183
184
        if (!empty($parameters)) {
185
            $path = $path . implode('/', $parameters);
186
        }
187
188
        return $path;
189
    }
190
191
    /**
192
     * @return Request
193
     */
194
    public function getRequest() :Request
195
    {
196
        return $this->request;
197
    }
198
199
}