Completed
Push — master ( 193986...cdf399 )
by Andrey
11:47
created

Feedback::tableName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
173
    {
174
        if ($this->save()) {
175
            Yii::$app->mailer->compose()
176
                ->setTo($email)
177
                ->setFrom([$this->email => $this->name])
178
                ->setSubject('New message from bizness-develop feedback. ' . $this->subject)
179
                ->setTextBody($this->message)
180
                ->send();
181
182
            return true;
183
        }
184
185
        return false;
186
    }
187
}
188