Completed
Pull Request — master (#177)
by Corey
02:56
created
common/models/User.php 3 patches
Indentation   +20 added lines, -20 removed lines patch added patch discarded remove patch
@@ -560,29 +560,29 @@
 block discarded – undo
560 560
 
561 561
 
562 562
   public function cleanExportData($data) {
563
-   $order = array_flip(["date", "behavior", "category", "question1", "question2", "question3"]);
563
+    $order = array_flip(["date", "behavior", "category", "question1", "question2", "question3"]);
564 564
 
565
-   $ret = array_map(
566
-     function($row) use ($order) {
567
-       // change timestamp to local time (for the user)
568
-       $row['date'] = $this->time->convertUTCToLocal($row['date'], false);
565
+    $ret = array_map(
566
+      function($row) use ($order) {
567
+        // change timestamp to local time (for the user)
568
+        $row['date'] = $this->time->convertUTCToLocal($row['date'], false);
569 569
        
570
-       // clean up things we don't need
571
-       $row['category'] = $row['category']['name'];
572
-       $row['behavior'] = $row['behavior']['name'];
573
-       unset($row['id']);
574
-       unset($row['behavior_id']);
575
-       unset($row['category_id']);
576
-
577
-       // sort the array into a sensible order
578
-       uksort($row, function($a, $b) use ($order) {
570
+        // clean up things we don't need
571
+        $row['category'] = $row['category']['name'];
572
+        $row['behavior'] = $row['behavior']['name'];
573
+        unset($row['id']);
574
+        unset($row['behavior_id']);
575
+        unset($row['category_id']);
576
+
577
+        // sort the array into a sensible order
578
+        uksort($row, function($a, $b) use ($order) {
579 579
         return $order[$a] <=> $order[$b];
580
-       });
581
-       return $row;
582
-     }, 
583
-     $data
584
-   );
585
-   return $ret;
580
+        });
581
+        return $row;
582
+      }, 
583
+      $data
584
+    );
585
+    return $ret;
586 586
   }
587 587
 
588 588
   /*
Please login to merge, or discard this patch.
Spacing   +26 added lines, -26 removed lines patch added patch discarded remove patch
@@ -143,7 +143,7 @@  discard block
 block discarded – undo
143 143
    */
144 144
   public function findByPasswordResetToken($token)
145 145
   {
146
-    if(!$this->isTokenCurrent($token)) {
146
+    if (!$this->isTokenCurrent($token)) {
147 147
       return null;
148 148
     }
149 149
 
@@ -161,15 +161,15 @@  discard block
 block discarded – undo
161 161
    */
162 162
   public function findByVerifyEmailToken($token)
163 163
   {
164
-    if($this->isTokenConfirmed($token)) return null;
164
+    if ($this->isTokenConfirmed($token)) return null;
165 165
 
166 166
     $user = $this->findOne([
167 167
       'verify_email_token' => [$token, $token . self::CONFIRMED_STRING],
168 168
       'status' => self::STATUS_ACTIVE,
169 169
     ]);
170 170
 
171
-    if($user) {
172
-      if(!$this->isTokenConfirmed($token) &&
171
+    if ($user) {
172
+      if (!$this->isTokenConfirmed($token) &&
173 173
          !$this->isTokenCurrent($token, 'user.verifyAccountTokenExpire')) {
174 174
         return null;
175 175
       }
@@ -191,8 +191,8 @@  discard block
 block discarded – undo
191 191
       'status' => self::STATUS_ACTIVE,
192 192
     ]);
193 193
 
194
-    if($user) {
195
-      if(!$user->isTokenCurrent($token, 'user.verifyAccountTokenExpire')) {
194
+    if ($user) {
195
+      if (!$user->isTokenCurrent($token, 'user.verifyAccountTokenExpire')) {
196 196
         return null;
197 197
       }
198 198
     }
@@ -210,7 +210,7 @@  discard block
 block discarded – undo
210 210
   public function isTokenCurrent($token, String $paramPath = 'user.passwordResetTokenExpire') {
211 211
     $expire = \Yii::$app->params[$paramPath];
212 212
     $parts = explode('_', $token);
213
-    $timestamp = (int) end($parts);
213
+    $timestamp = (int)end($parts);
214 214
     if ($timestamp + $expire < time()) {
215 215
       // token expired
216 216
       return false;
@@ -225,7 +225,7 @@  discard block
 block discarded – undo
225 225
    * @param string    $match the needle to search for
226 226
    */
227 227
   public function isTokenConfirmed($token = null, String $match = self::CONFIRMED_STRING) {
228
-    if(is_null($token)) $token = $this->verify_email_token;
228
+    if (is_null($token)) $token = $this->verify_email_token;
229 229
     return substr($token, -strlen($match)) === $match;
230 230
   }
231 231
 
@@ -250,7 +250,7 @@  discard block
 block discarded – undo
250 250
   }
251 251
 
252 252
   public function isVerified() {
253
-    if(is_null($this->verify_email_token)) {
253
+    if (is_null($this->verify_email_token)) {
254 254
       // for old users who verified their accounts before the addition of
255 255
       // '_confirmed' to the token
256 256
       return true;
@@ -369,7 +369,7 @@  discard block
 block discarded – undo
369 369
    * partner email address the user has set.
370 370
    */
371 371
   public function sendEmailReport($date) {
372
-    if(!$this->send_email) return false; // no partner emails set
372
+    if (!$this->send_email) return false; // no partner emails set
373 373
     list($start, $end) = $this->time->getUTCBookends($date);
374 374
 
375 375
     $user_behavior = Yii::$container->get(UserBehaviorInterface::class);
@@ -378,14 +378,14 @@  discard block
 block discarded – undo
378 378
     // we should only proceed with sending the email if the user
379 379
     // scored above their set email threshold (User::email_category)
380 380
     $this_checkin     = $checkins_last_month[$date]; // gets the check-in
381
-    if(!$this_checkin)  return false;           // sanity check
382
-    $highest_cat_data = end($this_checkin);     // gets the data for the highest category from the check-in
383
-    if(!$highest_cat_data) return false;        // another sanity check
381
+    if (!$this_checkin)  return false; // sanity check
382
+    $highest_cat_data = end($this_checkin); // gets the data for the highest category from the check-in
383
+    if (!$highest_cat_data) return false; // another sanity check
384 384
     $highest_cat_idx  = key($this_checkin); // gets the key of the highest category
385 385
 
386 386
     // if the highest category they reached today was less than
387 387
     // the category threshold they have set, don't send the email
388
-    if($highest_cat_idx < $this->email_category) return false;
388
+    if ($highest_cat_idx < $this->email_category) return false;
389 389
 
390 390
     $graph = Yii::$container
391 391
       ->get(\common\components\Graph::class)
@@ -395,8 +395,8 @@  discard block
 block discarded – undo
395 395
     $user_questions = $this->getUserQuestions($date);
396 396
 
397 397
     $messages = [];
398
-    foreach($this->getPartnerEmails() as $email) {
399
-      if($email) {
398
+    foreach ($this->getPartnerEmails() as $email) {
399
+      if ($email) {
400 400
         $messages[] = Yii::$app->mailer->compose('checkinReport', [
401 401
           'user'          => $this,
402 402
           'email'         => $email,
@@ -408,7 +408,7 @@  discard block
 block discarded – undo
408 408
           'behaviors_list'  => \common\models\Behavior::$behaviors,
409 409
         ])->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name])
410 410
         ->setReplyTo($this->email)
411
-        ->setSubject($this->email." has completed a Faster Scale check-in")
411
+        ->setSubject($this->email . " has completed a Faster Scale check-in")
412 412
         ->setTo($email);
413 413
       }
414 414
     }
@@ -482,7 +482,7 @@  discard block
 block discarded – undo
482 482
     return \Yii::$app->mailer->compose('signupNotification')
483 483
       ->setFrom([\Yii::$app->params['supportEmail'] => \Yii::$app->name])
484 484
       ->setTo(\Yii::$app->params['adminEmail'])
485
-      ->setSubject('A new user has signed up for '.\Yii::$app->name)
485
+      ->setSubject('A new user has signed up for ' . \Yii::$app->name)
486 486
       ->send();
487 487
   }
488 488
 
@@ -490,20 +490,20 @@  discard block
 block discarded – undo
490 490
     return \Yii::$app->mailer->compose('verifyEmail', ['user' => $this])
491 491
       ->setFrom([\Yii::$app->params['supportEmail'] => \Yii::$app->name])
492 492
       ->setTo($this->email)
493
-      ->setSubject('Please verify your '.\Yii::$app->name .' account')
493
+      ->setSubject('Please verify your ' . \Yii::$app->name . ' account')
494 494
       ->send();
495 495
   }
496 496
 
497 497
   public function sendDeleteNotificationEmail() {
498 498
     $messages = [];
499
-    foreach(array_merge([$this->email], $this->getPartnerEmails()) as $email) {
500
-      if($email) {
499
+    foreach (array_merge([$this->email], $this->getPartnerEmails()) as $email) {
500
+      if ($email) {
501 501
         $messages[] = Yii::$app->mailer->compose('deleteNotification', [
502 502
           'user' => $this,
503 503
           'email' => $email
504 504
         ])->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name])
505 505
         ->setReplyTo($this->email)
506
-        ->setSubject($this->email." has deleted their The Faster Scale App account")
506
+        ->setSubject($this->email . " has deleted their The Faster Scale App account")
507 507
         ->setTo($email);
508 508
       }
509 509
     }
@@ -512,16 +512,16 @@  discard block
 block discarded – undo
512 512
   }
513 513
 
514 514
   public function getUserQuestions($local_date = null) {
515
-    if(is_null($local_date)) $local_date = $this->time->getLocalDate();
515
+    if (is_null($local_date)) $local_date = $this->time->getLocalDate();
516 516
     $questions = $this->getQuestionData($local_date);
517 517
     return $this->parseQuestionData($questions);
518 518
   }
519 519
 
520 520
   public function parseQuestionData($questions) {
521
-    if(!$questions) return [];
521
+    if (!$questions) return [];
522 522
 
523 523
     $question_answers = [];
524
-    foreach($questions as $question) {
524
+    foreach ($questions as $question) {
525 525
       $behavior = $question['behavior'];
526 526
 
527 527
       $question_answers[$behavior['id']]['question'] = [
@@ -604,7 +604,7 @@  discard block
 block discarded – undo
604 604
     return rtrim(
605 605
       strtr(
606 606
         base64_encode(
607
-          hash('sha256', $this->id."::".$this->created_at, true)
607
+          hash('sha256', $this->id . "::" . $this->created_at, true)
608 608
         ),
609 609
       '+/', '-_'),
610 610
     '=');
Please login to merge, or discard this patch.
Braces   +27 added lines, -8 removed lines patch added patch discarded remove patch
@@ -161,7 +161,9 @@  discard block
 block discarded – undo
161 161
    */
162 162
   public function findByVerifyEmailToken($token)
163 163
   {
164
-    if($this->isTokenConfirmed($token)) return null;
164
+    if($this->isTokenConfirmed($token)) {
165
+      return null;
166
+    }
165 167
 
166 168
     $user = $this->findOne([
167 169
       'verify_email_token' => [$token, $token . self::CONFIRMED_STRING],
@@ -225,7 +227,9 @@  discard block
 block discarded – undo
225 227
    * @param string    $match the needle to search for
226 228
    */
227 229
   public function isTokenConfirmed($token = null, String $match = self::CONFIRMED_STRING) {
228
-    if(is_null($token)) $token = $this->verify_email_token;
230
+    if(is_null($token)) {
231
+      $token = $this->verify_email_token;
232
+    }
229 233
     return substr($token, -strlen($match)) === $match;
230 234
   }
231 235
 
@@ -369,7 +373,10 @@  discard block
 block discarded – undo
369 373
    * partner email address the user has set.
370 374
    */
371 375
   public function sendEmailReport($date) {
372
-    if(!$this->send_email) return false; // no partner emails set
376
+    if(!$this->send_email) {
377
+      return false;
378
+    }
379
+    // no partner emails set
373 380
     list($start, $end) = $this->time->getUTCBookends($date);
374 381
 
375 382
     $user_behavior = Yii::$container->get(UserBehaviorInterface::class);
@@ -378,14 +385,22 @@  discard block
 block discarded – undo
378 385
     // we should only proceed with sending the email if the user
379 386
     // scored above their set email threshold (User::email_category)
380 387
     $this_checkin     = $checkins_last_month[$date]; // gets the check-in
381
-    if(!$this_checkin)  return false;           // sanity check
388
+    if(!$this_checkin) {
389
+      return false;
390
+    }
391
+    // sanity check
382 392
     $highest_cat_data = end($this_checkin);     // gets the data for the highest category from the check-in
383
-    if(!$highest_cat_data) return false;        // another sanity check
393
+    if(!$highest_cat_data) {
394
+      return false;
395
+    }
396
+    // another sanity check
384 397
     $highest_cat_idx  = key($this_checkin); // gets the key of the highest category
385 398
 
386 399
     // if the highest category they reached today was less than
387 400
     // the category threshold they have set, don't send the email
388
-    if($highest_cat_idx < $this->email_category) return false;
401
+    if($highest_cat_idx < $this->email_category) {
402
+      return false;
403
+    }
389 404
 
390 405
     $graph = Yii::$container
391 406
       ->get(\common\components\Graph::class)
@@ -512,13 +527,17 @@  discard block
 block discarded – undo
512 527
   }
513 528
 
514 529
   public function getUserQuestions($local_date = null) {
515
-    if(is_null($local_date)) $local_date = $this->time->getLocalDate();
530
+    if(is_null($local_date)) {
531
+      $local_date = $this->time->getLocalDate();
532
+    }
516 533
     $questions = $this->getQuestionData($local_date);
517 534
     return $this->parseQuestionData($questions);
518 535
   }
519 536
 
520 537
   public function parseQuestionData($questions) {
521
-    if(!$questions) return [];
538
+    if(!$questions) {
539
+      return [];
540
+    }
522 541
 
523 542
     $question_answers = [];
524 543
     foreach($questions as $question) {
Please login to merge, or discard this patch.
common/models/UserBehavior.php 2 patches
Spacing   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -86,7 +86,7 @@  discard block
 block discarded – undo
86 86
       ->groupBy('date, user_id')
87 87
       ->having('user_id = :user_id');
88 88
     $temp_dates = $query->all();
89
-    foreach($temp_dates as $temp_date) {
89
+    foreach ($temp_dates as $temp_date) {
90 90
       $past_checkin_dates[] = $this->time->convertUTCToLocal($temp_date['date']);
91 91
     }
92 92
 
@@ -115,12 +115,12 @@  discard block
 block discarded – undo
115 115
 
116 116
   public function getCheckinBreakdown(int $period = 30) {
117 117
     $datetimes = $this->time->getDateTimesInPeriod($period);
118
-    $key = "checkins_".Yii::$app->user->id."_{$period}_".$this->time->getLocalDate();
118
+    $key = "checkins_" . Yii::$app->user->id . "_{$period}_" . $this->time->getLocalDate();
119 119
     $checkins = Yii::$app->cache->get($key);
120 120
 
121
-    if($checkins === false) {
121
+    if ($checkins === false) {
122 122
       $checkins = [];
123
-      foreach($datetimes as $datetime) {
123
+      foreach ($datetimes as $datetime) {
124 124
         $behaviors = self::decorate($this->getBehaviorsWithCounts($datetime));
125 125
         $checkins[$datetime->format('Y-m-d')] = $this->getBehaviorsByCategory($behaviors);
126 126
       }
@@ -146,7 +146,7 @@  discard block
 block discarded – undo
146 146
   public function getBehaviorsByCategory(array $decorated_behaviors) {
147 147
     $arr = array_reduce($decorated_behaviors, function($acc, $row) {
148 148
       $cat_id = $row['category_id'];
149
-      if(array_key_exists($cat_id, $acc)) {
149
+      if (array_key_exists($cat_id, $acc)) {
150 150
         $acc[$cat_id]['count'] += $row['count'];
151 151
       } else {
152 152
         $acc[$cat_id] = [
@@ -178,11 +178,11 @@  discard block
 block discarded – undo
178 178
       ->having('user_id = :user_id')
179 179
       ->orderBy('count DESC');
180 180
 
181
-    if($limit instanceof \DateTime) {
181
+    if ($limit instanceof \DateTime) {
182 182
       list($start, $end) = $this->time->getUTCBookends($limit->format('Y-m-d'));
183 183
       $query->params += [':start_date' => $start, ':end_date' => $end];
184 184
       $query->where('user_id=:user_id AND date > :start_date AND date <= :end_date');
185
-    } else if(is_int($limit)) {
185
+    } else if (is_int($limit)) {
186 186
       $query->limit($limit);
187 187
     }
188 188
     return $query->all();
@@ -197,7 +197,7 @@  discard block
 block discarded – undo
197 197
    * @return array
198 198
    */
199 199
   public function getByDate(int $user_id, $local_date = null) {
200
-    if(is_null($local_date)) $local_date = $this->time->getLocalDate();
200
+    if (is_null($local_date)) $local_date = $this->time->getLocalDate();
201 201
 
202 202
     $behaviors = $this->getBehaviorData($user_id, $local_date);
203 203
     $behaviors = self::decorate($behaviors);
@@ -210,12 +210,12 @@  discard block
 block discarded – undo
210 210
    * @return an array of decorated UserBehaviors, each with an added Category and Behavior
211 211
    */
212 212
   public static function decorate(array $uo) {
213
-    foreach($uo as &$o) {
213
+    foreach ($uo as &$o) {
214 214
       $behavior = \common\models\Behavior::getBehavior('id', $o['behavior_id']);
215 215
       $category = \common\models\Category::getCategory('id', $o['category_id']);
216 216
       // if this behavior does not have a valid behavior or category, something
217 217
       // is weird and we don't want to do this halfway.
218
-      if($behavior && $category) {
218
+      if ($behavior && $category) {
219 219
         $o['behavior'] = $behavior;
220 220
         $o['category'] = $category;
221 221
       }
@@ -242,10 +242,10 @@  discard block
 block discarded – undo
242 242
 
243 243
   // this should probably be a private method...but unit testing is hard
244 244
   public function parseBehaviorData($behaviors) {
245
-    if(!$behaviors) return [];
245
+    if (!$behaviors) return [];
246 246
 
247 247
     $bhvrs_by_cat = [];
248
-    foreach($behaviors as $behavior) {
248
+    foreach ($behaviors as $behavior) {
249 249
       $indx = $behavior['category_id'];
250 250
 
251 251
       $bhvrs_by_cat[$indx][$behavior['behavior']['name']] = [
Please login to merge, or discard this patch.
Braces   +6 added lines, -2 removed lines patch added patch discarded remove patch
@@ -197,7 +197,9 @@  discard block
 block discarded – undo
197 197
    * @return array
198 198
    */
199 199
   public function getByDate(int $user_id, $local_date = null) {
200
-    if(is_null($local_date)) $local_date = $this->time->getLocalDate();
200
+    if(is_null($local_date)) {
201
+      $local_date = $this->time->getLocalDate();
202
+    }
201 203
 
202 204
     $behaviors = $this->getBehaviorData($user_id, $local_date);
203 205
     $behaviors = self::decorate($behaviors);
@@ -242,7 +244,9 @@  discard block
 block discarded – undo
242 244
 
243 245
   // this should probably be a private method...but unit testing is hard
244 246
   public function parseBehaviorData($behaviors) {
245
-    if(!$behaviors) return [];
247
+    if(!$behaviors) {
248
+      return [];
249
+    }
246 250
 
247 251
     $bhvrs_by_cat = [];
248 252
     foreach($behaviors as $behavior) {
Please login to merge, or discard this patch.
migrations/m190829_141748_alter_user_behavior_link_add_category_id.php 1 patch
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -22,20 +22,20 @@
 block discarded – undo
22 22
 
23 23
       // then populate the columns
24 24
       $query = (new Query())->from('user_behavior_link');
25
-      foreach($query->batch() as $rows) {
26
-        foreach($rows as $row) {
25
+      foreach ($query->batch() as $rows) {
26
+        foreach ($rows as $row) {
27 27
           $bhvr = Behavior::getBehavior('id', $row['behavior_id']);
28
-          if($bhvr) {
28
+          if ($bhvr) {
29 29
             $this->update('{{%user_behavior_link}}', ['category_id' => $bhvr['category_id']], ['id' => $row['id']]);
30 30
           }
31 31
         }
32 32
       }
33 33
 
34 34
       $query = (new Query())->from('question');
35
-      foreach($query->batch() as $rows) {
36
-        foreach($rows as $row) {
35
+      foreach ($query->batch() as $rows) {
36
+        foreach ($rows as $row) {
37 37
           $bhvr = Behavior::getBehavior('id', $row['behavior_id']);
38
-          if($bhvr) {
38
+          if ($bhvr) {
39 39
             $this->update('{{%question}}', ['category_id' => $bhvr['category_id']], ['id' => $row['id']]);
40 40
           }
41 41
         }
Please login to merge, or discard this patch.