Completed
Pull Request — master (#163)
by Corey
03:37
created

EditProfileForm   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
dl 0
loc 102
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B saveProfile() 0 27 6
A __construct() 0 3 1
A attributeLabels() 0 7 1
A loadUser() 0 9 1
A rules() 0 17 1
1
<?php
2
namespace site\models;
3
4
use yii\base\Model;
5
use Yii;
6
use \DateTimeZone;
7
8
/**
9
 * edit profile form
10
 */
11
class EditProfileForm extends Model
12
{
13
  public $email;
14
  public $timezone;
15
  public $expose_graph;
16
  public $send_email;
17
  public $partner_email1;
18
  public $partner_email2;
19
  public $partner_email3;
20
21
  private $user;
22
23
  /**
24
   * Creates a form model
25
   *
26
   * @param  object                          $user
27
   * @param  array                           $config name-value pairs that will be used to initialize the object properties
28
   * @throws \yii\base\InvalidParamException if token is empty or not valid
29
   */
30
  public function __construct(\common\models\User $user, $config = []) {
31
    $this->user = $user;
32
    parent::__construct($config);
33
  }
34
35
  /**
36
   * @inheritdoc
37
   */
38
  public function rules()
39
  {
40
    return [
41
      ['email', 'filter', 'filter' => 'trim'],
42
      ['email', 'filter', 'filter' => 'strtolower'],
43
      ['email', 'email'],
44
      ['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.', 'filter' => "id <> ".Yii::$app->user->id],
45
46
      ['timezone', 'string', 'min' => 2, 'max' => 255],
47
      ['timezone', 'in', 'range'=>DateTimeZone::listIdentifiers()],
48
49
      ['send_email', 'boolean'],
50
      [['partner_email1', 'partner_email2', 'partner_email3'], 'email'],
51
      [['partner_email1'], 'required',
52
        'when' => function($model) { return $model->send_email; },
53
        'message' => "If you've elected to send email reports, at least one partner email must be set.",
54
        "whenClient" => "function(attribute, value) {
55
          return $('#editprofileform-send_email').is(':checked');
56
        }"]
57
    ];
58
  }
59
60
  public function attributeLabels() {
61
    return [
62
      'partner_email1' => "Partner Email #1",
63
      'partner_email2' => "Partner Email #2",
64
      'partner_email3' => "Partner Email #3",
65
      'send_email'     => 'Send an email when I complete a check-in',
66
      'expose_graph'   => 'Share my scores graph via a link'
67
    ];
68
  }
69
70
  /**
71
   * saves user's profile info.
72
   *
73
   * @return User|null the saved model or null if saving fails
0 ignored issues
show
Bug introduced by
The type site\models\User was not found. Did you mean User? If so, make sure to prefix the type with \.
Loading history...
74
   */
75
  public function saveProfile()
76
  {
77
    if ($this->validate()) {
78
      $user  = $this->user;
79
80
      if($this->email)
81
        $user->email = $this->email;
82
      if($this->timezone)
83
        $user->timezone = $this->timezone;
84
      if($this->expose_graph) {
85
        $user->expose_graph = true;
86
      } else {
87
        $user->expose_graph = false;
88
      }
89
      if($this->send_email) {
90
        $user->partner_email1  = $this->partner_email1;
91
        $user->partner_email2  = $this->partner_email2;
92
        $user->partner_email3  = $this->partner_email3;
93
      } else {
94
        $user->partner_email1  = null;
95
        $user->partner_email2  = null;
96
        $user->partner_email3  = null;
97
      }
98
      $user->save();
99
      return $user;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $user returns the type common\models\User which is incompatible with the documented return type null|site\models\User.
Loading history...
100
    }
101
    return null;
102
  }
103
104
  public function loadUser() {
105
    $user                  = $this->user;
106
    $this->email           = $user->email;
107
    $this->timezone        = $user->timezone;
108
    $this->partner_email1  = $user->partner_email1;
109
    $this->partner_email2  = $user->partner_email2;
110
    $this->partner_email3  = $user->partner_email3;
111
    $this->expose_graph    = $user->expose_graph;
112
    $this->send_email      = $user->send_email;
113
  }
114
}
115