Test Setup Failed
Push — master ( f0f097...c6f3d6 )
by Php Easy Api
04:31
created

AuthenticateProvider::currentDeviceToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Resta\Authenticate;
4
5
use Resta\Authenticate\Resource\AuthUserManager;
6
use Resta\Authenticate\Resource\AuthCheckManager;
7
use Resta\Authenticate\Resource\AuthLoginManager;
8
use Resta\Authenticate\Resource\AuthLogoutManager;
9
10
class AuthenticateProvider extends ConfigProvider implements AuthenticateContract
11
{
12
    //get auth response,auth exception,auth token and auth basic
13
    use AuthenticateResponse,AuthenticateException,AuthenticateToken,AuthenticateBasic;
0 ignored issues
show
Bug introduced by
The trait Resta\Authenticate\AuthenticateToken requires the property $id which is not provided by Resta\Authenticate\AuthenticateProvider.
Loading history...
14
15
    /**
16
     * @var string
17
     */
18
    protected $guard = 'default';
19
20
    /**
21
     * get all device tokens for authenticate
22
     *
23
     * @return mixed|null
24
     */
25
    public function allDeviceTokens()
26
    {
27
        // we obtain the data value obtained via
28
        // authenticate availability with the help of callback object.
29
        return $this->checkParamsViaAvailability(function(){
30
            return (new AuthUserManager($this->currentDeviceToken(),$this))->allDeviceTokens();
31
        });
32
    }
33
34
    /**
35
     * check if the authenticated of user
36
     *
37
     * @return bool
38
     */
39
    public function check()
40
    {
41
        // header to determine whether
42
        // the token value is present and return a callback.
43
        return $this->checkTokenViaHeaders(function($token){
44
45
            // we send the user-supplied token value
46
            // to the authCheckManager object.
47
            new AuthCheckManager($this,$token);
48
49
            // as a result we send output according to
50
            // the boolean value from the checkResult method.
51
            return $this->getCheckResult();
52
        });
53
    }
54
55
    /**
56
     * get current device token the authenticated user
57
     *
58
     * @return mixed
59
     */
60
    public function currentDeviceToken()
61
    {
62
        // we obtain the data value obtained via
63
        // authenticate availability with the help of callback object.
64
        return $this->checkParamsViaAvailability('data',function($data){
65
            return $data;
66
        });
67
    }
68
69
    /**
70
     * authenticate guard adapter
71
     *
72
     * @param $guard
73
     * @return $this|mixed
74
     *
75
     */
76
    public function guard($guard)
77
    {
78
        $this->guard=$guard;
79
80
        $this->setAuthenticateNeeds();
81
82
        return $this;
83
    }
84
85
    /**
86
     * login for authenticate
87
     *
88
     * @param null|array $credentials
89
     * @param bool $objectReturn
90
     * @return $this|mixed
91
     */
92
    public function login($credentials=null,$objectReturn=false)
93
    {
94
        // we will determine whether
95
        // the http path is correct for this method.
96
        $this->checkProcessHttpMethod('login');
97
98
        // we invoke the login manager and the properties
99
        // that this class creates will inform us about user input.
100
        $loginManager = new AuthLoginManager($credentials,$this);
101
102
        // if you want to see the entire login manager object directly,
103
        // send true to the objectReturn parameter.
104
        if($objectReturn) return $loginManager;
105
106
        // the login value stored in the params property of the login manager object will return a builder object.
107
        // we will return the value of the login state as a boolean using the count method of this builder object.
108
        if($this->getModel()=="Default"){
109
            return $this->getResult();
110
        }
111
112
        return $loginManager->loginProcess();
113
114
    }
115
116
    /**
117
     * logout the authenticated user
118
     *
119
     * @return mixed|void
120
     */
121
    public function logout()
122
    {
123
        // we will determine whether
124
        // the http path is correct for this method.
125
        $this->checkProcessHttpMethod('logout');
126
127
        // header to determine whether
128
        // the token value is present and return a callback.
129
        return $this->checkTokenViaHeaders(function($token){
130
131
            // we send the user-supplied token value
132
            // to the authCheckManager object.
133
            new AuthLogoutManager($this,$token);
134
135
            // as a result we send output according to
136
            // the boolean value from the checkResult method.
137
            return $this->getLogoutResult();
138
        });
139
    }
140
141
    /**
142
     * get data of the authenticated user
143
     *
144
     * @return mixed
145
     */
146
    public function user()
147
    {
148
        // we obtain the user value obtained via
149
        // authenticate availability with the help of callback object.
150
        return $this->checkParamsViaAvailability(function(){
151
            return (new AuthUserManager($this->currentDeviceToken(),$this))->userProcess();
152
        });
153
    }
154
155
    /**
156
     * get token sent by user
157
     *
158
     * @return null|string
159
     */
160
    public function getTokenSentByUser()
161
    {
162
        //get headers
163
        $headers = headers();
164
165
        //get token key from config
166
        $tokenKey = $this->getTokenKey();
167
168
        return (isset($headers[$tokenKey])) ? $headers[$tokenKey][0] : null;
169
    }
170
}
171