Passed
Push — master ( b34d93...aea763 )
by Thierry
03:02 queued 24s
created

DialogLibraryHelper::getOptionScript()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 23
rs 9.2222
cc 6
nc 6
nop 3
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Jaxon\Ui\Dialog\Library;
4
5
use Jaxon\Config\ConfigManager;
6
use Jaxon\Utils\Template\TemplateEngine;
7
8
use function array_merge;
9
use function is_string;
10
use function is_bool;
11
use function is_numeric;
12
use function json_encode;
13
use function rtrim;
14
use function str_repeat;
15
use function trim;
16
17
class DialogLibraryHelper
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class DialogLibraryHelper
Loading history...
18
{
19
    /**
20
     * @var ConfigManager
21
     */
22
    protected $xConfigManager;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
23
24
    /**
25
     * The Jaxon template engine
26
     *
27
     * @var TemplateEngine
28
     */
29
    protected $xTemplateEngine;
30
31
    /**
32
     * The name of the library
33
     *
34
     * @var string
35
     */
36
    protected $sName = '';
37
38
    /**
39
     * The URI where to get the library files from
40
     *
41
     * @var string
42
     */
43
    protected $sUri = '';
44
45
    /**
46
     * The subdir of the JS and CSS files in the CDN
47
     *
48
     * @var string
49
     */
50
    protected $sSubDir = '';
51
52
    /**
53
     * The default version of the plugin library
54
     *
55
     * @var string
56
     */
57
    protected $sVersion = '';
58
59
    /**
60
     * The constructor
61
     *
62
     * @param AbstractDialogLibrary $xDialogLibrary
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
63
     * @param ConfigManager $xConfigManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 9 spaces after parameter type; 1 found
Loading history...
64
     * @param TemplateEngine $xTemplateEngine
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
65
     */
66
    public function __construct(AbstractDialogLibrary $xDialogLibrary,
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
67
        ConfigManager $xConfigManager, TemplateEngine $xTemplateEngine)
68
    {
69
        $this->xConfigManager = $xConfigManager;
70
        $this->xTemplateEngine = $xTemplateEngine;
71
72
        // Set the library name
73
        $this->sName = $xDialogLibrary->getName();
74
        // Set the default URI.
75
        $sDefaultUri = $xConfigManager->getOption('dialogs.lib.uri', $xDialogLibrary->getUri());
76
        // Set the library URI.
77
        $this->sUri = rtrim($this->getOption('uri', $sDefaultUri), '/');
78
        // Set the subdir
79
        $this->sSubDir = trim($this->getOption('subdir', $xDialogLibrary->getSubDir()), '/');
80
        // Set the version number
81
        $this->sVersion = trim($this->getOption('version', $xDialogLibrary->getVersion()), '/');
82
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
83
84
    /**
85
     * Get the value of a config option
86
     *
87
     * @param string $sOptionName The option name
88
     * @param mixed $xDefault The default value, to be returned if the option is not defined
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
89
     *
90
     * @return mixed
91
     */
92
    public function getOption(string $sOptionName, $xDefault = null)
93
    {
94
        $sOptionName = 'dialogs.' . $this->sName . '.' . $sOptionName;
95
        return $this->xConfigManager->getOption($sOptionName, $xDefault);
96
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
97
98
    /**
99
     * Check the presence of a config option
100
     *
101
     * @param string $sOptionName The option name
102
     *
103
     * @return bool
104
     */
105
    public function hasOption(string $sOptionName): bool
106
    {
107
        $sOptionName = 'dialogs.' . $this->sName . '.' . $sOptionName;
108
        return $this->xConfigManager->hasOption($sOptionName);
109
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
110
111
    /**
112
     * Get the names of the options matching a given prefix
113
     *
114
     * @param string $sPrefix The prefix to match
115
     *
116
     * @return array
117
     */
118
    public function getOptionNames(string $sPrefix): array
119
    {
120
        // The options names are relative to the plugin in Dialogs configuration
121
        return $this->xConfigManager->getOptionNames('dialogs.' . $this->sName . '.' . $sPrefix);
122
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
123
124
    /**
125
     * Get the names of the options matching a given prefix
126
     *
127
     * @param string $sVarPrefix
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
128
     * @param string $sKeyPrefix
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
129
     * @param int $nSpaces
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
130
     *
131
     * @return string
132
     */
133
    public function getOptionScript(string $sVarPrefix, string $sKeyPrefix, int $nSpaces = 4): string
134
    {
135
        $aOptions = $this->getOptionNames($sKeyPrefix);
136
        $sSpaces = str_repeat(' ', $nSpaces);
137
        $sScript = '';
138
        foreach($aOptions as $sShortName => $sFullName)
139
        {
140
            $value = $this->xConfigManager->getOption($sFullName);
141
            if(is_string($value))
142
            {
143
                $value = "'$value'";
144
            }
145
            elseif(is_bool($value))
146
            {
147
                $value = ($value ? 'true' : 'false');
148
            }
149
            elseif(!is_numeric($value))
150
            {
151
                $value = json_encode($value);
152
            }
153
            $sScript .= "\n" . $sSpaces . $sVarPrefix . $sShortName . ' = ' . $value . ';';
154
        }
155
        return $sScript;
156
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
157
158
    /**
159
     * Get the text of the "Yes" button for confirm dialog
160
     *
161
     * @return string
162
     */
163
    public function getQuestionTitle(): string
164
    {
165
        return $this->xConfigManager->getOption('dialogs.question.title', '');
166
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
167
168
    /**
169
     * Get the javascript HTML header code
170
     *
171
     * @param string $sFile The javascript file name
172
     *
173
     * @return string
174
     */
175
    public function getJsCode(string $sFile): string
176
    {
177
        return '<script type="text/javascript" src="' . $this->sUri . '/' .
178
            $this->sSubDir . '/' . $this->sVersion . '/' . $sFile . '"></script>';
179
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
180
181
    /**
182
     * Get the CSS HTML header code
183
     *
184
     * @param string $sFile The CSS file name
185
     *
186
     * @return string
187
     */
188
    public function getCssCode(string $sFile): string
189
    {
190
        return '<link rel="stylesheet" href="' . $this->sUri . '/' .
191
            $this->sSubDir . '/' . $this->sVersion . '/' . $sFile . '" />';
192
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
193
194
    /**
195
     * Render a template
196
     *
197
     * @param string $sTemplate The name of template to be rendered
198
     * @param array $aVars The template vars
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
199
     *
200
     * @return string
201
     */
202
    public function render(string $sTemplate, array $aVars = []): string
203
    {
204
        // Is the library the default for alert messages?
205
        $isDefaultForMessage = ($this->sName == $this->xConfigManager->getOption('dialogs.default.message'));
206
        // Is the library the default for confirm questions?
207
        $isDefaultForQuestion = ($this->sName == $this->xConfigManager->getOption('dialogs.default.question'));
208
        $aLocalVars = [
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
209
            'yes' => $this->xConfigManager->getOption('dialogs.question.yes', 'Yes'),
210
            'no' =>  $this->xConfigManager->getOption('dialogs.question.no', 'No'),
0 ignored issues
show
Coding Style introduced by
Expected 1 space after "=>"; 2 found
Loading history...
211
            'defaultForMessage' => $isDefaultForMessage,
212
            'defaultForQuestion' => $isDefaultForQuestion
213
        ];
214
        return $this->xTemplateEngine->render('jaxon::dialogs::' . $sTemplate, array_merge($aLocalVars, $aVars));
215
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
216
}
217