ContactForm::attributeLabels()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
namespace App\Http\Form;
3
4
use Yii;
5
use yii\base\Model;
6
7
/**
8
 * ContactForm is the model behind the contact form.
9
 */
10
class ContactForm extends Model
11
{
12
    public $name;
13
    public $email;
14
    public $subject;
15
    public $body;
16
    public $verifyCode;
17
18
    /**
19
     * {@inheritdoc}
20
     */
21
    public function rules()
22
    {
23
        return [
24
            // name, email, subject and body are required
25
            [['name', 'email', 'subject', 'body'], 'required'],
26
            // email has to be a valid email address
27
            ['email', 'email'],
28
            // verifyCode needs to be entered correctly
29
            ['verifyCode', 'captcha'],
30
        ];
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function attributeLabels()
37
    {
38
        return [
39
            'name' => Yii::t('app', 'Name'),
40
            'email' => Yii::t('app', 'Email'),
41
            'subject' => Yii::t('app', 'Subject'),
42
            'body' => Yii::t('app', 'Content'),
43
            'verifyCode' => Yii::t('app', 'Verification Code'),
44
        ];
45
    }
46
47
    /**
48
     * Sends an email to the specified email address using the information collected by this model.
49
     *
50
     * @param string $email the target email address
51
     * @return bool whether the email was sent
52
     */
53
    public function sendEmail($email)
54
    {
55
        return Yii::$app->getMailer()
56
            ->compose()
57
            ->setTo($email)
58
            ->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name])
59
            ->setReplyTo([$this->email => $this->name])
60
            ->setSubject($this->subject)
61
            ->setTextBody($this->body)
62
            ->send();
63
    }
64
}
65