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

Middleware::shouldCheck()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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