DeleteAction   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 17
c 2
b 0
f 0
dl 0
loc 34
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 16 6
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
 * @method getMessageOnDelete()
16
 */
17
class DeleteAction extends Action
18
{
19
    public $softDeleteAttribute = 'deleted_at';
20
    public $preventAjaxRedirect = false;
21
    public $messageOnDelete = 'Model deleted';
22
23
    public function run($id)
24
    {
25
        $model = $this->controller->findModel($id);
26
        $model->delete();
27
        if ($model->hasErrors() || $this->hasSoftDeleteError($model)) {
28
            $msg = current($model->getFirstErrors());
29
            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

29
            Yii::$app->session->/** @scrutinizer ignore-call */ 
30
                                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...
30
        } else {
31
            Yii::$app->session->setFlash('success', $this->getMessageOnDelete($model));
0 ignored issues
show
Unused Code introduced by
The call to carono\yii2crud\actions\...n::getMessageOnDelete() has too many arguments starting with $model. ( Ignorable by Annotation )

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

31
            Yii::$app->session->setFlash('success', $this->/** @scrutinizer ignore-call */ getMessageOnDelete($model));

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
32
        }
33
34
        if (Yii::$app->request->isAjax && !$this->preventAjaxRedirect) {
35
            return Yii::$app->response->redirect($this->controller->deleteRedirect($model), 302, false);
36
        }
37
38
        return $this->controller->redirect($this->controller->deleteRedirect($model));
39
    }
40
41
    /**
42
     * @param ActiveRecord $model
43
     * @return bool
44
     */
45
    public function hasSoftDeleteError($model): bool
46
    {
47
        if ($model->hasAttribute($this->softDeleteAttribute)) {
48
            return empty($model->{$this->softDeleteAttribute});
49
        }
50
        return false;
51
    }
52
}