1 | <?php |
||
2 | |||
3 | namespace backend\controllers; |
||
4 | |||
5 | use Yii; |
||
6 | use backend\models\MailerAccount; |
||
7 | use backend\models\MailerDomain; |
||
8 | use backend\models\search\MailerAccountSearch; |
||
9 | use backend\components\Controller; |
||
10 | use yii\web\NotFoundHttpException; |
||
11 | |||
12 | /** |
||
13 | * MailerAccountsController implements the CRUD actions for MailerAccount model. |
||
14 | * @method MailerAccount findModel($id) |
||
15 | */ |
||
16 | class MailerAccountsController extends Controller |
||
17 | { |
||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $modelClass = MailerAccount::class; |
||
22 | |||
23 | /** |
||
24 | * Lists all MailerAccount models. |
||
25 | * @return mixed |
||
26 | */ |
||
27 | public function actionIndex() |
||
28 | { |
||
29 | $searchModel = new MailerAccountSearch(); |
||
30 | return $this->render('index', [ |
||
31 | 'searchModel' => $searchModel, |
||
32 | 'dataProvider' => $searchModel |
||
33 | ->search(Yii::$app->request->queryParams), |
||
34 | ]); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Displays a single MailerAccount model. |
||
39 | * @param integer $id |
||
40 | * @return mixed |
||
41 | * @throws NotFoundHttpException if the model cannot be found |
||
42 | */ |
||
43 | public function actionView($id) |
||
44 | { |
||
45 | return $this->render('view', [ |
||
46 | 'model' => $this->findModel($id), |
||
47 | ]); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Creates a new MailerAccount model. |
||
52 | * If creation is successful, the browser will be redirected to the 'view' page. |
||
53 | * @return mixed |
||
54 | */ |
||
55 | public function actionCreate() |
||
56 | { |
||
57 | $model = new MailerAccount(); |
||
58 | if ($model->load(Yii::$app->request->post()) && $model->validate()) { |
||
59 | $password = $model->generatePassword(); |
||
60 | if ($model->save(false)) { |
||
61 | $this |
||
62 | ->addFlashMessage('Account created: ' . $model->email, self::FLASH_SUCCESS) |
||
63 | ->addFlashMessage('New password: ' . $password); |
||
64 | } |
||
65 | return $this->redirect(['view', 'id' => $model->id]); |
||
66 | } |
||
67 | return $this->render('create', [ |
||
68 | 'model' => $model, |
||
69 | ]); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Updates an existing MailerAccount model. |
||
74 | * If update is successful, the browser will be redirected to the 'view' page. |
||
75 | * @param integer $id |
||
76 | * @return mixed |
||
77 | * @throws NotFoundHttpException if the model cannot be found |
||
78 | */ |
||
79 | public function actionUpdate($id) |
||
80 | { |
||
81 | $model = $this->findModel($id); |
||
82 | $request = Yii::$app->request; |
||
0 ignored issues
–
show
|
|||
83 | if ($model->load($request->post()) && $model->validate()) { |
||
84 | $password = null; |
||
85 | if (!empty($request->post($model->formName())['password'])) { |
||
86 | $password = $model->generatePassword(); |
||
87 | } |
||
88 | if ($model->save(false)) { |
||
89 | $this->addFlashMessage('Account updated: ' . $model->email, self::FLASH_SUCCESS); |
||
90 | if ($password) { |
||
91 | $this->addFlashMessage('New password: ' . $password); |
||
92 | } |
||
93 | } |
||
94 | return $this->redirect(['view', 'id' => $model->id]); |
||
95 | } |
||
96 | return $this->render('update', [ |
||
97 | 'model' => $model, |
||
98 | ]); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Deletes an existing MailerAccount model. |
||
103 | * If deletion is successful, the browser will be redirected to the 'index' page. |
||
104 | * @param integer $id |
||
105 | * @return mixed |
||
106 | * @throws NotFoundHttpException if the model cannot be found |
||
107 | */ |
||
108 | public function actionDelete($id) |
||
109 | { |
||
110 | $model = $this->findModel($id); |
||
111 | if ($model->delete()) { |
||
0 ignored issues
–
show
The expression
$model->delete() of type integer|false is loosely compared to true ; this is ambiguous if the integer can be 0. You might want to explicitly use !== false instead.
In PHP, under loose comparison (like For 0 == false // true
0 == null // true
123 == false // false
123 == null // false
// It is often better to use strict comparison
0 === false // false
0 === null // false
![]() |
|||
112 | $this->addFlashMessage('Account deleted: ' . $model->email, self::FLASH_WARNING); |
||
113 | } |
||
114 | return $this->redirect(['index']); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @inheritdoc |
||
119 | */ |
||
120 | public function render($view, $params = []) |
||
121 | { |
||
122 | return parent::render($view, $params + [ |
||
123 | 'domainsList' => MailerDomain::find() |
||
124 | ->getListValues(), |
||
125 | ]); |
||
126 | } |
||
127 | } |
||
128 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.