Passed
Push — master ( 4d9763...76aa8a )
by James
23:28 queued 10:43
created

Installer::hasNoTables()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 24
rs 9.7998
c 0
b 0
f 0
cc 4
nc 4
nop 0
1
<?php
2
3
/**
4
 * Installer.php
5
 * Copyright (c) 2018 [email protected]
6
 *
7
 * This file is part of Firefly III.
8
 *
9
 * Firefly III is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * Firefly III is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
declare(strict_types=1);
24
25
namespace FireflyIII\Http\Middleware;
26
27
use Closure;
28
use DB;
29
use FireflyIII\Exceptions\FireflyException;
30
use Illuminate\Database\QueryException;
31
use Log;
32
33
/**
34
 * Class Installer
35
 *
36
 * @codeCoverageIgnore
37
 *
38
 */
39
class Installer
40
{
41
    /**
42
     * Handle an incoming request.
43
     *
44
     * @throws FireflyException
45
     *
46
     * @param  \Illuminate\Http\Request $request
47
     * @param  \Closure                 $next
48
     *
49
     * @return mixed
50
     *
51
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
52
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
53
     *
54
     */
55
    public function handle($request, Closure $next)
56
    {
57
        // ignore installer in test environment.
58
        if ('testing' === config('app.env')) {
59
            return $next($request);
60
        }
61
        // don't run installer when already in installer.
62
        $url    = $request->url();
63
        $strpos = stripos($url, '/install');
64
        if (!(false === $strpos)) {
65
            Log::debug(sprintf('URL is %s, will NOT run installer middleware', $url));
66
67
            return $next($request);
68
        }
69
70
        // run installer when no tables are present,
71
        // or when old scheme version
72
        // or when old firefly version
73
        if ($this->hasNoTables() || $this->oldDBVersion() || $this->oldVersion()) {
74
            return response()->redirectTo(route('installer.index'));
75
        }
76
        // update scheme version
77
        // update firefly version
78
79
80
        return $next($request);
81
    }
82
83
    /**
84
     * Is access denied error.
85
     *
86
     * @param string $message
87
     *
88
     * @return bool
89
     */
90
    protected function isAccessDenied(string $message): bool
91
    {
92
        return !(false === stripos($message, 'Access denied'));
93
    }
94
95
    /**
96
     * Is no tables exist error.
97
     *
98
     * @param string $message
99
     *
100
     * @return bool
101
     */
102
    protected function noTablesExist(string $message): bool
103
    {
104
        return !(false === stripos($message, 'Base table or view not found'));
105
    }
106
107
    /**
108
     * Check if the tables are created and accounted for.
109
     *
110
     * @return bool
111
     * @throws FireflyException
112
     */
113
    private function hasNoTables(): bool
114
    {
115
        Log::debug('Now in routine hasNoTables()');
116
117
        try {
118
            DB::table('users')->count();
119
        } catch (QueryException $e) {
120
            $message = $e->getMessage();
121
            Log::error(sprintf('Error message trying to access users-table: %s', $message));
122
            if ($this->isAccessDenied($message)) {
123
                throw new FireflyException('It seems your database configuration is not correct. Please verify the username and password in your .env file.');
124
            }
125
            if ($this->noTablesExist($message)) {
126
                // redirect to UpdateController
127
                Log::warning('There are no Firefly III tables present. Redirect to migrate routine.');
128
129
                return true;
130
131
            }
132
            throw new FireflyException(sprintf('Could not access the database: %s', $message));
133
        }
134
        Log::debug('Everything seems OK with the tables.');
135
136
        return false;
137
138
    }
139
140
    /**
141
     * Check if the "db_version" variable is correct.
142
     *
143
     * @return bool
144
     */
145
    private function oldDBVersion(): bool
146
    {
147
        // older version in config than database?
148
        $configVersion = (int)config('firefly.db_version');
149
        $dbVersion     = (int)app('fireflyconfig')->getFresh('db_version', 1)->data;
150
        if ($configVersion > $dbVersion) {
151
            Log::warning(
152
                sprintf(
153
                    'The current configured version (%d) is older than the required version (%d). Redirect to migrate routine.', $dbVersion, $configVersion
154
                )
155
            );
156
157
            return true;
158
        }
159
        Log::info(sprintf('Configured DB version (%d) equals expected DB version (%d)', $dbVersion, $configVersion));
160
161
        return false;
162
    }
163
164
    /**
165
     * Check if the "firefly_version" variable is correct.
166
     *
167
     * @return bool
168
     */
169
    private function oldVersion(): bool
170
    {
171
        // version compare thing.
172
        $configVersion = (string)config('firefly.version');
173
        $dbVersion     = (string)app('fireflyconfig')->getFresh('ff3_version', '1.0')->data;
174
        if (1 === version_compare($configVersion, $dbVersion)) {
175
            Log::warning(
176
                sprintf(
177
                    'The current configured Firefly III version (%s) is older than the required version (%s). Redirect to migrate routine.', $dbVersion, $configVersion
178
                )
179
            );
180
181
            return true;
182
        }
183
        Log::info(sprintf('Installed Firefly III version (%s) equals expected Firefly III version (%s)', $dbVersion, $configVersion));
184
185
        return false;
186
    }
187
}
188