GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 928b67...8d8dff )
by Renato
12:32
created

MaintenanceModeMiddleware   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 11
Bugs 1 Features 1
Metric Value
wmc 8
c 11
b 1
f 1
lcom 1
cbo 4
dl 0
loc 67
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A handle() 0 12 4
A getIp() 0 10 3
1
<?php
2
3
namespace Rdehnhardt\MaintenanceMode\Http\Middleware;
4
5
use Closure;
6
use Illuminate\Contracts\Foundation\Application;
7
use Illuminate\Http\Request;
8
use Illuminate\Http\Response;
9
use Rdehnhardt\MaintenanceMode\MaintenanceModeService;
10
11
class MaintenanceModeMiddleware
12
{
13
    /**
14
     * Maintenance Mode Service.
15
     *
16
     * @var \Rdehnhardt\MaintenanceMode\MaintenanceModeService
17
     */
18
    protected $maintenance;
19
20
    /**
21
     * Lumen Application Instance.
22
     *
23
     * @var \Laravel\Lumen\Application
24
     */
25
    protected $app;
26
27
    /**
28
     * View Factory.
29
     *
30
     * @var \Illuminate\View\Factory
31
     */
32
    protected $view;
33
34
    public function __construct(MaintenanceModeService $maintenance, Application $app)
35
    {
36
        $this->maintenance = $maintenance;
37
        $this->app = $app;
0 ignored issues
show
Documentation Bug introduced by
It seems like $app of type object<Illuminate\Contra...Foundation\Application> is incompatible with the declared type object<Laravel\Lumen\Application> of property $app.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
38
        $this->view = $app['view'];
39
    }
40
41
    /**
42
     * Handle incoming requests.
43
     *
44
     * @param Request $request
45
     * @param \Closure $next
46
     *
47
     * @return \Symfony\Component\HttpFoundation\Response
48
     * @throws \Symfony\Component\HttpKernel\Exception\HttpException
49
     * @throws \InvalidArgumentException
50
     */
51
    public function handle($request, Closure $next)
52
    {
53
        if ($this->maintenance->isDownMode() && !$this->maintenance->checkAllowedIp($this->getIp())) {
54
            if ($this->view->exists('errors.503')) {
55
                return new Response($this->view->make('errors.503'), 503);
56
            }
57
58
            return $this->app->abort(503, 'The application is down for maintenance.');
59
        }
60
61
        return $next($request);
62
    }
63
64
    /**
65
     * Get client ip
66
     */
67
    private function getIp()
0 ignored issues
show
Coding Style introduced by
getIp uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
68
    {
69
        if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
70
            return $_SERVER['HTTP_CLIENT_IP'];
71
        } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
72
            return $_SERVER['HTTP_X_FORWARDED_FOR'];
73
        } else {
74
            return $_SERVER['REMOTE_ADDR'];
75
        }
76
    }
77
}
78