Test Setup Failed
Push β€” master ( 373408...f0f097 )
by Php Easy Api
03:20
created

UserBuilderHelper::deleteDeviceToken()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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

149
            $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...
150
        });
151
    }
152
153
    /**
154
     * @return void|mixed
155
     */
156
    protected function updateToken($token=null)
157
    {
158
        //if query status value is true
159
        if($this->auth->params['status']){
160
161
            // we go to the method that produces
162
            // the classical token value and get the token value.
163
            $this->auth->params['token'] = ($token===null) ? $this->auth->getTokenData() : $token;
164
165
            // we update the token value.
166
            // if there is no update, we reset the status value to 0.
167
            $update = $this->auth->params['builder']->update(['token'=>$this->auth->params['token']]);
168
169
            if(!$update){
170
                $this->auth->params['status'] = 0;
171
                $this->auth->params['exception'] = 'update';
172
            }
173
        }
174
    }
175
176
    /**
177
     * save device token for token
178
     *
179
     * @return mixed
180
     */
181
    protected function saveDeviceToken()
182
    {
183
        $token_integer = crc32(md5($this->auth->params['token']));
184
185
        if(!is_null($token_integer)){
0 ignored issues
show
introduced by
The condition is_null($token_integer) is always false.
Loading history...
186
187
            if(DeviceToken::where('user_id',$this->auth->params['authId'])
188
                ->where('device_agent_integer',crc32(md5($_SERVER['HTTP_USER_AGENT'])))->count()==0){
189
                return DeviceToken::create([
190
                    'user_id' => $this->auth->params['authId'],
191
                    'token' => $this->auth->params['token'],
192
                    'token_integer' => $token_integer,
193
                    'device_agent' => $_SERVER['HTTP_USER_AGENT'],
194
                    'device_agent_integer' => crc32(md5($_SERVER['HTTP_USER_AGENT'])),
195
                    'expire' => 0
196
                ]);
197
            }
198
            else{
199
200
                return DeviceToken::where('user_id',$this->auth->params['authId'])
201
                    ->where('device_agent_integer',crc32(md5($_SERVER['HTTP_USER_AGENT'])))
202
                    ->update([
203
                    'token' => $this->auth->params['token'],
204
                    'token_integer' => $token_integer
205
                ]);
206
            }
207
208
        }
209
210
    }
211
212
    /**
213
     * delete device token for token
214
     *
215
     * @return mixed
216
     */
217
    protected function deleteDeviceToken()
218
    {
219
        $token_integer = crc32(md5($this->auth->getTokenData()));
220
221
        if(!is_null($token_integer)){
0 ignored issues
show
introduced by
The condition is_null($token_integer) is always false.
Loading history...
222
223
            if(DeviceToken::where('user_id',$this->auth->params['authId'])
224
                    ->where('device_agent_integer',crc32(md5($_SERVER['HTTP_USER_AGENT'])))->count()){
225
226
                DeviceToken::where('token_integer',$token_integer)->delete();
227
            }
228
        }
229
230
    }
231
}
232