MaintenanceFormWidget   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 7
eloc 20
dl 0
loc 63
rs 10
c 1
b 1
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 4 1
A registerResource() 0 9 1
A run() 0 5 2
A getOptions() 0 5 1
A findModel() 0 6 2
1
<?php
2
3
namespace dominus77\maintenance\widgets\maintenance;
4
5
use yii\base\Widget;
6
use yii\helpers\Json;
7
use dominus77\maintenance\widgets\maintenance\assets\MaintenanceFormAsset;
8
use dominus77\maintenance\models\FileStateForm;
9
use dominus77\maintenance\Maintenance;
10
11
/**
12
 * Class MaintenanceFormWidget
13
 * @package dominus77\maintenance\widgets\maintenance
14
 *
15
 * @property array $options
16
 */
17
class MaintenanceFormWidget extends Widget
18
{
19
    /**
20
     * @var bool
21
     */
22
    public $status = true;
23
24
    /**
25
     * @var FileStateForm
26
     */
27
    public $model;
28
29
    public function init()
30
    {
31
        parent::init();
32
        $this->model = $this->findModel();
33
    }
34
35
    /**
36
     * @return string|void
37
     */
38
    public function run()
39
    {
40
        if ($this->status === true) {
41
            $this->registerResource();
42
            echo $this->render('maintenance-form', ['model' => $this->model]);
43
        }
44
    }
45
46
    /**
47
     * @return array
48
     */
49
    protected function getOptions()
50
    {
51
        return [
52
            'modeOn' => Maintenance::STATUS_CODE_MAINTENANCE,
53
            'modeOff' => Maintenance::STATUS_CODE_OK
54
        ];
55
    }
56
57
    /**
58
     * Register resource
59
     */
60
    protected function registerResource()
61
    {
62
        $view = $this->getView();
63
        MaintenanceFormAsset::register($view);
64
        $options = Json::encode($this->getOptions());
65
        $script = "            
66
            initMaintenanceForm({$options});
67
        ";
68
        $view->registerJs($script);
69
    }
70
71
    /**
72
     * @return FileStateForm
73
     */
74
    protected function findModel()
75
    {
76
        if ($this->model === null) {
77
            return new FileStateForm();
78
        }
79
        return $this->model;
80
    }
81
}
82