Passed
Pull Request — master (#191)
by Corey
03:34
created

FsaController   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 13
c 1
b 0
f 0
dl 0
loc 35
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A actionRemoveOldUnconfirmedAccounts() 0 14 1
A getTimeThreshold() 0 4 1
1
<?php
2
namespace console\controllers;
3
4
use \yii\helpers\BaseConsole as Console;
5
use \common\models\User;
6
7
/**
8
 * Faster Scale App CLI Helpers
9
 */
10
class FsaController extends \yii\console\Controller
11
{
12
  public $defaultAction = null;
13
14
  public $ageThreshold = 7; // in days
15
16
  /**
17
   * Deletes unconfirmed accounts that are older than the lifetime of the
18
   * verification token
19
   */
20
  public function actionRemoveOldUnconfirmedAccounts(): void
21
  {
22
    $this->stdout("Removing accounts older than {$this->ageThreshold} days...\n", Console::FG_YELLOW);
23
    $count = User::deleteAll([
24
      'and',
25
        ['or',
26
          ['not', ['verify_email_token' => null]],
27
          // we have to escape the '_' at the start of User::CONFIRMED_STRING
28
          ['not like', 'verify_email_token', '%\\'.User::CONFIRMED_STRING, false]
29
        ],
30
        ['<', 'created_at', $this->getTimeThreshold()],
31
    ]);
32
33
    $this->stdout("Removed $count accounts", Console::FG_GREEN);
34
  }
35
36
  /**
37
   * Returns the cut-off time threshold for unconfirmed users we should delete
38
   *
39
   * @return integer
40
   */
41
  public function getTimeThreshold(): int
42
  {
43
        $expire = \Yii::$app->params['user.verifyAccountTokenExpire'];
44
        return time() - $expire;
45
  }
46
47
}
48