IndexAction   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 30
c 1
b 0
f 0
dl 0
loc 86
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 11 3
A run() 0 5 2
A setViewPath() 0 6 2
A setLayout() 0 4 2
A getViewRenderParams() 0 10 2
1
<?php
2
3
namespace dominus77\maintenance\actions\frontend;
4
5
use Yii;
6
use dominus77\maintenance\interfaces\StateInterface;
7
use dominus77\maintenance\models\SubscribeForm;
8
use dominus77\maintenance\BackendMaintenance;
9
use dominus77\maintenance\models\FileStateForm;
10
use yii\base\Action;
11
use Exception;
12
13
/**
14
 * Class IndexAction
15
 * @package dominus77\maintenance\actions\frontend
16
 *
17
 * @property array $viewRenderParams
18
 */
19
class IndexAction extends Action
20
{
21
    /** @var string */
22
    public $defaultName;
23
24
    /** @var string */
25
    public $defaultMessage;
26
27
    /** @var string */
28
    public $viewPath;
29
30
    /** @var string */
31
    public $view;
32
33
    /** @var array */
34
    public $params = [];
35
36
    /**
37
     * @var StateInterface
38
     */
39
    protected $state;
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function init()
45
    {
46
        parent::init();
47
        $this->state = Yii::$container->get(StateInterface::class);
48
49
        if ($this->defaultMessage === null) {
50
            $this->defaultMessage = BackendMaintenance::t('app', $this->state->defaultContent);
51
        }
52
53
        if ($this->defaultName === null) {
54
            $this->defaultName = BackendMaintenance::t('app', $this->state->defaultTitle);
55
        }
56
    }
57
58
    /**
59
     * @return string
60
     * @throws Exception
61
     */
62
    public function run()
63
    {
64
        $this->setViewPath();
65
        $this->setLayout();
66
        return $this->controller->render($this->view ?: $this->id, $this->getViewRenderParams());
67
    }
68
69
    /**
70
     * @return array
71
     * @throws Exception
72
     */
73
    protected function getViewRenderParams()
74
    {
75
        $subscribeForm = new SubscribeForm();
76
        $fileStateForm = new FileStateForm();
77
        return [
78
            'title' => $fileStateForm->title ?: $this->defaultName,
79
            'name' => $fileStateForm->title,
80
            'message' => $fileStateForm->text,
81
            'subscribeForm' => $subscribeForm,
82
            'fileStateForm' => $fileStateForm,
83
        ];
84
    }
85
86
    /**
87
     * Set View Path
88
     */
89
    protected function setViewPath()
90
    {
91
        if ($this->viewPath !== null) {
92
            $this->controller->setViewPath($this->viewPath);
93
        } else {
94
            $this->controller->setViewPath('@dominus77/maintenance/views/frontend/maintenance');
95
        }
96
    }
97
98
    /**
99
     * Set Layout
100
     */
101
    protected function setLayout()
102
    {
103
        if ($this->controller->layout === null) {
104
            $this->controller->layout = '@dominus77/maintenance/views/frontend/layouts/maintenance';
105
        }
106
    }
107
}
108