ContactForm   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 49
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 11 1
A attributeLabels() 0 6 1
A sendEmail() 0 9 1
1
<?php
2
3
namespace frontend\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', 'email'],
29
            // verifyCode needs to be entered correctly
30
            ['verifyCode', 'captcha'],
31
        ];
32
    }
33
34
    /**
35
     * @inheritdoc
36
     */
37
    public function attributeLabels()
38
    {
39
        return [
40
            'verifyCode' => 'Verification Code',
41
        ];
42
    }
43
44
    /**
45
     * Sends an email to the specified email address using the information collected by this model.
46
     *
47
     * @param  string  $email the target email address
48
     * @return boolean whether the email was sent
49
     */
50
    public function sendEmail($email)
51
    {
52
        return Yii::$app->mailer->compose()
53
            ->setTo($email)
54
            ->setFrom([$this->email => $this->name])
55
            ->setSubject($this->subject)
56
            ->setTextBody($this->body)
57
            ->send();
58
    }
59
}
60