Completed
Push — feature/6.x ( 5f23eb...e064bf )
by Schlaefer
03:44
created

SaitoBootstrapMiddleware   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
eloc 25
c 4
b 1
f 1
dl 0
loc 67
rs 10
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 37 6
A forceRediret() 0 14 3
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * Saito - The Threaded Web Forum
6
 *
7
 * @copyright Copyright (c) the Saito Project Developers
8
 * @link https://github.com/Schlaefer/Saito
9
 * @license http://opensource.org/licenses/MIT
10
 */
11
12
namespace App\Middleware;
13
14
use Cake\Core\Configure;
15
use Cake\Http\Response;
16
use Cake\ORM\TableRegistry;
17
use Cake\Routing\Router;
18
use Installer\Lib\InstallerState;
19
use Psr\Http\Message\ResponseInterface;
20
use Psr\Http\Message\ServerRequestInterface;
21
use Psr\Http\Server\MiddlewareInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Server\MiddlewareInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use Psr\Http\Server\RequestHandlerInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Server\RequestHandlerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
24
/**
25
 * Loads Settings from DB into Configure
26
 */
27
class SaitoBootstrapMiddleware implements MiddlewareInterface
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
33
    {
34
        /// start installer
35
        $url = $request->getUri()->getPath();
36
        if (!Configure::read('Saito.installed')) {
37
            if (strpos($url, '.')) {
38
                // Don't serve anything except existing assets and installer routes.
39
                // Automatic browser favicon.ico request messes-up installer state.
40
                return new Response(['status' => 503]);
41
            }
42
            $request = $this->forceRediret($request, 'Installer', 'Install');
43
44
            return $handler->handle($request);
45
        } elseif (strpos($url, 'install/finished')) {
46
            /// User has has removed installer token. Installer no longer available.
47
            InstallerState::reset();
48
49
            return (new Response())->withLocation(Router::url('/'));
50
        }
51
52
        /// load settings
53
        $tableLocator = TableRegistry::getTableLocator();
54
        /** @var \App\Model\Table\SettingsTable $settingsTable */
55
        $settingsTable = $tableLocator->get('Settings');
56
        $settingsTable->load(Configure::read('Saito.Settings'));
57
58
        /// start updater
59
        $updated = Configure::read('Saito.updated');
60
        if (!$updated) {
61
            $dbVersion = Configure::read('Saito.Settings.db_version');
62
            $saitoVersion = Configure::read('Saito.v');
63
            if ($dbVersion !== $saitoVersion) {
64
                $request = $this->forceRediret($request, 'Installer', 'Updater', 'start');
65
            }
66
        }
67
68
        return $handler->handle($request);
69
    }
70
71
    /**
72
     * Forces a particular Cake route on the request
73
     *
74
     * @param \Psr\Http\Message\ServerRequestInterface $request The request.
75
     * @param null|string $plugin The plugin.
76
     * @param null|string $controller The controller.
77
     * @param null|string $action The action.
78
     * @return \Psr\Http\Message\ServerRequestInterface
79
     */
80
    protected function forceRediret(
81
        ServerRequestInterface $request,
82
        ?string $plugin = null,
83
        ?string $controller = null,
84
        ?string $action = null
85
    ): ServerRequestInterface {
86
        $params = $request->getAttribute('params', []);
87
        foreach (['plugin', 'controller', 'action'] as $param) {
88
            if ($$param !== null) {
89
                $params[$param] = $$param;
90
            }
91
        }
92
93
        return $request->withAttribute('params', $params);
94
    }
95
}
96