ChangeEmailForm   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 27
c 1
b 0
f 0
dl 0
loc 61
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A attributeLabels() 0 3 1
A __construct() 0 3 1
A rules() 0 6 1
A changeEmail() 0 12 2
A sendChangeEmailEmail() 0 9 1
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', 'required'],
32
      ['desired_email', 'filter', 'filter' => 'trim'],
33
      ['desired_email', 'filter', 'filter' => 'strtolower'],
34
      ['desired_email', 'email'],
35
    ];
36
  }
37
38
  public function attributeLabels() {
39
    return [
40
      'desired_email' => 'New Email',
41
      ];
42
  }
43
44
  /**
45
   * Sets verification tokens and sends verification emails
46
   *
47
   * @return Boolean whether or not the operation was successful
48
   */
49
  public function changeEmail() {
50
    // check that the desired_email is not already the email of another user
51
    $user = $this->user->findByEmail($this->desired_email);
52
    // if it is, we do nothing and return true, don't leak the data
53
    if(!$user) {
54
      $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...
55
      $this->user->generateChangeEmailToken();
56
      $this->user->save();
57
58
      $this->sendChangeEmailEmail();
59
    }
60
    return true;
61
  }
62
63
  private function sendChangeEmailEmail() {
64
    Yii::$app->mailer->compose('changeEmail', [
65
      '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...
66
      'desired_email' => $this->desired_email,
67
      '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...
68
    ])->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name])
69
    ->setSubject(Yii::$app->name . ' -- your requested email change')
70
    ->setTo($this->desired_email)
71
    ->send();
72
  }
73
}
74