Passed
Push — master ( 1164b2...5d6ac0 )
by meta
03:55
created

OauthTokenGuard::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Metaclassing\EnterpriseAuth\Middleware;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Contracts\Auth\Guard;
7
use Illuminate\Contracts\Auth\UserProvider;
8
use Illuminate\Contracts\Auth\Authenticatable;
9
10
class OauthTokenGuard implements Guard
11
{
12
    protected $request;
13
    protected $provider;
14
    protected $user;
15
16
    /**
17
     * Create a new authentication guard.
18
     *
19
     * @param  \Illuminate\Contracts\Auth\UserProvider  $provider
20
     * @param  \Illuminate\Http\Request  $request
21
     * @return void
22
     */
23
    public function __construct(UserProvider $provider, Request $request)
24
    {
25
        $this->request = $request;
26
        $this->provider = $provider;
27
        $this->user = null;
28
29
        // use the API auth controller helper functions to check the user creds
30
        $apiAuthController = new \Metaclassing\EnterpriseAuth\Controllers\ApiAuthController();
31
        $this->user = $apiAuthController->authenticateRequest($request);
32
    }
33
34
    /**
35
     * Determine if the current user is authenticated.
36
     *
37
     * @return bool
38
     */
39
    public function check()
40
    {
41
        return ! is_null($this->user());
42
    }
43
44
    /**
45
     * Determine if the current user is a guest.
46
     *
47
     * @return bool
48
     */
49
    public function guest()
50
    {
51
        return ! $this->check();
52
    }
53
54
    /**
55
     * Get the currently authenticated user.
56
     *
57
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
58
     */
59
    public function user()
60
    {
61
        if (! is_null($this->user)) {
62
            return $this->user;
63
        }
64
    }
65
66
    /**
67
     * Get the JSON params from the current request.
68
     *
69
     * @return string
70
     */
71
    /*
72
        public function getJsonParams()
73
        {
74
            $jsondata = $this->request->query('jsondata');
75
76
            return (!empty($jsondata) ? json_decode($jsondata, TRUE) : NULL);
77
        }
78
    /**/
79
80
    /**
81
     * Get the ID for the currently authenticated user.
82
     *
83
     * @return string|null
84
     */
85
    public function id()
86
    {
87
        if ($user = $this->user()) {
0 ignored issues
show
Unused Code introduced by
The assignment to $user is dead and can be removed.
Loading history...
88
            return $this->user()->getAuthIdentifier();
89
        }
90
    }
91
92
    /**
93
     * Validate a user's credentials.
94
     *
95
     * @return bool
96
     */
97
    public function validate(array $credentials = [])
98
    {
99
        return is_null($this->user);
100
    }
101
102
    /**
103
     * Set the current user.
104
     *
105
     * @param  array $user User info
106
     */
107
    public function setUser(Authenticatable $user)
108
    {
109
        $this->user = $user;
110
111
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Metaclassing\EnterpriseA...dleware\OauthTokenGuard which is incompatible with the return type mandated by Illuminate\Contracts\Auth\Guard::setUser() of void.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
112
    }
113
}
114