Test Setup Failed
Push β€” master ( ba2f64...373408 )
by Php Easy Api
04:25
created

UserBuilderHelper::saveDeviceToken()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 3
nop 0
dl 0
loc 23
rs 9.7333
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
78
     */
79
    protected function logoutQuery($token)
80
    {
81
        //we get the model specified for the builder.
82
        $driver=$this->query['driver'];
83
84
        //token query for builder
85
        $query=$driver::where(function($query) use($token) {
86
87
            //where query for token
88
            $query->where('token',$token);
89
90
            // if the addToWhereClosure value is a closure,
91
            // then in this case we actually run
92
            // the closure object and add it to the query value.
93
            $this->queryAddToWhere($query);
94
95
        });
96
97
        return $query;
98
    }
99
100
    /**
101
     * get query add to where
102
     *
103
     * @param $query
104
     * @param array $credentials
105
     * @return mixed
106
     */
107
    protected function queryAddToWhere($query,$credentials=array())
108
    {
109
        // if the addToWhereClosure value is a closure,
110
        // then in this case we actually run
111
        // the closure object and add it to the query value.
112
        if($this->isCallableAddToWhere()){
113
            return $this->query['addToWhere']($query,$credentials);
114
        }
115
    }
116
117
    /**
118
     * set query
119
     *
120
     * @param AuthLoginCredentialsManager $credentials
121
     * @return mixed
122
     */
123
    protected function setQuery($credentials)
124
    {
125
        //we get the model specified for the builder.
126
        $driver = $this->query['driver'];
127
128
        if(count($credentials->get())==0){
129
130
            // if the credential array is empty in the config section,
131
            // then you must run the query with a callable value of addToWhere value.
132
            return $this->callbackQueryWithoutCredentials($driver);
133
        }
134
135
        //
136
        if($this->isCallableAddToWhere()){
137
            return $this->queryAddToWhere($driver,$credentials->get());
138
        }
139
140
        // using the driver object we write the query builder statement.
141
        // we do the values of the query with the credentials that are sent.
142
        return $driver::where(function($query) use($credentials) {
143
144
            // with the callback method (eloquent model)
145
            // we write the where clause.
146
            foreach ($credentials->get() as $credential=>$credentialValue){
147
                $query->where($credential,$credentialValue);
148
            }
149
150
            // if the addToWhereClosure value is a closure,
151
            // then in this case we actually run
152
            // the closure object and add it to the query value.
153
            $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

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