LinkToObjectResolver   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 91
ccs 0
cts 33
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 17 4
A getLabel() 0 4 1
A getLink() 0 15 4
1
<?php
2
/**
3
 * Finance module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-finance
6
 * @package   hipanel-module-finance
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\widgets;
12
13
use Yii;
14
use yii\base\Model;
15
use yii\base\Widget;
16
use yii\helpers\Html;
17
18
/**
19
 * Class LinkToObjectResolver.
20
 *
21
 * @author Dmytro Naumenko <[email protected]>
22
 * @author Andrey Klockok <[email protected]>
23
 */
24
class LinkToObjectResolver extends Widget
25
{
26
    /** @var Model */
27
    public $model;
28
29
    /** @var string */
30
    public $labelAttribute = 'object';
31
32
    /** @var string */
33
    public $typeAttribute = 'type';
34
35
    /** @var string */
36
    public $idAttribute = 'id';
37
38
    public $linkOptions = [
39
        'class' => 'text-bold',
40
    ];
41
42
    /**
43
     * Custom links for $links. For example:.
44
     *
45
     * ```php
46
     * 'customLinks' => [
47
     *   'part' => '@server/view',
48
     * ]
49
     * ```
50
     */
51
    public $customLinks = [];
52
53
    private $links = [
54
        'ip' => '@ip/view',
55
        'client' => '@client/view',
56
        'account' => '@account/view',
57
        'server' => '@server/view',
58
        'pcdn' => '@server/view',
59
        'vcdn' => '@server/view',
60
        'device' => '@server/view',
61
        'part' => '@part/view',
62
        'tariff' => '@plan/view',
63
        'switch' => '@hub/view',
64
        'model_group' => '@model-group/view',
65
    ];
66
67
    /**
68
     * @return string
69
     */
70
    public function run()
71
    {
72
        foreach ($this->customLinks as $link => $path) {
73
            $this->links[$link] = $path;
74
        }
75
        $label = $this->getLabel();
76
        if ($label === null) {
77
            return '';
78
        }
79
80
        $link = $this->getLink();
81
        if ($link === null) {
82
            return $this->getLabel();
83
        }
84
85
        return Html::a($this->getLabel(), $this->getLink(), $this->linkOptions);
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    private function getLabel()
92
    {
93
        return $this->model->{$this->labelAttribute};
94
    }
95
96
    /**
97
     * @return array|null
98
     */
99
    private function getLink()
100
    {
101
        $type = $this->model->{$this->typeAttribute};
102
        if (!Yii::getAlias($this->links[$type], false)) {
103
            return null;
104
        }
105
        if (!isset($this->links[$type])) {
106
            return null;
107
        }
108
        if (!isset($this->model->{$this->idAttribute})) {
109
            return null;
110
        }
111
112
        return [$this->links[$type], 'id' => $this->model->{$this->idAttribute}];
113
    }
114
}
115