Completed
Push — master ( 4276b6...c641f7 )
by Klochok
56:40 queued 41:42
created

AdBanner::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
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 AdBanner trying to display banner images with a link transition.
12
 * It expects that it will be configured by DI in the block `definitions` and there banners will be transferred
13
 * to the properties `items`. The data must be of the key is the transition link
14
 * and the value is the link to the banner image.
15
 *
16
 * Example:
17
 * // to `definitions`
18
 *
19
 * \hipanel\widgets\AdBanner::class => [
20
 *     'items' => $params['ad-banner.dashboard.items'],
21
 * ],
22
 *
23
 * @package hipanel\widgets
24
 */
25
class AdBanner extends Widget
26
{
27
    /**
28
     * @var array
29
     */
30
    public $items = [];
31
32
    /**
33
     * @var array
34
     */
35
    public $imgOptions = [];
36
37
    /**
38
     * @var array
39
     */
40
    public $linkOptions = [
41
        'target' => '_blank',
42
        'rel' => 'noopener noreferrer',
43
    ];
44
45
    /**
46
     * @return string
47
     */
48
    public function run(): string
49
    {
50
        $html = '';
51
        foreach ($this->getItems() as $url => $src) {
52
            $html .= Html::a(Html::img($src, $this->imgOptions), $url, $this->linkOptions);
53
        }
54
55
        return $html;
56
    }
57
58
    /**
59
     * @return array
60
     */
61
    protected function getItems(): array
62
    {
63
        if (!($this instanceof SidebarAdBanner) && !StringHelper::endsWith(Yii::$app->request->url, 'dashboard/dashboard')) {
64
            return [];
65
        }
66
67
        return $this->items;
68
    }
69
}
70