Passed
Push — master ( 32a82b...c5c36a )
by Mr.
06:32
created

SvgInlineFontAwesome::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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