SvgInlineFontAwesome   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 11
eloc 27
c 5
b 0
f 0
dl 0
loc 113
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setFixedWidth() 0 3 1
A setPrefix() 0 3 1
A setSvgSize() 0 13 4
A setStyle() 0 3 1
A name() 0 7 1
A __construct() 0 11 2
A setFontAwesomeIconsFolder() 0 3 1
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 bool $registerAssets
39
     */
40
    public function __construct(
41
        Aliases $aliases,
42
        AssetManager $assetManager,
43
        ContainerInterface $container,
44
        bool $registerAssets = false
45
    ) {
46
        parent::__construct($aliases, $container);
47
48
        if ($registerAssets) {
49
            $assetManager->register([
50
                FontawesomeAsset::class,
51
            ]);
52
        }
53
    }
54
55
    /**
56
     * Sets the name of the icon.
57
     *
58
     * @param string $name  name of the icon
59
     * @return FontAwesomeIcon component object
60
     */
61
    public function name(string $name, ?string $style = null): FontAwesomeIcon
62
    {
63
        $this->icon = new FontAwesomeIcon();
64
        $iconFile = implode(DIRECTORY_SEPARATOR, [$this->faIconsFolder, $style ?? $this->style, "{$name}.svg"]);
65
        $this->icon->setName($iconFile);
66
67
        return $this->icon;
68
    }
69
70
    /**
71
     * @see $fixedWidth
72
     * @param bool $value
73
     * @return void
74
     */
75
    public function setFixedWidth(bool $value): void
76
    {
77
        $this->fixedWidth = $value;
78
    }
79
80
    /**
81
     * @see $bootstrapIconsFolder
82
     * @param string $value
83
     * @return void
84
     */
85
    public function setFontAwesomeIconsFolder(string $value): void
86
    {
87
        $this->faIconsFolder = $this->aliases->get($value);
88
    }
89
90
    /**
91
     * @see $prefix
92
     * @param string $value
93
     * @return void
94
     */
95
    public function setPrefix(string $value): void
96
    {
97
        $this->prefix = $value;
98
    }
99
100
    /**
101
     * @see $style
102
     * @param string $value
103
     * @return void
104
     */
105
    public function setStyle(string $value): void
106
    {
107
        $this->style = $value;
108
    }
109
110
    /**
111
     * Prepares either the size class (default) or the width/height if either of these is given manually.
112
     *
113
     * @return void
114
     */
115
    protected function setSvgSize(): void
116
    {
117
        parent::setSvgSize();
118
119
        $width = $this->icon->get('width');
120
        $height = $this->icon->get('height');
121
122
        if (!$width && !$height) {
123
            Html::addCssClass($this->class, $this->prefix);
124
            $widthClass = $this->icon->get('fixedWidth')
125
                ? "{$this->prefix}-fw"
126
                : "{$this->prefix}-w-" . ceil($this->svgWidth / $this->svgHeight * 16);
127
            Html::addCssClass($this->class, $widthClass);
128
        }
129
    }
130
}
131