Completed
Push — master ( bf2680...d511ea )
by Dmitry
13s queued 11s
created

LogoLink::isSmallImage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * Pluggable themes for Yii2
4
 *
5
 * @link      https://github.com/hiqdev/yii2-thememanager
6
 * @package   yii2-thememanager
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\thememanager\widgets;
12
13
use Yii;
14
use yii\base\Widget;
15
use yii\helpers\Html;
16
17
/**
18
 * LogoLink widget.
19
 * @author Andrii Vasyliev <[email protected]>
20
 */
21
class LogoLink extends Widget
22
{
23
    /**
24
     * @var array html options
25
     */
26
    public $options = [];
27
28
    /**
29
     * @var string url to image
30
     */
31
    public $image;
32
33
    /**
34
     * @var string text to display
35
     */
36
    public $name;
37
38
    /**
39
     * @var array|string text to display
40
     */
41
    public $url;
42
43
    public $smallImage;
44
45
    public $imageOptions = [];
46
47
    public $smallImageOptions = [];
48
49
    public function run()
50
    {
51
        if ($this->isImage()) {
52
            return $this->renderImage($this->image, $this->imageOptions);
53
        }
54
55
        if ($this->isSmallImage()) {
56
            return $this->renderImage($this->smallImage, $this->smallImageOptions);
57
        }
58
59
        return Html::a($this->name, $this->url, $this->options);
60
    }
61
62
    private function isImage(): bool
63
    {
64
        return !empty($this->image);
65
    }
66
67
    private function isSmallImage(): bool
68
    {
69
        return !empty($this->smallImage);
70
    }
71
72
    private function renderImage(string $image, array $imageOptions): string
73
    {
74
        return Html::beginTag('a', array_merge(['href' => $this->url, 'class' => 'logo'], $this->options))
75
                . Html::img($this->getImage($image), $imageOptions)
76
                . Html::endTag('a');
77
    }
78
79
    protected function getImage($path)
80
    {
81
        return mb_substr($path, 0, 1, 'utf-8') === '@' ? Yii::$app->assetManager->publish($path)[1] : $path;
82
    }
83
}
84