ApiAuthenticate   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
c 1
b 0
f 1
lcom 1
cbo 4
dl 0
loc 48
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B handle() 0 20 6
1
<?php
2
3
/*
4
 * This file is part of Gitamin.
5
 *
6
 * Copyright (C) 2015-2016 The Gitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Gitamin\Http\Middleware;
13
14
use Closure;
15
use Gitamin\Models\User;
16
use Illuminate\Contracts\Auth\Guard;
17
use Illuminate\Database\Eloquent\ModelNotFoundException;
18
use Symfony\Component\HttpKernel\Exception\HttpException;
19
20
class ApiAuthenticate
21
{
22
    /**
23
     * The authentication guard instance.
24
     *
25
     * @var \Illuminate\Contracts\Auth\Guard
26
     */
27
    protected $auth;
28
29
    /**
30
     * Create a new api authenticate middleware instance.
31
     *
32
     * @param \Illuminate\Contracts\Auth\Guard $auth
33
     */
34
    public function __construct(Guard $auth)
35
    {
36
        $this->auth = $auth;
37
    }
38
39
    /**
40
     * Handle an incoming request.
41
     *
42
     * @param \Illuminate\Http\Request $request
43
     * @param \Closure                 $next
44
     *
45
     * @return mixed
46
     */
47
    public function handle($request, Closure $next)
48
    {
49
        if ($this->auth->guest()) {
50
            if ($apiToken = $request->header('X-Gitamin-Token')) {
51
                try {
52
                    $this->auth->onceUsingId(User::findByApiToken($apiToken)->id);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Gitamin\Models\User>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Bug introduced by
It seems like $apiToken defined by $request->header('X-Gitamin-Token') on line 50 can also be of type array; however, Gitamin\Models\User::findByApiToken() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
53
                } catch (ModelNotFoundException $e) {
54
                    throw new HttpException(401);
55
                }
56
            } elseif ($request->getUser()) {
57
                if ($this->auth->onceBasic() !== null) {
58
                    throw new HttpException(401);
59
                }
60
            } else {
61
                throw new HttpException(401);
62
            }
63
        }
64
65
        return $next($request);
66
    }
67
}
68