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

AbstractController   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 193
Duplicated Lines 4.15 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

Changes 0
Metric Value
wmc 21
lcom 2
cbo 8
dl 8
loc 193
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getServiceLocator() 0 4 1
A getSessionManager() 0 4 1
A getView() 0 13 2
A getDb() 0 4 1
A render() 0 4 1
A requiredPermissions() 0 11 3
A getRequiredPermissions() 0 4 1
A redirect() 0 6 1
A setFlashMessage() 0 5 1
A getFlashMessage() 0 5 1
B route() 8 29 6
A getRequest() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Cache\Cache;
11
use Faulancer\Cache\CacheableInterface;
12
use Faulancer\Exception\RouteInvalidException;
13
use Faulancer\Http\Request;
14
use Faulancer\Http\Response;
15
use Faulancer\Service\AuthenticatorService;
16
use Faulancer\Service\Config;
17
use Faulancer\Service\DbService;
18
use Faulancer\Service\HttpService;
19
use Faulancer\Service\SessionManagerService;
20
use Faulancer\ServiceLocator\ServiceInterface;
21
use Faulancer\Session\SessionManager;
22
use Faulancer\View\ViewController;
23
use Faulancer\ServiceLocator\ServiceLocator;
24
25
/**
26
 * Class AbstractController
27
 */
28
abstract class AbstractController
29
{
30
31
    /**
32
     * Holds the views per controller request
33
     * @var array
34
     */
35
    private $viewArray = [];
36
37
    /**
38
     * @var Request
39
     */
40
    protected $request;
41
42
    /**
43
     * @var array
44
     */
45
    protected $requiredPermissions = [];
46
47
    /**
48
     * AbstractController constructor.
49
     * @param Request $request
50
     */
51
    public function __construct(Request $request)
52
    {
53
        $this->request = $request;
54
    }
55
56
    /**
57
     * Returns the service locator
58
     *
59
     * @return ServiceLocator
60
     */
61
    public function getServiceLocator() :ServiceLocator
62
    {
63
        return ServiceLocator::instance();
64
    }
65
66
    /**
67
     * Returns the session manager
68
     *
69
     * @return SessionManagerService|ServiceInterface
70
     */
71
    public function getSessionManager() :SessionManagerService
72
    {
73
        return $this->getServiceLocator()->get(SessionManagerService::class);
74
    }
75
76
    /**
77
     * Returns the view controller
78
     *
79
     * @return ViewController
80
     */
81
    public function getView() :ViewController
82
    {
83
        $calledClass = get_called_class();
84
85
        if (in_array($calledClass, array_keys($this->viewArray))) {
86
            return $this->viewArray[$calledClass];
87
        }
88
89
        $viewController = new ViewController();
90
        $this->viewArray[$calledClass] = $viewController;
91
92
        return $viewController;
93
    }
94
95
    /**
96
     * Returns the orm/entity manager
97
     *
98
     * @return DbService|ServiceInterface
99
     */
100
    public function getDb() :DbService
101
    {
102
        return $this->getServiceLocator()->get(DbService::class);
103
    }
104
105
    /**
106
     * Render view with given template
107
     *
108
     * @param  string $template
109
     * @param  array $variables
110
     * @return Response
111
     */
112
    public function render(string $template = '', $variables = []) :Response
113
    {
114
        return new Response($this->getView()->setTemplate($template)->setVariables($variables)->render());
115
    }
116
117
    /**
118
     * Set required authentication
119
     *
120
     * @param array $roles
121
     * @return bool
122
     */
123
    public function requiredPermissions($roles = [])
124
    {
125
        /** @var AuthenticatorService $authenticator */
126
        $authenticator = $this->getServiceLocator()->get(AuthenticatorService::class);
127
128
        if (!empty($roles) && $authenticator->isAuthenticated($roles) === false) {
129
            return $authenticator->redirectToAuthentication();
130
        }
131
132
        return true;
133
    }
134
135
    /**
136
     * @return array
137
     */
138
    public function getRequiredPermissions()
139
    {
140
        return $this->requiredPermissions;
141
    }
142
143
    /**
144
     * @param string $uri
145
     * @return bool
146
     */
147
    public function redirect(string $uri) :bool
148
    {
149
        /** @var HttpService $httpService */
150
        $httpService = $this->getServiceLocator()->get(HttpService::class);
151
        return $httpService->redirect($uri);
152
    }
153
154
    /**
155
     * @param string $key
156
     * @param string $message
157
     */
158
    public function setFlashMessage(string $key, string $message)
159
    {
160
        $sessionManager = $this->getSessionManager();
161
        $sessionManager->setFlashMessage($key, $message);
162
    }
163
164
    /**
165
     * @param $key
166
     * @return string|null
167
     */
168
    public function getFlashMessage(string $key)
169
    {
170
        $sessionManager = $this->getSessionManager();
171
        return $sessionManager->getFlashMessage($key);
172
    }
173
174
    /**
175
     * @param string $name
176
     * @param array  $parameters
177
     * @param bool   $absolute
178
     *
179
     * @return string
180
     * @throws RouteInvalidException
181
     */
182
    public function route(string $name, array $parameters = [], $absolute = false)
183
    {
184
        /** @var Config $config */
185
        $config = $this->getServiceLocator()->get(Config::class);
186
        $routes = $config->get('routes');
187
188 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...
189
190
            if ($routeName === $name) {
191
                $path = preg_replace('|/\((.*)\)|', '', $routeConfig['path']);
192
                break;
193
            }
194
195
        }
196
197
        if (empty($path)) {
198
            throw new RouteInvalidException('No route for name "' . $name . '" found');
199
        }
200
201
        if (!empty($parameters)) {
202
            $path = $path . '/' . implode('/', $parameters);
203
        }
204
205
        if ($absolute) {
206
            $path = $this->getRequest()->getScheme() . $this->getRequest()->getHost() . $path;
207
        }
208
209
        return $path;
210
    }
211
212
    /**
213
     * @return Request
214
     */
215
    public function getRequest() :Request
216
    {
217
        return $this->request;
218
    }
219
220
}