Passed
Push — master ( b86a9b...818b3e )
by Thierry
02:35
created

DialogLibraryHelper::getOptionNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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