Completed
Pull Request — master (#122)
by Klochok
11:06
created

Banner::getLinkOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace hipanel\widgets;
4
5
use Yii;
6
use yii\base\Widget;
7
use yii\helpers\Html;
8
use yii\helpers\StringHelper;
9
10
/**
11
 * Class Banner lookup `module.core.ad.banners.sidebar` or `module.core.ad.banners.main` params and trying to display
12
 * banner images with a link transition. It expects that in these params there will be an associative array in which
13
 * the key is the transition link and the value is the link to the banner image.
14
 *
15
 * @package hipanel\widgets
16
 */
17
class Banner extends Widget
18
{
19
    /**
20
     * @var boolean
21
     */
22
    public $isSidebar = true;
23
24
    /**
25
     * @var array
26
     */
27
    public $items = [];
28
29
    /**
30
     * @var array
31
     */
32
    public $sidebarImgOptions = [
33
        'class' => 'img-responsive hidden-xs hidden-sm',
34
    ];
35
36
    /**
37
     * @var array
38
     */
39
    public $mainImgOptions = [];
40
41
    /**
42
     * @var array
43
     */
44
    public $sidebarLinkOptions = [
45
        'target' => '_blank',
46
        'rel' => 'noopener noreferrer',
47
    ];
48
49
    /**
50
     * @var array
51
     */
52
    public $mainLinkOptions = [
53
        'target' => '_blank',
54
        'rel' => 'noopener noreferrer',
55
    ];
56
57
    /**
58
     * @return string
59
     */
60
    public function run(): string
61
    {
62
        $html = '';
63
        foreach ($this->getItems() as $url => $src) {
64
            $html .= Html::a(Html::img($src, $this->getImgOptions()), $url, $this->getLinkOptions());
65
        }
66
67
        return $html;
68
    }
69
70
    /**
71
     * @return array
72
     */
73
    private function getImgOptions(): array
74
    {
75
        return $this->isSidebar ? $this->sidebarImgOptions : $this->mainImgOptions;
76
    }
77
78
    /**
79
     * @return array
80
     */
81
    private function getLinkOptions(): array
82
    {
83
        return $this->isSidebar ? $this->sidebarLinkOptions : $this->mainLinkOptions;
84
    }
85
86
    /**
87
     * @return array
88
     */
89
    private function getItems(): array
90
    {
91
        $items = [];
92
        if ($this->isSidebar === false && !StringHelper::endsWith(Yii::$app->request->url, 'dashboard/dashboard')) {
93
            return [];
94
        }
95
96
        if (!empty($this->items)) {
97
            $items = $this->items;
98
        }
99
100
        if ($formParams = Yii::$app->params[sprintf('module.core.ad.banners.%s', $this->isSidebar ? 'sidebar' : 'main')]) {
101
            $items = $formParams;
102
        }
103
104
        return $items;
105
    }
106
}
107