Passed
Push — main ( b4a399...1c46ee )
by Thierry
03:10 queued 01:37
created

LibraryHelper::getCssCode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Jaxon\Dialogs\Dialog;
4
5
use Jaxon\App\Config\ConfigManager;
6
use Jaxon\App\Dialog\Library\LibraryInterface;
7
use Jaxon\Utils\Template\TemplateEngine;
8
9
use function is_array;
10
use function rtrim;
11
use function trim;
12
13
class LibraryHelper
14
{
15
    /**
16
     * The name of the library
17
     *
18
     * @var string
19
     */
20
    protected $sName = '';
21
22
    /**
23
     * The URI where to get the library files from
24
     *
25
     * @var string
26
     */
27
    protected $sUri = '';
28
29
    /**
30
     * The constructor
31
     *
32
     * @param LibraryInterface $xDialogLibrary
33
     * @param ConfigManager $xConfigManager
34
     * @param TemplateEngine $xTemplateEngine
35
     */
36
    public function __construct(LibraryInterface $xDialogLibrary,
37
        private ConfigManager $xConfigManager, private TemplateEngine $xTemplateEngine)
38
    {
39
        // Set the library name
40
        $this->sName = $xDialogLibrary->getName();
41
        // Set the default URI.
42
        $sDefaultUri = $xConfigManager->getAppOption('dialogs.lib.uri', $xDialogLibrary->getUri());
43
        // Set the library URI.
44
        $this->sUri = rtrim($this->getOption('uri', $sDefaultUri), '/');
45
    }
46
47
    /**
48
     * Get the value of a config option
49
     *
50
     * @param string $sOptionName The option name
51
     * @param mixed $xDefault The default value, to be returned if the option is not defined
52
     *
53
     * @return mixed
54
     */
55
    public function getOption(string $sOptionName, $xDefault = null)
56
    {
57
        $sOptionName = "dialogs.{$this->sName}.$sOptionName";
58
        return $this->xConfigManager->getAppOption($sOptionName, $xDefault);
59
    }
60
61
    /**
62
     * Check the presence of a config option
63
     *
64
     * @param string $sOptionName The option name
65
     *
66
     * @return bool
67
     */
68
    public function hasOption(string $sOptionName): bool
69
    {
70
        return $this->xConfigManager->hasAppOption("dialogs.{$this->sName}.$sOptionName");
71
    }
72
73
    /**
74
     * Get the options of the js library
75
     *
76
     * @return array
77
     */
78
    public function getJsOptions(): array
79
    {
80
        $xOptions = $this->xConfigManager->getAppOption("dialogs.{$this->sName}.options", []);
81
        return is_array($xOptions) ? $xOptions : [];
82
    }
83
84
    /**
85
     * @param string $sOption The assets option name
86
     * @param string $sFile The javascript file name
87
     *
88
     * @return string|null
89
     */
90
    private function getAssetUri(string $sOption, string $sFile): ?string
91
    {
92
        return !$this->hasOption($sOption) ? "{$this->sUri}/$sFile" :
93
            (trim($this->getOption($sOption)) ?: null);
0 ignored issues
show
Bug introduced by
It seems like $this->getOption($sOption) can also be of type null; however, parameter $string of trim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

93
            (trim(/** @scrutinizer ignore-type */ $this->getOption($sOption)) ?: null);
Loading history...
94
    }
95
96
    /**
97
     * Get the javascript HTML header code
98
     *
99
     * @param string $sFile The javascript file name
100
     *
101
     * @return string
102
     */
103
    public function getJsCode(string $sFile): string
104
    {
105
        // If this 'assets.js' option is defined and evaluates to false, then the asset is not displayed.
106
        $sUri = $this->getAssetUri('assets.js', $sFile);
107
        return !$sUri ? '' : '<script type="text/javascript" src="' . $sUri . '"></script>';
108
    }
109
110
    /**
111
     * Get the CSS HTML header code
112
     *
113
     * @param string $sFile The CSS file name
114
     *
115
     * @return string
116
     */
117
    public function getCssCode(string $sFile): string
118
    {
119
        // If this 'assets.css' option is defined and evaluates to false, then the asset is not displayed.
120
        $sUri = $this->getAssetUri('assets.css', $sFile);
121
        return !$sUri ? '' : '<link rel="stylesheet" href="' . $sUri . '" />';
122
    }
123
124
    /**
125
     * Render a template
126
     *
127
     * @param string $sTemplate The name of template to be rendered
128
     * @param array $aVars The template vars
129
     *
130
     * @return string
131
     */
132
    public function render(string $sTemplate, array $aVars = []): string
133
    {
134
        return $this->xTemplateEngine->render("jaxon::dialogs::$sTemplate", $aVars);
135
    }
136
}
137