ObjectCombo   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 87
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 12 1
A getClasses() 0 15 4
A getClassOptions() 0 9 2
A getLabel() 0 4 2
1
<?php
2
/**
3
 * HiPanel core package
4
 *
5
 * @link      https://hipanel.com/
6
 * @package   hipanel-core
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\widgets\combo;
12
13
use hipanel\modules\client\widgets\combo\ClientCombo;
14
use hipanel\modules\domain\widgets\combo\DomainCombo;
15
use hipanel\modules\domain\widgets\combo\ZoneCombo;
16
use hipanel\modules\finance\widgets\combo\PlanCombo;
17
use hipanel\modules\hosting\widgets\combo\AccountCombo;
18
use hipanel\modules\server\widgets\combo\ServerCombo;
19
use hipanel\modules\stock\widgets\combo\PartCombo;
20
use Yii;
21
use yii\bootstrap\InputWidget;
22
23
/**
24
 * Class BaseObjectSelector.
25
 */
26
class ObjectCombo extends InputWidget
27
{
28
    /**
29
     * @var string
30
     */
31
    public $class_attribute = 'class';
32
33
    /**
34
     * @var string
35
     */
36
    public $class_attribute_name = 'class';
37
38
    /**
39
     * @var string Normally, when $model already has $attribute property filled,
40
     * and the $attribute is some ID, it will be resolved to name with an AJAX query.
41
     * In case page that contains this combo has a lot of inputs (e.g. bulk edit form)
42
     * each will produce an AJAX query, that will last forever.
43
     *
44
     * In case $model already has a name, corresponding to that ID (stored in $attribute),
45
     * you can help Combo by hinting that attribute name and thus preventing AJAX queries.
46
     */
47
    public $selectedAttributeName;
48
49
    /**
50
     * @var array
51
     */
52
    public $knownClasses = [
53
        'client' => ['alias' => '@client', 'combo' => ClientCombo::class],
54
        'device' => ['alias' => '@server', 'combo' => ServerCombo::class],
55
        'domain' => ['alias' => '@domain', 'combo' => DomainCombo::class],
56
        'zone' => ['alias' => '@domain', 'combo' => ZoneCombo::class],
57
        'part' => ['alias' => '@part', 'combo' => PartCombo::class],
58
        'account' => ['alias' => '@account', 'combo' => AccountCombo::class],
59
        'plan' => ['alias' => '@plan', 'combo' => PlanCombo::class],
60
    ];
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function run()
66
    {
67
        return $this->render('ObjectCombo', [
68
            'model' => $this->model,
69
            'attribute' => $this->attribute,
70
            'classOptions' => $this->getClassOptions(),
71
            'classes' => $this->getClasses(),
72
            'class_attribute' => $this->model->{$this->class_attribute},
73
            'class_attribute_name' => $this->class_attribute_name,
74
            'selectedAttributeName' => $this->selectedAttributeName,
75
        ]);
76
    }
77
78
    private function getClasses(): array
79
    {
80
        $classes = [];
81
        foreach ($this->knownClasses as $class => $options) {
82
            if (!Yii::getAlias($options['alias'], false)) {
83
                continue;
84
            }
85
            if ($options['combo']) {
86
                $options['comboOptions'] = get_class_vars($options['combo']);
87
            }
88
            $classes[$class] = $options;
89
        }
90
91
        return $classes;
92
    }
93
94
    private function getClassOptions(): array
95
    {
96
        $dropDownOptions = [];
97
        foreach ($this->getClasses() as $class => $options) {
98
            $dropDownOptions[$class] = Yii::t('hipanel.object-combo', $this->getLabel($class));
99
        }
100
101
        return $dropDownOptions;
102
    }
103
104
    /**
105
     * @param string $class
106
     * @return string
107
     */
108
    private function getLabel($class): string
109
    {
110
        return $class === 'device' ? 'Server' : ucfirst($class);
111
    }
112
}
113