OnOffMonitoringButton   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 44
dl 0
loc 73
rs 10
c 2
b 1
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 7 2
A getIdent() 0 13 3
A run() 0 29 4
1
<?php
2
/**
3
 * Created for IG Monitoring.
4
 * User: jakim <[email protected]>
5
 * Date: 06.02.2018
6
 */
7
8
namespace app\modules\admin\widgets;
9
10
11
use app\components\ArrayHelper;
12
use app\dictionaries\TrackerType;
13
use app\models\Tag;
14
use app\modules\admin\models\MonitoringForm;
15
use yii\base\Widget;
16
use yii\helpers\Url;
17
18
class OnOffMonitoringButton extends Widget
19
{
20
    /**
21
     * @var \app\models\Tag|\app\models\Account
22
     */
23
    public $model;
24
    public $form;
25
    public $btnCssClass = 'btn btn-block';
26
    public $stopBtnCssClass = 'btn-danger';
27
    public $startBtnCssClass = 'btn-success';
28
    public $stopBtnLabel = '<span class="fa fa-stop"></span> Turn off monitoring';
29
    public $startBtnLabel = '<span class="fa fa-play"></span> Turn on monitoring';
30
31
    public $offAjaxOptions = [];
32
33
    protected $trackerType = TrackerType::ACCOUNT;
34
35
    public function init()
36
    {
37
        parent::init();
38
        if ($this->model instanceof Tag) {
39
            $this->trackerType = TrackerType::TAG;
40
        } else {
41
            $this->trackerType = TrackerType::ACCOUNT;
42
        }
43
    }
44
45
    public function run()
46
    {
47
        if ($this->model->monitoring) {
48
            return AjaxButton::widget([
49
                'confirm' => true,
50
                'url' => Url::to(["monitoring/delete-{$this->trackerType}", 'id' => $this->model->id]),
51
                'options' => [
52
                    'class' => "{$this->btnCssClass} {$this->stopBtnCssClass}",
53
                    'data' => [
54
                        'style' => 'slide-right',
55
                    ],
56
                ],
57
                'text' => $this->stopBtnLabel,
58
                'ajaxOptions' => $this->offAjaxOptions,
59
            ]);
60
        }
61
62
        $form = $this->form ?: new MonitoringForm([
63
            'scenario' => $this->trackerType,
64
            'names' => $this->getIdent(),
65
            'categories' => $this->trackerType == TrackerType::ACCOUNT ? ArrayHelper::getColumn($this->model->categories, 'name') : null,
0 ignored issues
show
Bug Best Practice introduced by
The property categories does not exist on app\models\Tag. Since you implemented __get, consider adding a @property annotation.
Loading history...
66
        ]);
67
68
        return CreateMonitoringModal::widget([
69
            'form' => $form,
70
            'trackerType' => $this->trackerType,
71
            'modalToggleButton' => [
72
                'class' => "{$this->btnCssClass} {$this->startBtnCssClass}",
73
                'label' => $this->startBtnLabel,
74
            ],
75
        ]);
76
    }
77
78
    protected function getIdent(): string
79
    {
80
        switch ($this->trackerType) {
81
            case TrackerType::ACCOUNT:
82
                $ident = 'username';
83
                break;
84
            case TrackerType::TAG:
85
            default:
86
                $ident = 'name';
87
                break;
88
        }
89
90
        return ArrayHelper::getValue($this->model, $ident);
0 ignored issues
show
Bug Best Practice introduced by
The expression return app\components\Ar...e($this->model, $ident) could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
91
    }
92
}