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

ChangeEmailForm::rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 3
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
    // check that the desired_email is not already the email of another user
49
    $user = $this->user->findByEmail($this->desired_email);
50
    // if it is, we do nothing and return true, don't leak the data
51
    if(!$user) {
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();
54
      $this->user->save();
55
56
      $this->sendChangeEmailEmail();
57
    }
58
    return true;
59
  }
60
61
  private function sendChangeEmailEmail() {
62
    Yii::$app->mailer->compose('changeEmail', [
63
      'current_email' => $this->user->email,
0 ignored issues
show
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...
64
      'desired_email' => $this->desired_email,
65
      'token'         => $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...
66
    ])->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name])
67
    ->setSubject(Yii::$app->name . ' -- your requested email change')
68
    ->setTo($this->desired_email)
69
    ->send();
70
  }
71
}
72