Passed
Push — master ( 80c01e...797e47 )
by Orkhan
02:21
created

Middleware   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 11
c 1
b 0
f 0
dl 0
loc 49
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A clientIp() 0 7 2
A shouldCheck() 0 3 1
1
<?php
2
3
namespace Orkhanahmadov\LaravelIpMiddleware;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Http\Response;
7
use Illuminate\Contracts\Config\Repository;
8
use Illuminate\Contracts\Foundation\Application;
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 clientIp($request): string
43
    {
44
        if ($serverVariable = $this->config->get('ip-middleware.custom_server_variable')) {
45
            return $request->server($serverVariable) ?? $request->ip();
46
        }
47
48
        return $request->ip();
49
    }
50
51
    /**
52
     * Return result if middleware should IP check.
53
     *
54
     * @return bool
55
     */
56
    protected function shouldCheck(): bool
57
    {
58
        return ! $this->application->environment($this->config->get('ip-middleware.ignore_environments'));
59
    }
60
}
61