HasSetting::getSettingName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of Gitamin.
5
 *
6
 * Copyright (C) 2015-2016 The Gitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Gitamin\Http\Middleware;
13
14
use Closure;
15
use Exception;
16
use Gitamin\Facades\Setting;
17
use Illuminate\Support\Facades\Redirect;
18
19
class HasSetting
20
{
21
    /**
22
     * Run the has setting middleware.
23
     *
24
     * We're verifying that the given setting exists in our database. If it
25
     * doesn't, then we're sending the user to the install page so that they can
26
     * complete the installation of Gitamin on their server.
27
     *
28
     * @param \Illuminate\Http\Request $request
29
     * @param \Closure                 $next
30
     *
31
     * @return mixed
32
     */
33
    public function handle($request, Closure $next)
34
    {
35
        $settingName = $this->getSettingName($request);
36
37
        try {
38
            if (! Setting::get($settingName)) {
39
                return Redirect::to('install');
40
            }
41
        } catch (Exception $e) {
42
            return Redirect::to('install');
43
        }
44
45
        return $next($request);
46
    }
47
48
    /**
49
     * Get the setting from the request.
50
     *
51
     * @param \Illuminate\Http\Request $request
52
     *
53
     * @return array
54
     */
55
    private function getSettingName($request)
56
    {
57
        $actions = $request->route()->getAction();
58
59
        return $actions['setting'];
60
    }
61
}
62