ApiKey::handle()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 16
nc 5
nop 2
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Middleware;
4
5
use App\Model\helpdesk\Settings\System;
6
use Closure;
7
8
class ApiKey
9
{
10
    public $setting;
11
12
    public function __construct()
13
    {
14
        $setting = new System();
15
        $this->setting = $setting;
16
    }
17
18
    /**
19
     * Handle an incoming request.
20
     *
21
     * @param \Illuminate\Http\Request $request
22
     * @param \Closure                 $next
23
     *
24
     * @return mixed
25
     */
26
    public function handle($request, Closure $next)
27
    {
28
        $set = $this->setting->where('id', '1')->first();
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<App\Model\helpdesk\Settings\System>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
29
        if ($set->api_key_mandatory == 1) {
30
            if ($set->api_enable == 1) {
31
                $key = $set->api_key;
32
                $check = $this->test($key, $request->input('api_key'));
33
                if ($check == '1') {
34
                    return $next($request);
35
                }
36
                if ($check == '0') {
37
                    $result = 'wrong api key';
38
39
                    return response()->json(compact('result'));
40
                }
41
            } else {
42
                $result = 'please enable api';
43
44
                return response()->json(compact('result'));
45
            }
46
        } else {
47
            return $next($request);
48
        }
49
    }
50
51
    public function test($v1, $v2)
52
    {
53
        if ($v1 == $v2) {
54
            return '1';
55
        } else {
56
            return '0';
57
        }
58
    }
59
}
60