Passed
Push — master ( 4d325a...bebd8f )
by Alexey
02:23
created

IndexAction::setMessage()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 11
rs 10
cc 4
nc 4
nop 2
1
<?php
2
3
namespace dominus77\maintenance\actions\backend;
4
5
use Yii;
6
use yii\base\Action;
7
use yii\web\Response;
8
use dominus77\maintenance\models\FileStateForm;
9
use dominus77\maintenance\BackendMaintenance;
10
use yii\web\Session;
11
12
/**
13
 * Class IndexAction
14
 * @package dominus77\maintenance\actions
15
 *
16
 * @property array $viewRenderParams
17
 */
18
class IndexAction extends Action
19
{
20
    /** @var string */
21
    public $defaultName;
22
23
    /** @var string */
24
    public $layout;
25
26
    /** @var string */
27
    public $viewPath;
28
29
    /** @var string */
30
    public $view;
31
32
    /** @var array */
33
    public $params = [];
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function init()
39
    {
40
        parent::init();
41
42
        if ($this->defaultName === null) {
43
            $this->defaultName = BackendMaintenance::t('app', 'Mode site');
44
        }
45
    }
46
47
    /**
48
     * @return string|Response
49
     */
50
    public function run()
51
    {
52
        $this->setViewPath();
53
        $model = new FileStateForm();
54
        if (($post = Yii::$app->request->post()) && $model->load($post) && $model->validate()) {
55
            $message = $model->isEnabled() ? BackendMaintenance::t('app', 'Maintenance mode successfully updated!') : '';
56
            $result = $model->save();
57
            $this->setMessage($message, $result);
58
            return $this->controller->refresh();
59
        }
60
        return $this->controller->render($this->view ?: $this->id, $this->getViewRenderParams($model));
61
    }
62
63
    /**
64
     * @param $message string
65
     * @param $result bool|int
66
     */
67
    protected function setMessage($message, $result)
68
    {
69
        /** @var Session $session */
70
        $session = Yii::$app->session;
71
        if (is_bool($result) && $result === true) {
72
            $session->setFlash(FileStateForm::MAINTENANCE_UPDATE_KEY, $message);
73
        }
74
        if (is_numeric($result)) {
75
            $session->setFlash(FileStateForm::MAINTENANCE_NOTIFY_SENDER_KEY, BackendMaintenance::t('app',
76
                '{n, plural, =0{no followers} =1{one message sent} other{# messages sent}}',
77
                ['n' => $result])
78
            );
79
        }
80
    }
81
82
    /**
83
     * @param $model FileStateForm
84
     * @return array
85
     */
86
    protected function getViewRenderParams($model)
87
    {
88
        return [
89
            'name' => $this->defaultName,
90
            'model' => $model,
91
        ];
92
    }
93
94
    /**
95
     * Set View Path
96
     */
97
    protected function setViewPath()
98
    {
99
        if ($this->viewPath !== null) {
100
            $this->controller->setViewPath($this->viewPath);
101
        } else {
102
            $this->controller->setViewPath('@dominus77/maintenance/views/backend/maintenance');
103
        }
104
    }
105
}
106