ContactForm   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 22
c 1
b 0
f 0
dl 0
loc 52
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A attributeLabels() 0 4 1
A rules() 0 13 1
A sendEmail() 0 8 1
1
<?php
2
3
namespace site\models;
4
5
use Yii;
6
use yii\base\Model;
7
8
/**
9
 * ContactForm is the model behind the contact form.
10
 */
11
class ContactForm extends Model
12
{
13
  public $name;
14
  public $email;
15
  public $subject;
16
  public $body;
17
  public $verifyCode;
18
19
  /**
20
   * @inheritdoc
21
   */
22
  public function rules()
23
  {
24
    return [
25
      // name, email, subject and body are required
26
      [['name', 'email', 'subject', 'body'], 'required'],
27
      // email has to be a valid email address
28
      ['email', 'filter', 'filter' => 'trim'],
29
      ['email', 'filter', 'filter' => 'strtolower'],
30
      ['email', 'email'],
31
      // verifyCode needs to be entered correctly
32
      ['verifyCode', 'captcha', 'when' => function() {
33
        return Yii::$app->user->isGuest;
34
      }, 'skipOnEmpty' => !!YII_ENV_TEST],
35
    ];
36
  }
37
38
  /**
39
   * @inheritdoc
40
   * @codeCoverageIgnore
41
   */
42
  public function attributeLabels()
43
  {
44
    return [
45
      'verifyCode' => 'Verification Code',
46
    ];
47
  }
48
49
  /**
50
   * Sends an email to the specified email address using the information collected by this model.
51
   *
52
   * @param  string  $email the target email address
53
   * @return boolean whether the email was sent
54
   */
55
  public function sendEmail($email)
56
  {
57
    return Yii::$app->mailer->compose()
58
      ->setTo($email)
59
      ->setFrom([$this->email => $this->name])
60
      ->setSubject("[FSA Contact] ".$this->subject)
61
      ->setTextBody($this->body)
62
      ->send();
63
  }
64
}
65