Passed
Pull Request — master (#2)
by
unknown
15:51
created

Feedback::rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 65
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 36
nc 1
nop 0
dl 0
loc 65
c 0
b 0
f 0
cc 1
rs 9.344

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace app\models;
4
5
use Yii;
6
use yii\helpers\ArrayHelper;
7
use Itstructure\AdminModule\interfaces\ModelInterface;
8
use Itstructure\AdminModule\models\ActiveRecord;
9
10
/**
11
 * This is the model class for table "feedback".
12
 *
13
 * @property int $id
14
 * @property string $name
15
 * @property string $email
16
 * @property string $phone
17
 * @property string $subject
18
 * @property string $message
19
 * @property int $read
20
 *
21
 * @package app\models
22
 */
23
class Feedback extends ActiveRecord implements ModelInterface
24
{
25
    const SCENARIO_CONTACT = 'contact';
26
    const SCENARIO_FEEDBACK = 'feedback';
27
28
    /**
29
     * @var string
30
     */
31
    public $verifyCode;
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public static function tableName()
37
    {
38
        return 'feedback';
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function rules()
45
    {
46
        return [
47
            [
48
                [
49
                    'name',
50
                    'email',
51
                    'subject',
52
                    'message',
53
                ],
54
                'required'
55
            ],
56
            [
57
                'message',
58
                'string',
59
                'max' => 2048
60
            ],
61
            [
62
                [
63
                    'name',
64
                    'email'
65
                ],
66
                'string',
67
                'max' => 64
68
            ],
69
            [
70
                'phone',
71
                'string',
72
                'max' => 32
73
            ],
74
            [
75
                'subject',
76
                'string',
77
                'max' => 255
78
            ],
79
            [
80
                'email',
81
                'email'
82
            ],
83
            [
84
                'read',
85
                'integer'
86
            ],
87
            [
88
                [
89
                    'created_at',
90
                    'updated_at',
91
                ],
92
                'safe',
93
            ],
94
            [
95
                'verifyCode',
96
                'captcha',
97
                'on' => [
98
                    self::SCENARIO_CONTACT
99
                ],
100
                'captchaAction' => 'contact/captcha'
101
            ],
102
            [
103
                'verifyCode',
104
                'captcha',
105
                'on' => [
106
                    self::SCENARIO_FEEDBACK
107
                ],
108
                'captchaAction' => 'feedback/captcha'
109
            ]
110
        ];
111
    }
112
113
    /**
114
     * Scenarios.
115
     *
116
     * @return array
117
     */
118
    public function scenarios()
119
    {
120
        return [
121
            self::SCENARIO_CONTACT => ArrayHelper::merge($this->attributes(), [
122
                'verifyCode'
123
            ]),
124
            self::SCENARIO_FEEDBACK => $this->attributes(),
125
        ];
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function attributeLabels()
132
    {
133
        return [
134
            'id' => 'ID',
135
            'name' => 'Name',
136
            'email' => 'Email',
137
            'phone' => 'Phone',
138
            'subject' => 'Subject',
139
            'message' => 'Message',
140
            'read' => 'Read',
141
            'verifyCode' => 'Verification Code',
142
        ];
143
    }
144
145
    /**
146
     * Returns id of the model.
147
     *
148
     * @return int
149
     */
150
    public function getId()
151
    {
152
        return $this->id;
153
    }
154
155
    /**
156
     * Set read status to "1" after view record.
157
     *
158
     * @param int $id
159
     */
160
    public static function fixReadStatus(int $id): void
161
    {
162
        static::updateAll([
163
            'read' => 1
164
        ], [
165
            'id' => $id
166
        ]);
167
    }
168
169
    /**
170
     * Sends an email to the specified email address using the information collected by this model.
171
     *
172
     * @param string $email the target email address
173
     *
174
     * @return bool whether the model passes validation
175
     */
176
    public function contact($email)
177
    {
178
        if ($this->save()) {
179
            Yii::$app->mailer->compose()
180
                ->setTo($email)
181
                ->setFrom([$this->email => $this->name])
182
                ->setSubject('New message from bizness-develop feedback. ' . $this->subject)
183
                ->setTextBody($this->message)
184
                ->send();
185
186
            return true;
187
        }
188
189
        return false;
190
    }
191
}
192