Blame view

console/controllers/CreateController.php 1.17 KB
a0e8d4e0   Alexey Boroda   first commit
1
  <?php
a0e8d4e0   Alexey Boroda   first commit
2
  
bc54438e   Alexey Boroda   -Some fixes
3
  namespace console\controllers;
a0e8d4e0   Alexey Boroda   first commit
4
  
bc54438e   Alexey Boroda   -Some fixes
5
6
7
8
9
10
11
12
13
14
15
16
  use artbox\core\models\User;
  use yii\console\Controller;
  use yii\helpers\Console;
  
  /**
   * Class CreateController
   *
   * @package console\controllers
   */
  class CreateController extends Controller
  {
      public function actionUser($username = null, $email = null, $password = null)
a0e8d4e0   Alexey Boroda   first commit
17
      {
bc54438e   Alexey Boroda   -Some fixes
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
          $username = $username ? : 'developer';
          $user = User::find()
                      ->where(['username' => $username])
                      ->one();
          if (empty($user)) {
              $user = new User();
              $user->username = $username;
          }
          $user->email = $email ? : 'dev@example.com';
          $user->setPassword($password ? : 'kristal2021');
          $user->generateAuthKey();
          
          if ($user->save()) {
              if ($user->isNewRecord) {
                  $this->stdout('User created' . "\n", Console::FG_GREEN);
a0e8d4e0   Alexey Boroda   first commit
33
              } else {
bc54438e   Alexey Boroda   -Some fixes
34
                  $this->stdout('User updated' . "\n", Console::FG_GREEN);
a0e8d4e0   Alexey Boroda   first commit
35
              }
bc54438e   Alexey Boroda   -Some fixes
36
37
38
39
          } else {
              $this->stdout('Error!' . "\n", Console::FG_RED);
              
              return self::EXIT_CODE_ERROR;
a0e8d4e0   Alexey Boroda   first commit
40
          }
bc54438e   Alexey Boroda   -Some fixes
41
42
43
44
          
          return self::EXIT_CODE_NORMAL;
      }
  }