PopulateRequestResponse::__invoke()   B
last analyzed

Complexity

Conditions 9
Paths 144

Size

Total Lines 72
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 9
eloc 41
c 2
b 0
f 1
nc 144
nop 3
dl 0
loc 72
rs 7.4151

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * PHPPgAdmin 6.1.3
5
 */
6
7
namespace PHPPgAdmin\Middleware;
8
9
/**
10
 * Set the requestobj and responseobj properties of the container
11
 * as the value of $request and $response, which already contain the route.
12
 */
13
class PopulateRequestResponse extends Middleware
14
{
15
    use \PHPPgAdmin\Traits\HelperTrait;
16
17
    public function __invoke(
18
        \Slim\Http\Request $request,
19
        \Slim\Http\Response $response,
20
        $next
21
    ) {
22
        $container = $this->container;
23
        $subfolder = $this->container->getSubfolder();
24
25
        $route = $request->getAttribute('route');
26
27
        $container['server'] = $request->getParam('server');
28
        $container['database'] = $request->getParam('database');
29
        $container['schema'] = $request->getParam('schema');
30
        $misc = $container->get('misc');
31
32
        $view = $this->getViewManager($container);
33
34
        $misc->setHREF();
35
        $view->setForm();
36
37
        $view->offsetSet('METHOD', $request->getMethod());
38
39
        if ($route) {
40
            $view->offsetSet('subject', $route->getArgument('subject'));
41
            $container['server'] = $route->getArgument('server', $request->getParam('server'));
42
        }
43
44
        $request = $request->withUri($this->getUri($request)->withBasePath($subfolder));
45
        $uri = $request->getUri();
46
        $query_string = $uri->getQuery();
47
        $requestPath = $uri->getPath();
48
49
        $view->offsetSet('query_string', $query_string);
50
        $path = $requestPath . ($query_string ? '?' . $query_string : '');
51
        $view->offsetSet('path', $path);
52
53
        $params = $request->getParams();
54
55
        $viewparams = [];
56
57
        foreach ($params as $key => $value) {
58
            if (\is_scalar($value)) {
59
                $viewparams[$key] = $value;
60
            }
61
        }
62
63
        if (isset($_COOKIE['IN_TEST'])) {
64
            $in_test = (string) $_COOKIE['IN_TEST'];
65
        } else {
66
            $in_test = '0';
67
        }
68
69
        // remove tabs and linebreaks from query
70
        if (isset($params['query'])) {
71
            $viewparams['query'] = \str_replace(["\r", "\n", "\t"], ' ', $params['query']);
72
        }
73
        $view->offsetSet('params', $viewparams);
74
        $view->offsetSet('in_test', $in_test);
75
76
        if (0 < \count($container['errors'])) {
77
            return ($container->haltHandler)($request, $response, $container['errors'], 412);
78
        }
79
        $enqueued_reload_browser = ($container->flash->getFirstMessage('reload_browser') ?? false);
80
81
        if ($enqueued_reload_browser) {
82
            $view->setReloadBrowser($enqueued_reload_browser);
83
        }
84
        // First execute anything else
85
        $response = $next($request, $response);
86
87
        // Any other request, pass on current response
88
        return $response;
89
    }
90
91
    private function getUri(\Slim\Http\Request $request): \Slim\Http\Uri
92
    {
93
        return $request->getUri();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $request->getUri() returns the type Psr\Http\Message\UriInterface which includes types incompatible with the type-hinted return Slim\Http\Uri.
Loading history...
94
    }
95
96
    private function getViewManager(\PHPPgAdmin\ContainerUtils $container): \PHPPgAdmin\ViewManager
97
    {
98
        return $container->get('view');
99
    }
100
}
101