Passed
Push — master ( 6c48fd...75d922 )
by Orkhan
01:51
created

Middleware   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 9
c 1
b 0
f 0
dl 0
loc 45
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A shouldCheck() 0 3 1
A realIp() 0 3 1
1
<?php
2
3
namespace Orkhanahmadov\LaravelIpMiddleware;
4
5
use Illuminate\Contracts\Config\Repository;
6
use Illuminate\Contracts\Foundation\Application;
7
use Illuminate\Http\Request;
8
use Illuminate\Http\Response;
9
10
abstract class Middleware
11
{
12
    /**
13
     * @var Application
14
     */
15
    protected $application;
16
    /**
17
     * @var Repository
18
     */
19
    protected $config;
20
    /**
21
     * @var int
22
     */
23
    protected $errorCode = Response::HTTP_FORBIDDEN;
24
25
    /**
26
     * IpMiddleware constructor.
27
     *
28
     * @param Application $application
29
     * @param Repository $config
30
     */
31
    public function __construct(Application $application, Repository $config)
32
    {
33
        $this->application = $application;
34
        $this->config = $config;
35
        $this->errorCode = $config->get('ip-middleware.error_code');
36
    }
37
38
    /**
39
     * @param Request $request
40
     * @return string
41
     */
42
    protected function realIp($request): string
43
    {
44
        return $request->server('HTTP_CF_CONNECTING_IP') ?? $request->ip();
45
    }
46
47
    /**
48
     * Return result if middleware should IP check.
49
     *
50
     * @return bool
51
     */
52
    protected function shouldCheck(): bool
53
    {
54
        return !$this->application->environment($this->config->get('ip-middleware.ignore_environments'));
55
    }
56
}
57