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

UserBuilderHelper::userProcessQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Resta\Authenticate\Driver\Eloquent;
4
5
use Resta\Authenticate\Resource\AuthLoginCredentialsManager;
6
use Resta\Authenticate\Resource\AuthUserManager;
7
8
class UserBuilderHelper
9
{
10
    /**
11
     * @var array
12
     */
13
    protected $query = [];
14
15
    /**
16
     * UserBuilderHelper constructor.
17
     */
18
    public function __construct()
19
    {
20
        //in addition to the default credentials values
21
        // ​​on the user side, a closure method is executed and an extra query occurs.
22
        $this->query['addToWhere'] = $this->auth->getAddToWhere();
23
24
        //we get the model specified for the builder.
25
        $this->query['driver'] = $this->auth->getDriverNamespace();
26
    }
27
28
    /**
29
     * get all device token query
30
     *
31
     * @param AuthUserManager $manager
32
     * @return mixed
33
     */
34
    protected function allDeviceTokenQuery($manager)
35
    {
36
        $userId = $manager->getAuth()->params['userId'];
37
38
        return DeviceToken::where('user_id',$userId)->get();
39
    }
40
41
    /**
42
     * @param null|object
43
     * @return mixed
44
     */
45
    protected function callbackQueryWithoutCredentials($driver)
46
    {
47
        if($this->isCallableAddToWhere()){
48
49
            return $driver::where(function($query) {
50
51
                // if the addToWhereClosure value is a closure,
52
                // then in this case we actually run
53
                // the closure object and add it to the query value.
54
                $this->queryAddToWhere($query);
55
            });
56
        }
57
    }
58
59
    /**
60
     * @param $token
61
     * @return mixed
62
     */
63
    protected function checkQuery($token)
64
    {
65
        //token query for builder
66
        return DeviceToken::where(function($query) use($token) {
67
68
            //where query for token
69
            $query->where('token_integer',crc32(md5($token)));
70
            $query->where('device_agent_integer',crc32(md5($_SERVER['HTTP_USER_AGENT'])));
71
72
            // if the addToWhereClosure value is a closure,
73
            // then in this case we actually run
74
            // the closure object and add it to the query value.
75
            $this->queryAddToWhere($query);
76
        });
77
    }
78
79
    /**
80
     * @return bool
81
     */
82
    protected function isCallableAddToWhere()
83
    {
84
        // addToWhere checks whether
85
        // the config value is a callable value.
86
        return is_callable($this->query['addToWhere']);
87
    }
88
89
    /**
90
     * @param $token
91
     * @return mixed|void
92
     */
93
    protected function logoutQuery($token)
94
    {
95
        //token query for builder
96
        return DeviceToken::where(function($query) use($token) {
97
98
            //where query for token
99
            $query->where('token_integer',crc32(md5($token)));
100
            $query->where('device_agent_integer',crc32(md5($_SERVER['HTTP_USER_AGENT'])));
101
102
            // if the addToWhereClosure value is a closure,
103
            // then in this case we actually run
104
            // the closure object and add it to the query value.
105
            $this->queryAddToWhere($query);
106
107
        });
108
    }
109
110
    /**
111
     * get query add to where
112
     *
113
     * @param $query
114
     * @param array $credentials
115
     * @return mixed
116
     */
117
    protected function queryAddToWhere($query,$credentials=array())
118
    {
119
        // if the addToWhereClosure value is a closure,
120
        // then in this case we actually run
121
        // the closure object and add it to the query value.
122
        if($this->isCallableAddToWhere()){
123
            return $this->query['addToWhere']($query,$credentials);
124
        }
125
    }
126
127
    /**
128
     * set query
129
     *
130
     * @param AuthLoginCredentialsManager $credentials
131
     * @return mixed
132
     */
133
    protected function setQuery($credentials)
134
    {
135
        //we get the model specified for the builder.
136
        $driver = $this->query['driver'];
137
138
        if(count($credentials->get())==0){
139
140
            // if the credential array is empty in the config section,
141
            // then you must run the query with a callable value of addToWhere value.
142
            return $this->callbackQueryWithoutCredentials($driver);
143
        }
144
145
        //
146
        if($this->isCallableAddToWhere()){
147
            return $this->queryAddToWhere($driver,$credentials->get());
148
        }
149
150
        // using the driver object we write the query builder statement.
151
        // we do the values of the query with the credentials that are sent.
152
        return $driver::where(function($query) use($credentials) {
153
154
            // with the callback method (eloquent model)
155
            // we write the where clause.
156
            foreach ($credentials->get() as $credential=>$credentialValue){
157
                $query->where($credential,$credentialValue);
158
            }
159
160
            // if the addToWhereClosure value is a closure,
161
            // then in this case we actually run
162
            // the closure object and add it to the query value.
163
            $this->queryAddToWhere($query,$credentials->get(),$credentials->get());
0 ignored issues
show
Unused Code introduced by
The call to Resta\Authenticate\Drive...lper::queryAddToWhere() has too many arguments starting with $credentials->get(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

163
            $this->/** @scrutinizer ignore-call */ queryAddToWhere($query,$credentials->get(),$credentials->get());

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
164
        });
165
    }
166
167
    /**
168
     * @return void|mixed
169
     */
170
    protected function updateToken($token=null)
171
    {
172
        //if query status value is true
173
        if($this->auth->params['status']){
174
175
            // we go to the method that produces
176
            // the classical token value and get the token value.
177
            $this->auth->params['token'] = ($token===null) ? $this->auth->getTokenData() : $token;
178
179
            // we update the token value.
180
            // if there is no update, we reset the status value to 0.
181
            $update = $this->auth->params['builder']->update(['token'=>$this->auth->params['token']]);
182
183
            if(!$update){
184
                $this->auth->params['status'] = 0;
185
                $this->auth->params['exception'] = 'update';
186
            }
187
        }
188
    }
189
190
    /**
191
     * save device token for token
192
     *
193
     * @return mixed
194
     */
195
    protected function saveDeviceToken()
196
    {
197
        $token_integer = crc32(md5($this->auth->params['token']));
198
199
        if(!is_null($token_integer)){
0 ignored issues
show
introduced by
The condition is_null($token_integer) is always false.
Loading history...
200
201
            if(DeviceToken::where('user_id',$this->auth->params['authId'])
202
                ->where('device_agent_integer',crc32(md5($_SERVER['HTTP_USER_AGENT'])))->count()==0){
203
204
                return DeviceToken::create([
205
                    'user_id' => $this->auth->params['authId'],
206
                    'token' => $this->auth->params['token'],
207
                    'token_integer' => $token_integer,
208
                    'device_agent' => $_SERVER['HTTP_USER_AGENT'],
209
                    'device_agent_integer' => crc32(md5($_SERVER['HTTP_USER_AGENT'])),
210
                    'expire' => $this->auth->getExpire(),
211
                ]);
212
            }
213
            else{
214
215
                return DeviceToken::where('user_id',$this->auth->params['authId'])
216
                    ->where('device_agent_integer',crc32(md5($_SERVER['HTTP_USER_AGENT'])))
217
                    ->update([
218
                    'token' => $this->auth->params['token'],
219
                    'token_integer' => $token_integer
220
                ]);
221
            }
222
223
        }
224
225
    }
226
227
    /**
228
     * delete device token for token
229
     *
230
     * @return mixed
231
     */
232
    protected function deleteDeviceToken()
233
    {
234
        $token_integer = crc32(md5($this->auth->getTokenSentByUser()));
235
236
        if(!is_null($token_integer)){
0 ignored issues
show
introduced by
The condition is_null($token_integer) is always false.
Loading history...
237
238
            DeviceToken::where('token_integer',$token_integer)->delete();
239
            
240
            return (DeviceToken::where('token_integer',$token_integer)->count()) ? false : true;
241
        }
242
243
    }
244
245
    /**
246
     * @param AuthUserManager $manager
247
     * @return mixed
248
     */
249
    protected function userProcessQuery($manager)
250
    {
251
        $userId = $manager->getAuth()->params['userId'];
252
        $namespace = $manager->getAuth()->getDriverNamespace();
253
254
        return $namespace::find($userId);
255
    }
256
}
257