ContactForm::attributeLabels()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * HiSite Yii2 base project.
4
 *
5
 * @link      https://github.com/hiqdev/hisite
6
 * @package   hisite
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hisite\models;
12
13
use Yii;
14
use yii\base\Model;
15
16
/**
17
 * ContactForm is the model behind the contact form.
18
 */
19
class ContactForm extends Model
20
{
21
    public $name;
22
    public $email;
23
    public $subject;
24
    public $body;
25
    public $verifyCode;
26
27
    /**
28
     * @return array the validation rules
29
     */
30
    public function rules()
31
    {
32
        return [
33
            // name, email, subject and body are required
34
            [['name', 'email', 'subject', 'body'], 'required'],
35
            // email has to be a valid email address
36
            ['email', 'email'],
37
            // verifyCode needs to be entered correctly
38
            ['verifyCode', 'captcha'],
39
        ];
40
    }
41
42
    /**
43
     * @return array customized attribute labels
44
     */
45
    public function attributeLabels()
46
    {
47
        return [
48
            'verifyCode' => Yii::t('hisite', 'Verification Code'),
49
        ];
50
    }
51
52
    /**
53
     * Sends an email to the specified email address using the information collected by this model.
54
     * @param string $email the target email address
55
     * @return boolean whether the model passes validation
56
     */
57
    public function contact($email)
58
    {
59
        if ($this->validate()) {
60
            Yii::$app->mailer->compose()
61
                ->setTo($email)
62
                ->setFrom([$this->email => $this->name])
63
                ->setSubject($this->subject)
64
                ->setTextBody($this->body)
65
                ->send();
66
67
            return true;
68
        }
69
70
        return false;
71
    }
72
}
73