11
11
* @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html LGPL
12
12
* @link http://www.xpressengine.com
13
13
*/
14
+
14
15
namespace Xpressengine \Plugins \Freezer ;
15
16
16
17
use Carbon \Carbon ;
@@ -65,20 +66,24 @@ public function config($field = null, $default = null)
65
66
public function choose ($ action = 'freeze ' )
66
67
{
67
68
$ users = new Collection ();
68
- if ($ action === 'freeze ' ) {
69
+ if ($ action === 'freeze ' ) {
69
70
$ duration = $ this ->config ('timer ' );
70
71
$ eventDate = Carbon::now ()->subDays ($ duration );
71
72
$ users = $ this ->handler ->where ('loginAt ' , '< ' , $ eventDate )->get ();
72
- } elseif ($ action === 'notify ' ) {
73
+ } elseif ($ action === 'notify ' ) {
73
74
$ duration = $ this ->config ('notify_timer ' );
74
75
$ eventDate = Carbon::now ()->subDays ($ duration );
75
- $ candidates = User::where ('loginAt ' , '< ' , $ eventDate )->with (['freeze_logs ' => function ($ q ) {
76
- return $ q ->orderBy ('createdAt ' , 'desc ' );
77
- }])->get ();
76
+ $ candidates = User::where ('loginAt ' , '< ' , $ eventDate )->with (
77
+ [
78
+ 'freeze_logs ' => function ($ q ) {
79
+ return $ q ->orderBy ('createdAt ' , 'desc ' );
80
+ }
81
+ ]
82
+ )->get ();
78
83
79
84
foreach ($ candidates as $ user ) {
80
85
$ latestLog = $ user ->freeze_logs ->first ();
81
- if (data_get ($ latestLog , 'action ' ) !== 'notify ' ) {
86
+ if (data_get ($ latestLog , 'action ' ) !== 'notify ' ) {
82
87
$ users ->add ($ user );
83
88
}
84
89
}
@@ -99,25 +104,24 @@ public function notify($users = null)
99
104
$ size = $ this ->config ('queue_size ' , 1 );
100
105
$ queue = $ this ->config ('queue.notify ' , 'sync ' );
101
106
102
- if ($ users === null ) {
107
+ if ($ users === null ) {
103
108
$ users = $ this ->choose ('notify ' );
104
109
}
105
110
106
111
$ user_ids = [];
107
112
foreach ($ users as $ user ) {
108
113
$ user_ids [] = $ user ->id ;
109
114
110
- if (count ($ user_ids ) === $ size ) {
115
+ if (count ($ user_ids ) === $ size ) {
111
116
$ this ->dispatch ((new NotifyJob ($ user_ids , $ freezeType ))->onQueue ($ queue ));
112
117
$ user_ids = [];
113
118
}
114
119
}
115
- if (count ($ user_ids )) {
116
- $ this ->dispatch (new NotifyJob ($ user_ids , $ freezeType ));
120
+ if (count ($ user_ids )) {
121
+ $ this ->dispatch (( new NotifyJob ($ user_ids , $ freezeType))-> onQueue ( $ queue ));
117
122
}
118
123
119
124
return $ users ->count ();
120
-
121
125
}
122
126
123
127
/**
@@ -133,28 +137,28 @@ public function freeze($users = null)
133
137
$ size = $ this ->config ('queue_size ' , 1 );
134
138
$ queue = $ this ->config ('queue.freeze ' , 'sync ' );
135
139
136
- if ($ users === null ) {
140
+ if ($ users === null ) {
137
141
$ users = $ this ->choose ('freeze ' );
138
142
}
139
143
140
144
$ user_ids = [];
141
145
foreach ($ users as $ user ) {
142
146
$ user_ids [] = $ user ->id ;
143
147
144
- if (count ($ user_ids ) === $ size ) {
145
- $ this ->dispatch (new FreezeJob ($ user_ids , $ freezeType ));
148
+ if (count ($ user_ids ) === $ size ) {
149
+ $ this ->dispatch (( new FreezeJob ($ user_ids , $ freezeType))-> onQueue ( $ queue ));
146
150
$ user_ids = [];
147
151
}
148
152
}
149
- if (count ($ user_ids )) {
153
+ if (count ($ user_ids )) {
150
154
$ this ->dispatch ((new FreezeJob ($ user_ids , $ freezeType ))->onQueue ($ queue ));
151
155
}
152
156
return $ users ->count ();
153
157
}
154
158
155
159
public function freezeUser ($ user_ids )
156
160
{
157
- if (is_string ($ user_ids )) {
161
+ if (is_string ($ user_ids )) {
158
162
$ user_ids = [$ user_ids ];
159
163
}
160
164
@@ -165,16 +169,16 @@ public function freezeUser($user_ids)
165
169
$ this ->moveData ('freeze ' , $ user ->id );
166
170
$ this ->sendEmail ($ user , 'freeze ' );
167
171
} catch (\Exception $ e ) {
168
- $ this ->logging ($ user ->id , 'freeze ' , ['message ' => $ e ->getMessage ()], 'failed ' );
172
+ $ this ->logging ($ user ->id , 'freeze ' , ['message ' => $ e ->getMessage ()], 'failed ' );
169
173
throw $ e ;
170
174
}
171
- $ this ->logging ($ user ->id , 'freeze ' , $ user ->toArray (), 'successed ' );
175
+ $ this ->logging ($ user ->id , 'freeze ' , $ user ->toArray (), 'successed ' );
172
176
}
173
177
}
174
178
175
179
public function deleteUser ($ user_ids )
176
180
{
177
- if (is_string ($ user_ids )) {
181
+ if (is_string ($ user_ids )) {
178
182
$ user_ids = [$ user_ids ];
179
183
}
180
184
@@ -185,7 +189,7 @@ public function deleteUser($user_ids)
185
189
$ this ->handler ->leave ($ user ->id );
186
190
$ this ->sendEmail ($ user , 'delete ' );
187
191
} catch (\Exception $ e ) {
188
- $ this ->logging ($ user ->id , 'delete ' , ['message ' => $ e ->getMessage ()], 'failed ' );
192
+ $ this ->logging ($ user ->id , 'delete ' , ['message ' => $ e ->getMessage ()], 'failed ' );
189
193
throw $ e ;
190
194
}
191
195
$ this ->logging ($ user ->id , 'delete ' );
@@ -194,7 +198,7 @@ public function deleteUser($user_ids)
194
198
195
199
public function notifyUser ($ user_ids )
196
200
{
197
- if (is_string ($ user_ids )) {
201
+ if (is_string ($ user_ids )) {
198
202
$ user_ids = [$ user_ids ];
199
203
}
200
204
@@ -204,7 +208,7 @@ public function notifyUser($user_ids)
204
208
try {
205
209
$ this ->sendEmail ($ user , 'notify ' );
206
210
} catch (\Exception $ e ) {
207
- $ this ->logging ($ user ->id , 'notify ' , ['message ' => $ e ->getMessage ()], 'failed ' );
211
+ $ this ->logging ($ user ->id , 'notify ' , ['message ' => $ e ->getMessage ()], 'failed ' );
208
212
throw $ e ;
209
213
}
210
214
$ this ->logging ($ user ->id , 'notify ' );
@@ -218,48 +222,50 @@ public function unfreeze($user_id)
218
222
$ user = $ this ->handler ->find ($ user_id );
219
223
$ this ->sendEmail ($ user , 'unfreeze ' );
220
224
} catch (\Exception $ e ) {
221
- $ this ->logging ($ user_id , 'unfreeze ' , ['message ' => $ e ->getMessage ()], 'failed ' );
225
+ $ this ->logging ($ user_id , 'unfreeze ' , ['message ' => $ e ->getMessage ()], 'failed ' );
222
226
throw $ e ;
223
227
}
224
228
$ this ->logging ($ user ->id , 'unfreeze ' );
225
229
}
226
230
227
231
protected function moveData ($ type , $ user_id )
228
232
{
229
- if ($ type === 'freeze ' ) {
230
- $ origin = 'origin ' ; $ target = 'target ' ;
233
+ if ($ type === 'freeze ' ) {
234
+ $ origin = 'origin ' ;
235
+ $ target = 'target ' ;
231
236
} else {
232
- $ origin = 'target ' ; $ target = 'origin ' ;
237
+ $ origin = 'target ' ;
238
+ $ target = 'origin ' ;
233
239
}
234
240
235
241
// copy user table
236
242
$ table = ['origin ' => 'user ' , 'target ' => 'freezer_user ' ];
237
243
$ user = DB ::table ($ table [$ origin ])->find ($ user_id );
238
244
DB ::table ($ table [$ origin ])->delete ($ user_id );
239
- DB ::table ($ table [$ target ])->insert ((array )$ user );
245
+ DB ::table ($ table [$ target ])->insert ((array ) $ user );
240
246
241
247
// copy user_account table
242
248
$ table = ['origin ' => 'user_account ' , 'target ' => 'freezer_user_account ' ];
243
249
$ accounts = DB ::table ($ table [$ origin ])->where ('userId ' , $ user_id )->get ();
244
250
DB ::table ($ table [$ origin ])->where ('userId ' , $ user_id )->delete ();
245
251
foreach ($ accounts as $ account ) {
246
- DB ::table ($ table [$ target ])->insert ((array )$ account );
252
+ DB ::table ($ table [$ target ])->insert ((array ) $ account );
247
253
}
248
254
249
255
// copy user_email table
250
256
$ table = ['origin ' => 'user_email ' , 'target ' => 'freezer_user_email ' ];
251
257
$ emails = DB ::table ($ table [$ origin ])->where ('userId ' , $ user_id )->get ();
252
258
DB ::table ($ table [$ origin ])->where ('userId ' , $ user_id )->delete ();
253
259
foreach ($ emails as $ email ) {
254
- DB ::table ($ table [$ target ])->insert ((array )$ email );
260
+ DB ::table ($ table [$ target ])->insert ((array ) $ email );
255
261
}
256
262
257
263
// copy user_group_user
258
264
$ table = ['origin ' => 'user_group_user ' , 'target ' => 'freezer_user_group_user ' ];
259
265
$ group_users = DB ::table ($ table [$ origin ])->where ('userId ' , $ user_id )->get ();
260
266
DB ::table ($ table [$ origin ])->where ('userId ' , $ user_id )->delete ();
261
267
foreach ($ group_users as $ group ) {
262
- DB ::table ($ table [$ target ])->insert ((array )$ group );
268
+ DB ::table ($ table [$ target ])->insert ((array ) $ group );
263
269
}
264
270
}
265
271
@@ -283,9 +289,9 @@ protected function sendEmail($user, $type)
283
289
$ content = $ content ($ user , $ type , $ this ->config ());
284
290
285
291
// type = freeze, delete, unfreeze, notify
286
- $ view = $ this ->plugin ->view ('views.email ' ) ;
292
+ $ view = $ this ->plugin ->view ('views.email ' );
287
293
$ emailAddr = $ user ->email ;
288
- if ($ emailAddr ) {
294
+ if ($ emailAddr ) {
289
295
app ('mailer ' )->queue (
290
296
$ view ,
291
297
compact ('content ' ),
@@ -300,12 +306,12 @@ function ($message) use ($emailAddr, $subject) {
300
306
public function attempt ($ credentials = [])
301
307
{
302
308
303
- if (array_has ($ credentials , 'password ' )) { // 이메일/비번 로그인
309
+ if (array_has ($ credentials , 'password ' )) { // 이메일/비번 로그인
304
310
$ email = array_get ($ credentials , 'email ' );
305
311
$ userInfo = DB ::table ('freezer_user ' )->where ('email ' , $ email )->first ();
306
- if ($ userInfo !== null ) {
312
+ if ($ userInfo !== null ) {
307
313
$ plain = $ credentials ['password ' ];
308
- if (app ('hash ' )->check ($ plain , $ userInfo ->password )) {
314
+ if (app ('hash ' )->check ($ plain , $ userInfo ->password )) {
309
315
return $ userInfo ->id ;
310
316
}
311
317
}
@@ -316,11 +322,11 @@ public function attempt($credentials = [])
316
322
// account info가 freeze 되어 있다면 바로 반환
317
323
// email info가 freeze 되어 있다면,
318
324
$ accountInfo = DB ::table ('freezer_user_account ' )->where ('accountId ' , $ account_id )->first ();
319
- if ($ accountInfo !== null ) {
325
+ if ($ accountInfo !== null ) {
320
326
return $ accountInfo ->userId ;
321
- } elseif ($ email !== null ) {
327
+ } elseif ($ email !== null ) {
322
328
$ emailInfo = DB ::table ('freezer_user_email ' )->where ('address ' , $ email )->first ();
323
- if ($ emailInfo !== null ) {
329
+ if ($ emailInfo !== null ) {
324
330
return $ emailInfo ->userId ;
325
331
}
326
332
}
@@ -330,10 +336,10 @@ public function attempt($credentials = [])
330
336
if ($ userInfo !== null ) {
331
337
return $ userInfo ->id ;
332
338
}
333
- } elseif (array_has ($ credentials , 'address ' )) { // 이메일 검사
339
+ } elseif (array_has ($ credentials , 'address ' )) { // 이메일 검사
334
340
$ address = array_get ($ credentials , 'address ' );
335
341
$ emailInfo = DB ::table ('freezer_user_email ' )->where ('address ' , $ address )->first ();
336
- if ($ emailInfo !== null ) {
342
+ if ($ emailInfo !== null ) {
337
343
return $ emailInfo ->userId ;
338
344
}
339
345
}
0 commit comments