Completed
Branch master (f58c14)
by Andrey
07:50
created

Feedback::getId()   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 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 [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array(self::SCENA...> $this->attributes()); (array<string,array>) is incompatible with the return type of the parent method Itstructure\AdminModule\...ActiveRecord::scenarios of type array<*,integer[]>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
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 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...
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