ContactForm   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
eloc 28
c 1
b 0
f 1
dl 0
loc 68
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 10 1
A scenarios() 0 5 1
A attributeLabels() 0 8 1
A sendEmail() 0 9 1
1
<?php
2
3
namespace modules\main\models\frontend;
4
5
use Yii;
6
use yii\helpers\Url;
7
use yii\base\Model;
8
use modules\main\Module;
9
10
/**
11
 * Class ContactForm
12
 * @package modules\main\models\frontend
13
 *
14
 * @property string $name Name
15
 * @property string $email Email
16
 * @property string $subject Subject
17
 * @property string $body Body
18
 * @property string $verifyCode Verify Code
19
 */
20
class ContactForm extends Model
21
{
22
    public $name;
23
    public $email;
24
    public $subject;
25
    public $body;
26
    public $verifyCode;
27
28
    const SCENARIO_GUEST = 'guest';
29
30
    /**
31
     * @inheritdoc
32
     * @return array
33
     */
34
    public function rules()
35
    {
36
        return [
37
            // name, email, subject and body are required
38
            [['name', 'email', 'subject', 'body'], 'required'],
39
            // email has to be a valid email address
40
            ['email', 'email'],
41
            // verifyCode needs to be entered correctly
42
            [['verifyCode'], 'required', 'on' => self::SCENARIO_GUEST],
43
            ['verifyCode', 'captcha', 'captchaAction' => Url::to('/main/default/captcha'), 'on' => self::SCENARIO_GUEST]
44
        ];
45
    }
46
47
    /**
48
     * @inheritdoc
49
     * @return array
50
     */
51
    public function scenarios()
52
    {
53
        $scenarios = parent::scenarios();
54
        $scenarios[self::SCENARIO_GUEST] = ['name', 'email', 'subject', 'body', 'verifyCode'];
55
        return $scenarios;
56
    }
57
58
    /**
59
     * @inheritdoc
60
     * @return array
61
     */
62
    public function attributeLabels()
63
    {
64
        return [
65
            'name' => Module::translate('module', 'Name'),
66
            'email' => Module::translate('module', 'E-mail'),
67
            'subject' => Module::translate('module', 'Subject'),
68
            'body' => Module::translate('module', 'Body'),
69
            'verifyCode' => Module::translate('module', 'Verification Code')
70
        ];
71
    }
72
73
    /**
74
     * Sends an email to the specified email address using the information collected by this model.
75
     *
76
     * @param string $email the target email address
77
     * @return bool whether the email was sent
78
     */
79
    public function sendEmail($email)
80
    {
81
        return Yii::$app->mailer->compose()
82
            ->setTo($email)
83
            ->setFrom([Yii::$app->params['senderEmail'] => Yii::$app->params['senderName']])
84
            ->setReplyTo([$this->email => $this->name])
85
            ->setSubject($this->subject)
86
            ->setTextBody($this->body)
87
            ->send();
88
    }
89
}
90