CanUpdate::handle()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 14
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 25
rs 9.4888
1
<?php
2
3
namespace Turahe\LaravelInstaller\Middleware;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
use Illuminate\Http\RedirectResponse;
8
use Turahe\LaravelInstaller\Helpers\MigrationsHelper;
9
10
/**
11
 * Class CanUpdate.
12
 */
13
class CanUpdate
14
{
15
    use MigrationsHelper;
16
17
    /**
18
     * Handle an incoming request.
19
     *
20
     * @param  Request  $request
21
     * @param Closure $next
22
     * @return mixed|RedirectResponse
23
     */
24
    public function handle($request, Closure $next): RedirectResponse
25
    {
26
        $updateEnabled = filter_var(config('installer.updaterEnabled'), FILTER_VALIDATE_BOOLEAN);
27
        switch ($updateEnabled) {
28
            case true:
29
                $canInstall = new canInstall;
30
31
                // if the application has not been installed,
32
                // redirect to the installer
33
                if (! $canInstall->alreadyInstalled()) {
34
                    return redirect()->route('LaravelInstaller::welcome');
35
                }
36
37
                if ($this->alreadyUpdated()) {
38
                    abort(404);
39
                }
40
                break;
41
42
            case false:
43
            default:
44
                abort(404);
45
                break;
46
        }
47
48
        return $next($request);
49
    }
50
51
    /**
52
     * If application is already updated.
53
     *
54
     * @return bool
55
     */
56
    public function alreadyUpdated(): bool
57
    {
58
        $migrations = $this->getMigrations();
59
        $dbMigrations = $this->getExecutedMigrations();
60
61
        // If the count of migrations and dbMigrations is equal,
62
        // then the update as already been updated.
63
        if (count($migrations) == count($dbMigrations)) {
64
            return true;
65
        }
66
67
        // Continue, the app needs an update
68
        return false;
69
    }
70
}
71