BaseAction   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 6

Test Coverage

Coverage 74.19%

Importance

Changes 4
Bugs 3 Features 1
Metric Value
wmc 12
c 4
b 3
f 1
lcom 3
cbo 6
dl 0
loc 125
ccs 23
cts 31
cp 0.7419
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getModel() 0 13 3
B redirect() 0 18 5
A addSuccessFlash() 0 8 2
A addErrorFlash() 0 8 2
1
<?php
2
/**
3
 * @link https://github.com/roboapp
4
 * @copyright Copyright (c) 2016 Roboapp
5
 * @license [MIT License](https://opensource.org/licenses/MIT)
6
 */
7
8
namespace roboapp\crud;
9
10
use Yii;
11
use yii\base\Action;
12
use yii\db\ActiveRecord;
13
use yii\di\Instance;
14
use yii\web\NotFoundHttpException;
15
use yii\web\Session;
16
17
/**
18
 * A base class for CRUD actions.
19
 *
20
 * @author iRipVanWinkle <[email protected]>
21
 * @since 1.0
22
 */
23
class BaseAction extends Action
24
{
25
    /**
26
     * @var callable a PHP callable that will be called to return the model corresponding
27
     * to the specified primary key value.
28
     * The signature of the callable should be:
29
     *
30
     * ```php
31
     * function ($params) {
32
     *     // $params is the params from request
33
     * }
34
     * ```
35
     *
36
     * The callable should return the model found. Otherwise the not found exception will be thrown.
37
     */
38
    public $findModel;
39
40
    /**
41
     * @var mixed $redirectTo the route to redirect to. It can be one of the followings:
42
     *
43
     * - A PHP callable. The callable will be executed to get route. The signature of the callable
44
     *   should be:
45
     *
46
     * ```php
47
     * function ($model){
48
     *     // $model is the model object.
49
     * }
50
     * ```
51
     *
52
     * The callable should return route/url to redirect to.
53
     *
54
     * - An array. Treated as route.
55
     * - A string. Treated as url.
56
     */
57
    public $redirectTo = null;
58
59
    /**
60
     * @var string the message shown after success completed operation.
61
     */
62
    public $successMessage = '';
63
64
    /**
65
     * @var string the message shown after completed operation with error.
66
     */
67
    public $errorMessage = '';
68
69
    /**
70
     * @var string the Session object or the application component ID of the session object.
71
     */
72
    public $session = 'session';
73
74
    /**
75
     * @var \yii\web\Controller the controller that owns this action
76
     */
77
    public $controller;
78
79
    /**
80
     * If the data model is not found, an exception is thrown HTTP 404
81
     * @param mixed $id
82
     * @return ActiveRecord
83
     * @throws NotFoundHttpException
84
     */
85 33
    public function getModel($id)
86
    {
87 33
        if (!is_array($id)) {
88 33
            $id = [$id];
89 33
        }
90
91 33
        $model = call_user_func($this->findModel, $id);
92 33
        if ($model === null) {
93 3
            throw new NotFoundHttpException('The requested page does not exist.');
94
        }
95
96 30
        return $model;
97
    }
98
99
100
    /**
101
     * Redirect to route passed as param
102
     * @param ActiveRecord $model
103
     * @return mixed
104
     */
105 21
    public function redirect(ActiveRecord $model = null)
106
    {
107 21
        $route = $this->redirectTo;
108 21
        $controller = $this->controller;
109 21
        $request = Yii::$app->getRequest();
110
111
        // if callable
112 21
        if ($model && $route instanceof \Closure) {
113
            $route = call_user_func($route, $model);
114
        }
115
116 21
        if ($route === null) {
117 6
            return $request->getReferrer() ?
118 6
                $controller->redirect($request->getReferrer()) : $controller->goHome();
119
        }
120
121 15
        return $controller->redirect($route);
122
    }
123
124
     /**
125
     * Adds flash message about success completed operation.
126
     */
127 12
    public function addSuccessFlash()
128
    {
129 12
        if ($this->successMessage) {
130
            /* @var Session $session */
131
            $session = Instance::of($this->session)->get();
132
            $session->addFlash('success', $this->successMessage);
133
        }
134 12
    }
135
136
    /**
137
     * Adds flash message if completed operation with error.
138
     */
139 9
    public function addErrorFlash()
140
    {
141 9
        if ($this->errorMessage) {
142
            /* @var Session $session */
143
            $session = Instance::of($this->session)->get();
144
            $session->addFlash('error', $this->errorMessage);
145
        }
146 9
    }
147
}
148