Completed
Push — master ( ff2b0b...f369f8 )
by Alexey
05:58
created

IndexAction::init()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 11
rs 10
cc 3
nc 4
nop 0
1
<?php
2
3
namespace common\components\maintenance\actions\frontend;
4
5
use common\components\maintenance\states\FileState;
6
use Yii;
7
use yii\base\Action;
8
use common\components\maintenance\interfaces\StateInterface;
9
use common\components\maintenance\models\SubscribeForm;
10
11
/**
12
 * Class IndexAction
13
 * @package common\components\maintenance\actions\frontend
14
 *
15
 * @property array $viewRenderParams
16
 */
17
class IndexAction extends Action
18
{
19
    /** @var string */
20
    public $defaultName;
21
22
    /** @var string */
23
    public $defaultMessage;
24
25
    /** @var string */
26
    public $layout = 'maintenance';
27
28
    /** @var string */
29
    public $view;
30
31
    /** @var array */
32
    public $params = [];
33
34
    /**
35
     * @var StateInterface
36
     */
37
    protected $state;
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function init()
43
    {
44
        parent::init();
45
        $this->state = Yii::$container->get(StateInterface::class);
46
47
        if ($this->defaultMessage === null) {
48
            $this->defaultMessage = Yii::t('app', $this->state->getParams(FileState::MAINTENANCE_PARAM_CONTENT));
49
        }
50
51
        if ($this->defaultName === null) {
52
            $this->defaultName = Yii::t('app', $this->state->getParams(FileState::MAINTENANCE_PARAM_TITLE));
53
        }
54
    }
55
56
    /**
57
     * @return string
58
     * @throws \Exception
59
     */
60
    public function run()
61
    {
62
        if ($this->layout !== null) {
63
            $this->controller->layout = $this->layout;
64
        }
65
        return $this->controller->render($this->view ?: $this->id, $this->getViewRenderParams());
66
    }
67
68
    /**
69
     * @return array
70
     * @throws \Exception
71
     */
72
    protected function getViewRenderParams()
73
    {
74
        $model = new SubscribeForm();
75
        return [
76
            'name' => $this->defaultName,
77
            'message' => $this->defaultMessage,
78
            'model' => $model,
79
        ];
80
    }
81
}
82