Passed
Push — master ( 62400b...78a114 )
by Aleksandr
03:25
created

DeleteAction   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 15
dl 0
loc 32
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 16 5
A hasSoftDeleteError() 0 6 2
1
<?php
2
3
4
namespace carono\yii2crud\actions;
5
6
use carono\yii2crud\CrudController;
7
use Yii;
8
use yii\db\ActiveRecord;
9
10
/**
11
 * Class DeleteAction
12
 *
13
 * @package carono\yii2crud\actions
14
 * @property CrudController $controller
15
 */
16
class DeleteAction extends Action
17
{
18
    public $softDeleteAttribute = 'deleted_at';
19
20
    public function run($id)
21
    {
22
        $model = $this->controller->findModel($id);
23
        $model->delete();
24
        if ($model->hasErrors() || $this->hasSoftDeleteError($model)) {
25
            $msg = current($model->getFirstErrors());
26
            Yii::$app->session->setFlash('error', $msg ?: Yii::t('errors', 'Fail Deleting Model'));
0 ignored issues
show
Bug introduced by
The method setFlash() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
            Yii::$app->session->/** @scrutinizer ignore-call */ 
27
                                setFlash('error', $msg ?: Yii::t('errors', 'Fail Deleting Model'));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
27
        } else {
28
            Yii::$app->session->setFlash('success', Yii::t('messages', 'Model deleted'));
29
        }
30
31
        if (Yii::$app->request->isAjax) {
32
            return Yii::$app->response->redirect(Yii::$app->request->referrer, 302, false);
33
        }
34
35
        return $this->controller->redirect($this->controller->deleteRedirect($model));
36
    }
37
38
    /**
39
     * @param ActiveRecord $model
40
     * @return bool
41
     */
42
    public function hasSoftDeleteError($model): bool
43
    {
44
        if ($model->hasAttribute($this->softDeleteAttribute)) {
45
            return empty($model->{$this->softDeleteAttribute});
46
        }
47
        return false;
48
    }
49
}