ContactForm::rules()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
/**
3
 * @link http://www.writesdown.com/
4
 * @copyright Copyright (c) 2015 WritesDown
5
 * @license http://www.writesdown.com/license/
6
 */
7
8
namespace frontend\models;
9
10
use Yii;
11
use yii\base\Model;
12
13
/**
14
 * Class ContactForm
15
 *
16
 * @author Agiel K. Saputra <[email protected]>
17
 * @since 0.1.0
18
 */
19
class ContactForm extends Model
20
{
21
    /**
22
     * @var string
23
     */
24
    public $name;
25
    /**
26
     * @var string
27
     */
28
    public $email;
29
    /**
30
     * @var string
31
     */
32
    public $subject;
33
    /**
34
     * @var string
35
     */
36
    public $body;
37
    /**
38
     * @var
39
     */
40
    public $verifyCode;
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function rules()
46
    {
47
        return [
48
            // name, email, subject and body are required
49
            [['name', 'email', 'subject', 'body'], 'required'],
50
            // email has to be a valid email address
51
            ['email', 'email'],
52
            // verifyCode needs to be entered correctly
53
            ['verifyCode', 'captcha'],
54
        ];
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60
    public function attributeLabels()
61
    {
62
        return [
63
            'verifyCode' => 'Verification Code',
64
        ];
65
    }
66
67
    /**
68
     * Sends an email to the specified email address using the information collected by this model.
69
     *
70
     * @param  string $email the target email address
71
     * @return boolean whether the email was sent
72
     */
73
    public function sendEmail($email)
74
    {
75
        return Yii::$app->mailer->compose()
76
            ->setTo($email)
77
            ->setFrom([$this->email => $this->name])
78
            ->setSubject($this->subject)
79
            ->setTextBody($this->body)
80
            ->send();
81
    }
82
}
83