Passed
Push — develop ( d334d9...84147f )
by Greg
06:22
created

UseDatabase   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 31
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 13 2
A __construct() 0 2 1
1
<?php
2
/**
3
 * webtrees: online genealogy
4
 * Copyright (C) 2019 webtrees development team
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
 * GNU General Public License for more details.
13
 * You should have received a copy of the GNU General Public License
14
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15
 */
16
declare(strict_types=1);
17
18
namespace Fisharebest\Webtrees\Http\Middleware;
19
20
use function app;
21
use function define;
22
use function file_exists;
23
use Fisharebest\Webtrees\Database;
24
use Fisharebest\Webtrees\Http\Controllers\SetupController;
25
use Fisharebest\Webtrees\Http\Response;
26
use Fisharebest\Webtrees\Webtrees;
27
use Psr\Http\Message\ResponseInterface;
28
use Psr\Http\Message\ServerRequestInterface;
29
use Psr\Http\Server\MiddlewareInterface;
30
use Psr\Http\Server\RequestHandlerInterface;
31
use function parse_ini_file;
32
33
/**
34
 * Middleware to connect to the database.
35
 */
36
class UseDatabase implements MiddlewareInterface
37
{
38
    /** @var SetupController $controller */
39
    private $setup_controller;
40
41
    /**
42
     * @param SetupController $setup_controller
43
     */
44
    public function __construct(SetupController $setup_controller) {
45
        $this->setup_controller = $setup_controller;
46
    }
47
48
    /**
49
     * @param ServerRequestInterface  $request
50
     * @param RequestHandlerInterface $handler
51
     *
52
     * @return Response
53
     */
54
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
55
    {
56
        // Read the connection settings and create the database
57
        if (file_exists(Webtrees::CONFIG_FILE)) {
58
            $database_config = parse_ini_file(Webtrees::CONFIG_FILE);
59
60
            Database::connect($database_config);
0 ignored issues
show
Bug introduced by
It seems like $database_config can also be of type false; however, parameter $config of Fisharebest\Webtrees\Database::connect() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
            Database::connect(/** @scrutinizer ignore-type */ $database_config);
Loading history...
61
62
            return $handler->handle($request);
63
        }
64
65
        // No database connection? Run the setup wizard to create one.
66
        return $this->setup_controller->setup($request);
67
    }
68
}
69