Completed
Push — master ( 155575...f6b9aa )
by Dmitry
31:24 queued 13:49
created

Action   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 91.67%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 84
ccs 22
cts 24
cp 0.9167
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getUniqueId() 0 4 1
A runWithParams() 0 19 4
A beforeRun() 0 4 1
A afterRun() 0 3 1
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\base;
9
10
use Yii;
11
12
/**
13
 * Action is the base class for all controller action classes.
14
 *
15
 * Action provides a way to reuse action method code. An action method in an Action
16
 * class can be used in multiple controllers or in different projects.
17
 *
18
 * Derived classes must implement a method named `run()`. This method
19
 * will be invoked by the controller when the action is requested.
20
 * The `run()` method can have parameters which will be filled up
21
 * with user input values automatically according to their names.
22
 * For example, if the `run()` method is declared as follows:
23
 *
24
 * ```php
25
 * public function run($id, $type = 'book') { ... }
26
 * ```
27
 *
28
 * And the parameters provided for the action are: `['id' => 1]`.
29
 * Then the `run()` method will be invoked as `run(1)` automatically.
30
 *
31
 * For more details and usage information on Action, see the [guide article on actions](guide:structure-controllers).
32
 *
33
 * @property string $uniqueId The unique ID of this action among the whole application. This property is
34
 * read-only.
35
 *
36
 * @author Qiang Xue <[email protected]>
37
 * @since 2.0
38
 */
39
class Action extends Component
40
{
41
    /**
42
     * @var string ID of the action
43
     */
44
    public $id;
45
    /**
46
     * @var Controller|\yii\web\Controller the controller that owns this action
47
     */
48
    public $controller;
49
50
51
    /**
52
     * Constructor.
53
     *
54
     * @param string $id the ID of this action
55
     * @param Controller $controller the controller that owns this action
56
     * @param array $config name-value pairs that will be used to initialize the object properties
57
     */
58 143
    public function __construct($id, $controller, $config = [])
59
    {
60 143
        $this->id = $id;
61 143
        $this->controller = $controller;
62 143
        parent::__construct($config);
63 143
    }
64
65
    /**
66
     * Returns the unique ID of this action among the whole application.
67
     *
68
     * @return string the unique ID of this action among the whole application.
69
     */
70 95
    public function getUniqueId()
71
    {
72 95
        return $this->controller->getUniqueId() . '/' . $this->id;
73
    }
74
75
    /**
76
     * Runs this action with the specified parameters.
77
     * This method is mainly invoked by the controller.
78
     *
79
     * @param array $params the parameters to be bound to the action's run() method.
80
     * @return mixed the result of the action
81
     * @throws InvalidConfigException if the action class does not have a run() method
82
     */
83 6
    public function runWithParams($params)
84
    {
85 6
        if (!method_exists($this, 'run')) {
86
            throw new InvalidConfigException(get_class($this) . ' must define a "run()" method.');
87
        }
88 6
        $args = $this->controller->bindActionParams($this, $params);
89 6
        Yii::trace('Running action: ' . get_class($this) . '::run()', __METHOD__);
90 6
        if (Yii::$app->requestedParams === null) {
91 6
            Yii::$app->requestedParams = $args;
92 6
        }
93 6
        if ($this->beforeRun()) {
94 6
            $result = call_user_func_array([$this, 'run'], $args);
95 6
            $this->afterRun();
96
97 6
            return $result;
98
        } else {
99
            return null;
100
        }
101
    }
102
103
    /**
104
     * This method is called right before `run()` is executed.
105
     * You may override this method to do preparation work for the action run.
106
     * If the method returns false, it will cancel the action.
107
     *
108
     * @return bool whether to run the action.
109
     */
110 6
    protected function beforeRun()
111
    {
112 6
        return true;
113
    }
114
115
    /**
116
     * This method is called right after `run()` is executed.
117
     * You may override this method to do post-processing work for the action run.
118
     */
119 6
    protected function afterRun()
120
    {
121 6
    }
122
}
123