Completed
Pull Request — master (#164)
by Corey
03:20
created

ChangeEmailForm::attributeLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace site\models;
3
4
use common\models\User;
5
use yii\base\Model;
6
use Yii;
7
8
/**
9
 * change email form
10
 */
11
class ChangeEmailForm extends Model {
12
  public $desired_email;
13
  private $user;
14
15
  /**
16
   * Creates a form model
17
   *
18
   * @param  object                          $user
19
   * @param  array                           $config name-value pairs that will be used to initialize the object properties
20
   */
21
  public function __construct(\common\interfaces\UserInterface $user, $config = []) {
22
    $this->user = $user;
23
    parent::__construct($config);
24
  }
25
26
  /**
27
   * @inheritdoc
28
   */
29
  public function rules() {
30
    return [
31
      ['desired_email', 'email'],
32
      ['desired_email', 'required'],
33
    ];
34
  }
35
36
  public function attributeLabels() {
37
    return [
38
      'desired_email' => 'New Email',
39
      ];
40
  }
41
42
  /**
43
   * Sets verification tokens and sends verification emails
44
   *
45
   * @return Boolean whether or not the operation was successful
46
   */
47
  public function changeEmail() {
48
    $user = $this->user->findByEmail($this->desired_email);
0 ignored issues
show
Bug introduced by
The method findByEmail() does not exist on common\interfaces\UserInterface. It seems like you code against a sub-type of common\interfaces\UserInterface such as common\models\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
    /** @scrutinizer ignore-call */ 
49
    $user = $this->user->findByEmail($this->desired_email);
Loading history...
49
    if(!$user) {
50
      //var_dump( $user);
51
      //exit();
52
      $this->user->desired_email = $this->desired_email;
0 ignored issues
show
Bug introduced by
Accessing desired_email on the interface common\interfaces\UserInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
53
      $this->user->generateChangeEmailToken();
0 ignored issues
show
Bug introduced by
The method generateChangeEmailToken() does not exist on common\interfaces\UserInterface. It seems like you code against a sub-type of common\interfaces\UserInterface such as common\models\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
      $this->user->/** @scrutinizer ignore-call */ 
54
                   generateChangeEmailToken();
Loading history...
54
      $this->user->save();
55
      //var_dump( $this->user);
56
      //exit();
57
58
      $this->sendChangeEmailEmail($this->user->email, $this->desired_email, $this->user->change_email_token);
0 ignored issues
show
Bug introduced by
Accessing change_email_token on the interface common\interfaces\UserInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
Bug introduced by
Accessing email on the interface common\interfaces\UserInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
59
    }
60
    return true;
61
    
62
    /*
63
     * new email address
64
     * verify_email_1
65
     * verify_email_2
66
     */
67
68
    // generate and set verification token
69
    // send verification email
70
    // LOG THEM OUT when change is completed
71
    //
72
    // create token
73
    // on first verify, add _one_confirmed
74
    // on second verify, remove field
75
    //
76
    // But then we have a race condition -- or not necessarily a race,
77
    // but a time-sensitive issue. If both requests were made at exactly the same time...
78
  }
79
80
  public function sendChangeEmailEmail($current_email, $desired_email, $token) {
81
    Yii::$app->mailer->compose('changeEmail', [
82
      'current_email' => $current_email,
83
      'desired_email' => $desired_email,
84
      'token'         => $token,
85
    ])->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name])
86
    ->setSubject('Faster Scale App -- your requested email change')
87
    ->setTo($desired_email)
88
    ->send();
89
  }
90
}
91