Completed
Push — master ( 62ede7...735413 )
by Nate
03:44
created

ModelFilter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 5
dl 0
loc 130
ccs 0
cts 54
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A afterAction() 0 7 2
A handleModel() 0 20 3
A resolveSuccessMessage() 0 7 1
A resolveFailMessage() 0 7 1
A findHandler() 0 23 3
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/guardian/license
6
 * @link       https://www.flipboxfactory.com/software/guardian/
7
 */
8
9
namespace flipbox\spark\filters;
10
11
use Craft;
12
use craft\helpers\ArrayHelper;
13
use flipbox\guardian\views\ViewInterface;
14
use yii\base\ActionFilter;
15
use yii\base\Exception;
16
use yii\base\Model;
17
use craft\web\Controller;
18
use yii\web\Response;
19
20
/**
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 1.0.0
23
 *
24
 * @property Controller $sender
25
 */
26
class ModelFilter extends ActionFilter
27
{
28
    /**
29
     * The success handler key
30
     */
31
    const SUCCESS_KEY = 'success';
32
33
    /**
34
     * The fail handler key
35
     */
36
    const FAIL_KEY = 'fail';
37
38
    /**
39
     * @var array this property defines the transformers for each action.
40
     * Each action that should only support one transformer.
41
     *
42
     * You can use `'*'` to stand for all actions. When an action is explicitly
43
     * specified, it takes precedence over the specification given by `'*'`.
44
     *
45
     * For example,
46
     *
47
     * ```php
48
     * [
49
     *   'create' => $handler,
50
     *   'update' => $handler,
51
     *   'delete' => $handler,
52
     *   '*' => $handler
53
     * ]
54
     * ```
55
     */
56
    public $actions = [];
57
58
    /**
59
     * @var array
60
     */
61
    public $handler = [
62
        self::SUCCESS_KEY => 'Successfully performed action',
63
        self::FAIL_KEY => 'Unable to perform action'
64
    ];
65
66
    /**
67
     * @param \yii\base\Action $action
68
     * @param mixed $result
69
     * @return null|Model|Response
70
     */
71
    public function afterAction($action, $result)
72
    {
73
        if (Craft::$app->getResponse()->format === Response::FORMAT_RAW) {
74
            return $result = $this->handleModel($result);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
75
        }
76
        return $result;
77
    }
78
79
    /**
80
     * @param Model $model
81
     * @return Model|null|Response
82
     */
83
    protected function handleModel(Model $model)
84
    {
85
        if (!$handler = $this->findHandler()) {
86
            return $model;
87
        }
88
89
        if ($model->hasErrors()) {
90
            Craft::$app->getSession()->setError(
91
                $this->resolveFailMessage($handler)
92
            );
93
94
            return null;
95
        }
96
97
        Craft::$app->getSession()->setNotice(
98
            $this->resolveSuccessMessage($handler)
99
        );
100
101
        $this->sender->redirectToPostedUrl($model);
102
    }
103
104
    /**
105
     * @param array $handler
106
     * @return mixed
107
     */
108
    private function resolveSuccessMessage(array $handler)
109
    {
110
        return ArrayHelper::getValue(
111
            $handler,
112
            self::SUCCESS_KEY
113
        );
114
    }
115
116
    /**
117
     * @param array $handler
118
     * @return mixed
119
     */
120
    private function resolveFailMessage(array $handler)
121
    {
122
        return ArrayHelper::getValue(
123
            $handler,
124
            self::FAIL_KEY
125
        );
126
    }
127
128
    /**
129
     * @return ViewInterface|array|null
130
     * @throws Exception
131
     */
132
    protected function findHandler()
133
    {
134
        // The requested action
135
        $action = Craft::$app->requestedAction->id;
136
137
        // Default
138
        $handler = $this->handler;
139
140
        // Look for definitions
141
        if (isset($this->actions[$action])) {
142
            $handler = array_merge(
143
                $handler,
144
                $this->actions[$action]
145
            );
146
        } elseif (isset($this->actions['*'])) {
147
            $handler = array_merge(
148
                $handler,
149
                $this->actions['*']
150
            );
151
        }
152
153
        return $handler;
154
    }
155
}
156