ApiOptionalAuthenticate::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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
19
class ApiOptionalAuthenticate
20
{
21
    /**
22
     * The authentication guard instance.
23
     *
24
     * @var \Illuminate\Contracts\Auth\Guard
25
     */
26
    protected $auth;
27
28
    /**
29
     * Create a new api authenticate middleware instance.
30
     *
31
     * @param \Illuminate\Contracts\Auth\Guard $auth
32
     */
33
    public function __construct(Guard $auth)
34
    {
35
        $this->auth = $auth;
36
    }
37
38
    /**
39
     * Handle an incoming request.
40
     *
41
     * @param \Illuminate\Http\Request $request
42
     * @param \Closure                 $next
43
     *
44
     * @return mixed
45
     */
46
    public function handle($request, Closure $next)
47
    {
48
        if ($this->auth->guest()) {
49
            if ($apiToken = $request->header('X-Gitamin-Token')) {
50
                try {
51
                    $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 49 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...
52
                } catch (ModelNotFoundException $e) {
53
                    //
54
                }
55
            } elseif ($request->getUser()) {
56
                if ($this->auth->onceBasic() !== null) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
57
                    //
58
                }
59
            }
60
        }
61
62
        return $next($request);
63
    }
64
}
65