Test Setup Failed
Pull Request — master (#19)
by Flo
03:33
created

AbstractController   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 217
Duplicated Lines 18.89 %

Coupling/Cohesion

Components 2
Dependencies 9

Importance

Changes 0
Metric Value
wmc 21
lcom 2
cbo 9
dl 41
loc 217
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 isPermitted() 0 6 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
B __call() 33 33 3

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\Exception\PluginException;
11
use Faulancer\Exception\RouteInvalidException;
12
use Faulancer\Fixture\Entity\UserEntity;
13
use Faulancer\Http\Request;
14
use Faulancer\Http\Response;
15
use Faulancer\Plugin\AbstractPlugin;
16
use Faulancer\Service\AuthenticatorService;
17
use Faulancer\Service\Config;
18
use Faulancer\Service\DbService;
19
use Faulancer\Service\HttpService;
20
use Faulancer\Service\SessionManagerService;
21
use Faulancer\ServiceLocator\ServiceInterface;
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
     * AbstractController constructor.
44
     * @param Request $request
45
     */
46
    public function __construct(Request $request)
47
    {
48
        $this->request = $request;
49
    }
50
51
    /**
52
     * Returns the service locator
53
     *
54
     * @return ServiceLocator
55
     */
56
    public function getServiceLocator() :ServiceLocator
57
    {
58
        return ServiceLocator::instance();
59
    }
60
61
    /**
62
     * Returns the session manager
63
     *
64
     * @return SessionManagerService|ServiceInterface
65
     */
66
    public function getSessionManager() :SessionManagerService
67
    {
68
        return $this->getServiceLocator()->get(SessionManagerService::class);
69
    }
70
71
    /**
72
     * Returns the view controller
73
     *
74
     * @return ViewController
75
     */
76
    public function getView() :ViewController
77
    {
78
        $calledClass = get_called_class();
79
80
        if (in_array($calledClass, array_keys($this->viewArray))) {
81
            return $this->viewArray[$calledClass];
82
        }
83
84
        $viewController = new ViewController();
85
        $this->viewArray[$calledClass] = $viewController;
86
87
        return $viewController;
88
    }
89
90
    /**
91
     * Returns the orm/entity manager
92
     *
93
     * @return DbService|ServiceInterface
94
     */
95
    public function getDb() :DbService
96
    {
97
        return $this->getServiceLocator()->get(DbService::class);
98
    }
99
100
    /**
101
     * Render view with given template
102
     *
103
     * @param  string $template
104
     * @param  array $variables
105
     * @return Response
106
     */
107
    public function render(string $template = '', $variables = []) :Response
108
    {
109
        return new Response($this->getView()->setTemplate($template)->setVariables($variables)->render());
110
    }
111
112
    /**
113
     * @param array $roles
114
     *
115
     * @return bool|null
116
     */
117
    public function isPermitted($roles = [])
118
    {
119
        /** @var AuthenticatorService $authService */
120
        $authService = $this->getServiceLocator()->get(AuthenticatorService::class);
121
        return $authService->isPermitted($roles);
122
    }
123
124
    /**
125
     * @param string $uri
126
     * @return bool
127
     */
128
    public function redirect(string $uri) :bool
129
    {
130
        /** @var HttpService $httpService */
131
        $httpService = $this->getServiceLocator()->get(HttpService::class);
132
        return $httpService->redirect($uri);
133
    }
134
135
    /**
136
     * @param string $key
137
     * @param string $message
138
     */
139
    public function setFlashMessage(string $key, string $message)
140
    {
141
        $sessionManager = $this->getSessionManager();
142
        $sessionManager->setFlashMessage($key, $message);
143
    }
144
145
    /**
146
     * @param $key
147
     * @return string|null
148
     */
149
    public function getFlashMessage(string $key)
150
    {
151
        $sessionManager = $this->getSessionManager();
152
        return $sessionManager->getFlashMessage($key);
153
    }
154
155
    /**
156
     * @param string $name
157
     * @param array  $parameters
158
     * @param bool   $absolute
159
     *
160
     * @return string
161
     * @throws RouteInvalidException
162
     */
163
    public function route(string $name, array $parameters = [], $absolute = false)
164
    {
165
        /** @var Config $config */
166
        $config = $this->getServiceLocator()->get(Config::class);
167
        $routes = $config->get('routes');
168
169 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...
170
171
            if ($routeName === $name) {
172
                $path = preg_replace('|/\((.*)\)|', '', $routeConfig['path']);
173
                break;
174
            }
175
176
        }
177
178
        if (empty($path)) {
179
            throw new RouteInvalidException('No route for name "' . $name . '" found');
180
        }
181
182
        if (!empty($parameters)) {
183
            $path = $path . implode('/', $parameters);
184
        }
185
186
        if ($absolute) {
187
            $path = $this->getRequest()->getScheme() . $this->getRequest()->getHost() . $path;
188
        }
189
190
        return $path;
191
    }
192
193
    /**
194
     * @return Request
195
     */
196
    public function getRequest() :Request
197
    {
198
        return $this->request;
199
    }
200
201
    /**
202
     * Magic method for providing a view helper
203
     *
204
     * @param  string $name      The class name
205
     * @param  array  $arguments Arguments if given
206
     * @return AbstractPlugin
207
     * @throws PluginException
208
     * @codeCoverageIgnore Not implemented yet
209
     */
210 View Code Duplication
    public function __call($name, $arguments)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
211
    {
212
        // Search in core view helpers first
213
        $corePlugin = 'Faulancer\Plugin\\' . ucfirst($name);
214
215
        if (class_exists($corePlugin)) {
216
217
            $class = new $corePlugin;
218
            array_unshift($arguments, $this);
219
220
            return call_user_func_array($class, $arguments);
221
222
        }
223
224
        // No core implementations found; search in custom view helpers
225
226
        /** @var Config $config */
227
        $config = ServiceLocator::instance()->get(Config::class);
228
        $namespace = '\\' . $config->get('namespacePrefix');
229
230
        $customPlugin = $namespace . '\Plugin\\' . ucfirst($name);
231
232
        if (class_exists($customPlugin)) {
233
234
            $class = new $customPlugin;
235
            array_unshift($arguments, $this);
236
237
            return call_user_func_array($class, $arguments);
238
239
        }
240
241
        throw new PluginException('No plugin for "' . $name . '" found.');
242
    }
243
244
}