Passed
Push — master ( 408c81...18b5ec )
by Mr.
02:52
created

SvgInlineFontAwesome::setSvgSize()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 14
rs 9.9332
cc 4
nc 3
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace YiiRocks\SvgInline\FontAwesome;
6
7
use Yiisoft\Assets\AssetManager;
8
use Yiisoft\Html\Html;
9
10
/**
11
 * SvgInlineFontAwesome provides a quick and easy way to access Font Awesome Icons.
12
 */
13
final class SvgInlineFontAwesome extends \YiiRocks\SvgInline\SvgInline implements SvgInlineFontAwesomeInterface
14
{
15
    /** @var string CSS class basename */
16
    protected string $prefix;
17
18
    /** @var string Default style */
19
    protected string $style;
20
21
    /** @var string Path to the Font Awesome Icons folder */
22
    private string $faIconsFolder;
23
24
    /** @var bool `true` for fixed-width class */
25
    private bool $fixedWidth;
26
27
    /** @var FontAwesomeIcon icon properties */
28
    private Object $icon;
29
30
    /**
31
     * Sets the name of the icon.
32
     *
33
     * @param string $name  name of the icon
34
     * @return FontAwesomeIcon component object
35
     */
36
    public function name(string $name, ?string $style = null): FontAwesomeIcon
37
    {
38
        $this->icon = new FontAwesomeIcon();
39
        $iconFile = implode(DIRECTORY_SEPARATOR, [$this->faIconsFolder, $style ?? $this->style, "{$name}.svg"]);
40
        $this->icon->setName($iconFile);
41
42
        return $this->icon;
43
    }
44
45
    /**
46
     * Register Font Awesome CSS file to asset manager
47
     *
48
     * @param AssetManager $assetManager
49
     * @return void
50
     */
51
    public function registerAssets(AssetManager $assetManager): void
52
    {
53
        $assetManager->register([
54
            FontawesomeAsset::class,
55
        ]);
56
    }
57
58
    /**
59
     * @see $fixedWidth
60
     * @param bool $value
61
     * @return void
62
     */
63
    public function setFixedWidth(bool $value): void
64
    {
65
        $this->fixedWidth = $value;
66
    }
67
68
    /**
69
     * @see $bootstrapIconsFolder
70
     * @param string $value
71
     * @return void
72
     */
73
    public function setFontAwesomeIconsFolder(string $value): void
74
    {
75
        $this->faIconsFolder = $this->aliases->get($value);
76
    }
77
78
    /**
79
     * @see $prefix
80
     * @param string $value
81
     * @return void
82
     */
83
    public function setPrefix(string $value): void
84
    {
85
        $this->prefix = $value;
86
    }
87
88
    /**
89
     * @see $style
90
     * @param string $value
91
     * @return void
92
     */
93
    public function setStyle(string $value): void
94
    {
95
        $this->style = $value;
96
    }
97
98
    /**
99
     * Prepares either the size class (default) or the width/height if either of these is given manually.
100
     *
101
     * @return void
102
     */
103
    protected function setSvgSize(): void
104
    {
105
        parent::setSvgSize();
106
107
        $width = $this->icon->get('width');
108
        $height = $this->icon->get('height');
109
110
        if (!$width && !$height) {
111
            Html::addCssClass($this->class, $this->prefix);
112
            $widthClass = $this->icon->get('fixedWidth')
113
                ? "{$this->prefix}-fw"
114
                : "{$this->prefix}-w-" . ceil($this->svgWidth / $this->svgHeight * 16);
115
            Html::addCssClass($this->class, $widthClass);
116
            unset($this->svgProperties['width'], $this->svgProperties['height']);
117
        }
118
    }
119
}
120