Passed
Pull Request — master (#19870)
by Rutger
08:20
created

Action   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 86.96%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 83
ccs 20
cts 23
cp 0.8696
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A runWithParams() 0 18 4
A __construct() 0 5 1
A beforeRun() 0 3 1
A getUniqueId() 0 3 1
A afterRun() 0 3 1
1
<?php
2
/**
3
 * @link https://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license https://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-read string $uniqueId The unique ID of this action among the whole application.
34
 *
35
 * @author Qiang Xue <[email protected]>
36
 * @since 2.0
37
 */
38
class Action extends Component
39
{
40
    /**
41
     * @var string ID of the action
42
     */
43
    public $id;
44
    /**
45
     * @var Controller|\yii\web\Controller|\yii\console\Controller the controller that owns this action
46
     */
47
    public $controller;
48
49
50
    /**
51
     * Constructor.
52
     *
53
     * @param string $id the ID of this action
54
     * @param Controller $controller the controller that owns this action
55
     * @param array $config name-value pairs that will be used to initialize the object properties
56
     */
57 439
    public function __construct($id, $controller, $config = [])
58
    {
59 439
        $this->id = $id;
60 439
        $this->controller = $controller;
61 439
        parent::__construct($config);
62 439
    }
63
64
    /**
65
     * Returns the unique ID of this action among the whole application.
66
     *
67
     * @return string the unique ID of this action among the whole application.
68
     */
69 313
    public function getUniqueId()
70
    {
71 313
        return $this->controller->getUniqueId() . '/' . $this->id;
72
    }
73
74
    /**
75
     * Runs this action with the specified parameters.
76
     * This method is mainly invoked by the controller.
77
     *
78
     * @param array $params the parameters to be bound to the action's run() method.
79
     * @return mixed the result of the action
80
     * @throws InvalidConfigException if the action class does not have a run() method
81
     */
82 14
    public function runWithParams($params)
83
    {
84 14
        if (!method_exists($this, 'run')) {
85
            throw new InvalidConfigException(get_class($this) . ' must define a "run()" method.');
86
        }
87 14
        $args = $this->controller->bindActionParams($this, $params);
88 14
        Yii::debug('Running action: ' . get_class($this) . '::run(), invoked by '  . get_class($this->controller), __METHOD__);
89 14
        if (Yii::$app->requestedParams === null) {
90
            Yii::$app->requestedParams = $args;
91
        }
92 14
        if ($this->beforeRun()) {
93 14
            $result = call_user_func_array([$this, 'run'], $args);
94 13
            $result = $this->afterRun($result);
95
96 13
            return $result;
97
        }
98
99
        return null;
100
    }
101
102
    /**
103
     * This method is called right before `run()` is executed.
104
     * You may override this method to do preparation work for the action run.
105
     * If the method returns false, it will cancel the action.
106
     *
107
     * @return bool whether to run the action.
108
     */
109 14
    protected function beforeRun()
110
    {
111 14
        return true;
112
    }
113
114
    /**
115
     * This method is called right after `run()` is executed.
116
     * You may override this method to do post-processing work for the action run.
117
     */
118 13
    protected function afterRun($result)
119
    {
120 13
        return $result;
121
    }
122
}
123