Test Setup Failed
Push — master ( c33eff...2de497 )
by Php Easy Api
03:48
created

UserBuilderHelper   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 243
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 71
dl 0
loc 243
rs 10
c 0
b 0
f 0
wmc 28

10 Methods

Rating   Name   Duplication   Size   Complexity  
A checkQuery() 0 8 1
A __construct() 0 8 1
A saveDeviceToken() 0 25 3
A setQuery() 0 38 5
A allDeviceTokenQuery() 0 5 1
A updateToken() 0 16 4
A userProcessQuery() 0 6 1
B checkPasswordVerify() 0 22 8
A deleteDeviceToken() 0 9 3
A logoutQuery() 0 8 1
1
<?php
2
3
namespace Resta\Authenticate\Driver\Eloquent;
4
5
use Resta\Authenticate\Resource\AuthLoginCredentialsManager;
6
use Resta\Authenticate\Resource\AuthUserManager;
7
use Resta\Support\Arr;
8
9
class UserBuilderHelper
10
{
11
    /**
12
     * @var array
13
     */
14
    protected $query = [];
15
16
    /**
17
     * @var null|array
18
     */
19
    protected $credentials;
20
21
    /**
22
     * @var bool
23
     */
24
    protected $passwordVerify = false;
25
26
    /**
27
     * @var $password
0 ignored issues
show
Documentation Bug introduced by
The doc comment $password at position 0 could not be parsed: Unknown type name '$password' at position 0 in $password.
Loading history...
28
     */
29
    private $password;
30
31
    /**
32
     * UserBuilderHelper constructor.
33
     */
34
    public function __construct()
35
    {
36
        //in addition to the default credentials values
37
        // ​​on the user side, a closure method is executed and an extra query occurs.
38
        $this->query['addToWhere'] = $this->auth->getAddToWhere();
39
40
        //we get the model specified for the builder.
41
        $this->query['driver'] = $this->auth->getDriverNamespace();
42
    }
43
44
    /**
45
     * get all device token query
46
     *
47
     * @param AuthUserManager $manager
48
     * @return mixed
49
     */
50
    protected function allDeviceTokenQuery($manager)
51
    {
52
        $userId = $manager->getAuth()->params['userId'];
53
54
        return DeviceToken::where('user_id',$userId)->get();
55
    }
56
57
    /**
58
     * @param $token
59
     * @return mixed
60
     */
61
    protected function checkQuery($token)
62
    {
63
        //token query for builder
64
        return DeviceToken::where(function($query) use($token) {
65
66
            //where query for token
67
            $query->where('token_integer',crc32(md5($token)));
68
            $query->where('device_agent_integer',crc32(md5($_SERVER['HTTP_USER_AGENT'])));
69
        });
70
    }
71
72
    /**
73
     * @param $token
74
     * @return mixed|void
75
     */
76
    protected function logoutQuery($token)
77
    {
78
        //token query for builder
79
        return DeviceToken::where(function($query) use($token) {
80
81
            //where query for token
82
            $query->where('token_integer',crc32(md5($token)));
83
            $query->where('device_agent_integer',crc32(md5($_SERVER['HTTP_USER_AGENT'])));
84
85
        });
86
    }
87
88
    /**
89
     * check pasword verify
90
     *
91
     * @param null|object $query
92
     * @return mixed
93
     */
94
    protected function checkPasswordVerify($query=null)
95
    {
96
        if(is_null($query) && isset($this->credentials['password'])){
97
            if(!is_null($password = $this->auth->provider('password'))
98
                && $password($this->credentials['password'])=='verify'){
99
100
                $this->password = $this->credentials['password'];
101
                $this->passwordVerify = true;
102
                $this->credentials = Arr::removeKey($this->credentials,['password']);
103
104
                return null;
105
            }
106
        }
107
108
        if(is_object($query) && $query->count()){
109
            $password = $query->first()->password;
110
            if(password_verify($this->password,$password)){
111
                return $query;
112
            }
113
        }
114
115
        return null;
116
    }
117
118
    /**
119
     * set query
120
     *
121
     * @param AuthLoginCredentialsManager $credentials
122
     * @return mixed
123
     */
124
    protected function setQuery($credentials)
125
    {
126
        //we get the model specified for the builder.
127
        $driver = $this->query['driver'];
128
129
        //get query credentials
130
        $this->credentials = $credentials->get();
131
132
        $this->checkPasswordVerify();
133
134
        // using the driver object we write the query builder statement.
135
        // we do the values of the query with the credentials that are sent.
136
        $query = $driver::where(function($query) use($credentials) {
0 ignored issues
show
Unused Code introduced by
The import $credentials is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
137
138
            // with the callback method (eloquent model)
139
            // we write the where clause.
140
            foreach ($this->credentials as $credential=>$credentialValue){
141
142
                if(!is_null($provider = $this->auth->provider($credential))){
143
                    $query->where($credential,$provider($credentialValue));
144
                }
145
                else{
146
                    $query->where($credential,$credentialValue);
147
                }
148
            }
149
150
            // for the authenticate query,
151
            // the user can add additional queries by the service provider.
152
            if(!is_null($addQuery = $this->auth->provider('addQuery'))){
153
                $addQuery($query);
154
            }
155
        });
156
157
        if(false === $this->passwordVerify){
158
            return $query;
159
        }
160
161
        return $this->checkPasswordVerify($query);
162
    }
163
164
    /**
165
     * @return void|mixed
166
     */
167
    protected function updateToken($token=null)
168
    {
169
        //if query status value is true
170
        if($this->auth->params['status']){
171
172
            // we go to the method that produces
173
            // the classical token value and get the token value.
174
            $this->auth->params['token'] = ($token===null) ? $this->auth->getTokenData() : $token;
175
176
            // we update the token value.
177
            // if there is no update, we reset the status value to 0.
178
            $update = $this->auth->params['builder']->update(['token'=>$this->auth->params['token']]);
179
180
            if(!$update){
181
                $this->auth->params['status'] = 0;
182
                $this->auth->params['exception'] = 'update';
183
            }
184
        }
185
    }
186
187
    /**
188
     * save device token for token
189
     *
190
     * @return mixed
191
     */
192
    protected function saveDeviceToken()
193
    {
194
        $token_integer = crc32(md5($this->auth->params['token']));
195
196
        if(!is_null($token_integer)){
0 ignored issues
show
introduced by
The condition is_null($token_integer) is always false.
Loading history...
197
198
            if(DeviceToken::where('user_id',$this->auth->params['authId'])
199
                    ->where('device_agent_integer',crc32(md5($_SERVER['HTTP_USER_AGENT'])))->count()==0){
200
201
                return DeviceToken::create([
202
                    'user_id' => $this->auth->params['authId'],
203
                    'token' => $this->auth->params['token'],
204
                    'token_integer' => $token_integer,
205
                    'device_agent' => $_SERVER['HTTP_USER_AGENT'],
206
                    'device_agent_integer' => crc32(md5($_SERVER['HTTP_USER_AGENT'])),
207
                    'expire' => $this->auth->getExpire(),
208
                ]);
209
            }
210
            else{
211
212
                return DeviceToken::where('user_id',$this->auth->params['authId'])
213
                    ->where('device_agent_integer',crc32(md5($_SERVER['HTTP_USER_AGENT'])))
214
                    ->update([
215
                        'token' => $this->auth->params['token'],
216
                        'token_integer' => $token_integer
217
                    ]);
218
            }
219
220
        }
221
222
    }
223
224
    /**
225
     * delete device token for token
226
     *
227
     * @return mixed|void
228
     */
229
    protected function deleteDeviceToken()
230
    {
231
        $token_integer = crc32(md5($this->auth->getTokenSentByUser()));
232
233
        if(!is_null($token_integer)){
0 ignored issues
show
introduced by
The condition is_null($token_integer) is always false.
Loading history...
234
235
            DeviceToken::where('token_integer',$token_integer)->delete();
236
237
            return (DeviceToken::where('token_integer',$token_integer)->count()) ? false : true;
238
        }
239
240
    }
241
242
    /**
243
     * @param AuthUserManager $manager
244
     * @return mixed
245
     */
246
    protected function userProcessQuery($manager)
247
    {
248
        $userId = $manager->getAuth()->params['userId'];
249
        $namespace = $manager->getAuth()->getDriverNamespace();
250
251
        return $namespace::find($userId);
252
    }
253
}
254