RenderAction   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 71
ccs 0
cts 28
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getParams() 0 13 4
A prepareData() 0 4 2
A setParams() 0 4 1
A getViewName() 0 4 2
A run() 0 4 1
1
<?php
2
/**
3
 * HiSite Yii2 base project.
4
 *
5
 * @link      https://github.com/hiqdev/hisite
6
 * @package   hisite
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hisite\actions;
12
13
use Closure;
14
use yii\helpers\Inflector;
15
16
/**
17
 * This action renders view.
18
 *
19
 * @property array|Closure $params that will be passed to the view.
20
 * Every element can be a callback, which gets $this pointer as argument
21
 */
22
class RenderAction extends \yii\base\Action
23
{
24
    /**
25
     * @var string view to render
26
     */
27
    public $view;
28
29
    /**
30
     * @var array|Closure additional data passed when rendering
31
     */
32
    public $data = [];
33
34
    /**
35
     * @var array
36
     */
37
    public $_params = [];
38
39
    /**
40
     * Prepares params for rendering, executing callable functions.
41
     *
42
     * @return array
43
     */
44
    public function getParams()
45
    {
46
        $res = [];
47
        if ($this->_params instanceof Closure) {
48
            $res = call_user_func($this->_params, $this);
49
        } else {
50
            foreach ($this->_params as $k => $v) {
51
                $res[$k] = $v instanceof Closure ? call_user_func($v, $this) : $v;
52
            }
53
        }
54
55
        return array_merge($res, $this->prepareData($res));
56
    }
57
58
    /**
59
     * Prepares additional data for render.
60
     *
61
     * @param $data array Additional data, prepared by other classes. Optional
62
     * @return array
63
     */
64
    public function prepareData($data = [])
65
    {
66
        return (array) ($this->data instanceof Closure ? call_user_func($this->data, $this, $data) : $this->data);
67
    }
68
69
    /**
70
     * @param array $params
71
     */
72
    public function setParams($params)
73
    {
74
        $this->_params = $params;
75
    }
76
77
    /**
78
     * @return string [[view]] or [[scenario]]
79
     */
80
    public function getViewName()
81
    {
82
        return $this->view ?: lcfirst(Inflector::id2camel($this->controller->action->id));
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function run()
89
    {
90
        return $this->controller->render($this->getViewName(), $this->getParams());
91
    }
92
}
93