Completed
Push — master ( 3c8880...e2c218 )
by Dmitry
06:42 queued 02:46
created

RenderAction   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 54
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getParams() 0 12 4
A setParams() 0 4 1
A getViewName() 0 4 2
A run() 0 4 1
1
<?php
2
3
/*
4
 * HiPanel core package
5
 *
6
 * @link      https://hipanel.com/
7
 * @package   hipanel-core
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\actions;
13
14
use Closure;
15
16
/**
17
 * Class RenderAction.
18
 *
19
 * @property array params that will be passed to the view.
20
 * Every element can be a callback, which gets the model and $this pointer as arguments
21
 */
22
class RenderAction extends Action
23
{
24
    /**
25
     * @var string view to render.
26
     */
27
    public $view;
28
29
    /**
30
     * @var array
31
     */
32
    public $_params = [];
33
34
    /**
35
     * Prepares params for rendering, executing callable functions.
36
     *
37
     * @return array
38
     */
39
    public function getParams()
40
    {
41
        $res = [];
42
        if ($this->_params instanceof Closure) {
43
            $res = call_user_func($this->_params, $this);
44
        } else {
45
            foreach ($this->_params as $k => $v) {
46
                $res[$k] = $v instanceof Closure ? call_user_func($v, $this, $this->getModel()) : $v;
47
            }
48
        }
49
        return array_merge($res, $this->prepareData($res));
50
    }
51
52
    /**
53
     * @param array $params
54
     */
55
    public function setParams($params)
56
    {
57
        $this->_params = $params;
58
    }
59
60
    /**
61
     * @return string [[view]] or [[scenario]]
62
     */
63
    public function getViewName()
64
    {
65
        return $this->view ?: $this->getScenario();
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function run()
72
    {
73
        return $this->controller->render($this->getViewName(), $this->getParams());
74
    }
75
}
76