IpChecker::__construct()   A
last analyzed

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
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace HayriCan\IpChecker\Http\Middleware;
4
5
/**
6
 * Laravel IP Checker
7
 *
8
 * @author    Hayri Can BARÇIN <hayricanbarcin (#) gmail (.) com>
9
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
10
 * @link      https://github.com/HayriCan/laravel-ip-checker
11
 */
12
13
use HayriCan\IpChecker\Contracts\IpCheckerInterface;
14
use Closure;
15
16
class IpChecker
17
{
18
    protected $allowedIps;
19
20
    public function __construct(IpCheckerInterface $ipChecker)
21
    {
22
        $this->allowedIps = $ipChecker;
23
    }
24
25
    /**
26
     * Handle an incoming request.
27
     *
28
     * @param $request
29
     * @param Closure $next
30
     * @return \Illuminate\Http\JsonResponse|mixed
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\JsonResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
     */
32
    public function handle($request, Closure $next)
33
    {
34
        if (!in_array($request->ip(), $this->allowedIps->getIpArray())) {
35
            $code = trans('ipchecker::messages.denied_access.code');
0 ignored issues
show
Bug introduced by
The function trans was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
            $code = /** @scrutinizer ignore-call */ trans('ipchecker::messages.denied_access.code');
Loading history...
36
            $message = trans('ipchecker::messages.denied_access.message');
37
38
            if (in_array(config('ipchecker.api_middleware'),$request->route()->gatherMiddleware())){
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
            if (in_array(/** @scrutinizer ignore-call */ config('ipchecker.api_middleware'),$request->route()->gatherMiddleware())){
Loading history...
39
                $return_array = [
40
                    'success'=>false,
41
                    'code'=>$code,
42
                    'message'=>$message,
43
                ];
44
45
                return response()->json($return_array,200,[],JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
0 ignored issues
show
Bug introduced by
The function response was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
                return /** @scrutinizer ignore-call */ response()->json($return_array,200,[],JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
Loading history...
46
            }
47
48
            return response()->view('ipchecker::error',compact('message','code'));
49
        }
50
51
        return $next($request);
52
    }
53
}
54