Passed
Push — main ( fef313...79e33c )
by Thierry
11:43
created

AjaxTrait::rq()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * Ajax.php
5
 *
6
 * The Jaxon library.
7
 *
8
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
9
 * @author Thierry Feuzeu <[email protected]>
10
 * @copyright 2022 Thierry Feuzeu <[email protected]>
11
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
12
 * @link https://github.com/jaxon-php/jaxon-core
13
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
14
15
namespace Jaxon\App\Traits;
16
17
use Jaxon\App\Config\ConfigManager;
18
use Jaxon\App\I18n\Translator;
19
use Jaxon\Di\Container;
20
use Jaxon\Exception\SetupException;
21
use Jaxon\Plugin\Manager\PluginManager;
22
use Jaxon\Plugin\Package;
23
use Jaxon\Plugin\Request\CallableClass\CallableRegistry;
24
use Jaxon\Plugin\ResponsePlugin;
25
use Jaxon\Request\Factory\Factory;
26
use Jaxon\Request\Factory\RequestFactory;
27
use Jaxon\Request\Handler\CallbackManager;
28
use Jaxon\Response\Manager\ResponseManager;
29
use Jaxon\Utils\Http\UriException;
30
use Psr\Log\LoggerInterface;
31
32
use function trim;
33
34
trait AjaxTrait
0 ignored issues
show
Coding Style introduced by
Missing doc comment for trait AjaxTrait
Loading history...
35
{
36
    /**
37
     * @var Container
38
     */
39
    protected $xContainer = null;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
40
41
    /**
42
     * @var ConfigManager
43
     */
44
    protected $xConfigManager;
45
46
    /**
47
     * @var ResponseManager
48
     */
49
    protected $xResponseManager;
50
51
    /**
52
     * @var PluginManager
53
     */
54
    protected $xPluginManager;
55
56
    /**
57
     * @var CallableRegistry
58
     */
59
    protected $xCallableRegistry;
60
61
    /**
62
     * @var Translator
63
     */
64
    protected $xTranslator;
65
66
    /**
67
     * @return Container
68
     */
69
    public function di(): ?Container
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
70
    {
71
        return $this->xContainer;
72
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
73
74
    /**
75
     * @return LoggerInterface
76
     */
77
    public function logger(): LoggerInterface
78
    {
79
        return $this->di()->getLogger();
80
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
81
82
    /**
83
     * @return Translator
84
     */
85
    public function translator(): Translator
86
    {
87
        return $this->xTranslator;
88
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
89
90
    /**
91
     * Set the value of a config option
92
     *
93
     * @param string $sName    The option name
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
94
     * @param mixed $sValue    The option value
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
95
     *
96
     * @return void
97
     */
98
    public function setOption(string $sName, $sValue)
99
    {
100
        $this->xConfigManager->setOption($sName, $sValue);
101
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
102
103
    /**
104
     * Get the value of a config option
105
     *
106
     * @param string $sName    The option name
107
     * @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 1 spaces after parameter name; 4 found
Loading history...
108
     *
109
     * @return mixed
110
     */
111
    public function getOption(string $sName, $xDefault = null)
112
    {
113
        return $this->xConfigManager->getOption($sName, $xDefault);
114
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
115
116
    /**
117
     * Check the presence of a config option
118
     *
119
     * @param string $sName    The option name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
120
     *
121
     * @return bool
122
     */
123
    public function hasOption(string $sName): bool
124
    {
125
        return $this->xConfigManager->hasOption($sName);
126
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
127
128
    /**
129
     * Get the configured character encoding
130
     *
131
     * @return string
132
     */
133
    public function getCharacterEncoding(): string
134
    {
135
        return trim($this->getOption('core.encoding', ''));
136
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
137
138
    /**
139
     * Get the content type of the HTTP response
140
     *
141
     * @return string
142
     */
143
    public function getContentType(): string
144
    {
145
        return $this->xResponseManager->getContentType();
146
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
147
148
    /**
149
     * Get the factory for request and parameter factories
150
     *
151
     * @return Factory
152
     */
153
    public function factory(): Factory
154
    {
155
        return $this->di()->getFactory();
156
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
157
158
    /**
159
     * Get an instance of a registered class
160
     *
161
     * @param string $sClassName The class name
162
     *
163
     * @return mixed
164
     * @throws SetupException
165
     */
166
    public function cl(string $sClassName)
167
    {
168
        $sClassName = trim($sClassName);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 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...
169
        $xCallableClass = $this->xCallableRegistry->getCallableObject($sClassName);
170
        return !$xCallableClass ? null : $xCallableClass->getRegisteredObject();
0 ignored issues
show
introduced by
$xCallableClass is of type Jaxon\Plugin\Request\CallableClass\CallableObject, thus it always evaluated to true.
Loading history...
171
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
172
173
    /**
174
     * Get the request factory to a registered class
175
     *
176
     * @param string $sClassName The class name
177
     *
178
     * @return RequestFactory|null
179
     * @throws SetupException
180
     */
181
    public function rq(string $sClassName = ''): ?RequestFactory
182
    {
183
        $sClassName = trim($sClassName);
184
        return $this->factory()->request($sClassName);
185
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
186
187
    /**
188
     * Get the request factory to a registered class
189
     *
190
     * @param string $sClassName The class name
191
     *
192
     * @return RequestFactory|null
193
     * @throws SetupException
194
     */
195
    public function request(string $sClassName = ''): ?RequestFactory
196
    {
197
        return $this->rq($sClassName);
198
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
199
200
    /**
201
     * Get the HTML tags to include Jaxon javascript files into the page.
202
     *
203
     * @return string
204
     */
205
    public function getJs(): string
206
    {
207
        return $this->di()->getCodeGenerator()->getJs();
208
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
209
210
    /**
211
     * Get the HTML tags to include Jaxon javascript files into the page.
212
     *
213
     * @return string  the javascript code
214
     */
215
    public function js(): string
216
    {
217
        return $this->di()->getCodeGenerator()->getJs();
218
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
219
220
    /**
221
     * Get the HTML tags to include Jaxon CSS code and files into the page.
222
     *
223
     * @return string
224
     */
225
    public function getCss(): string
226
    {
227
        return $this->di()->getCodeGenerator()->getCss();
228
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
229
230
    /**
231
     * Get the HTML tags to include Jaxon CSS code and files into the page.
232
     *
233
     * @return string
234
     */
235
    public function css(): string
236
    {
237
        return $this->di()->getCodeGenerator()->getCss();
238
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
239
240
    /**
241
     * Returns the js header and wrapper code to be printed into the page
242
     *
243
     * The javascript code returned by this function depends on the plugins
244
     * that are included and the functions and classes that are registered.
245
     *
246
     * @param bool $bIncludeJs    Also get the js code
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
247
     * @param bool $bIncludeCss    Also get the css code
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
248
     *
249
     * @return string
250
     * @throws UriException
251
     */
252
    public function getScript(bool $bIncludeJs = false, bool $bIncludeCss = false): string
253
    {
254
        return $this->di()->getCodeGenerator()->getScript($bIncludeJs, $bIncludeCss);
255
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
256
257
    /**
258
     * Returns the js header and wrapper code to be printed into the page
259
     *
260
     * @param bool $bIncludeJs    Also get the js code
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
261
     * @param bool $bIncludeCss    Also get the css code
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
262
     *
263
     * @return string  the javascript code
264
     * @throws UriException
265
     */
266
    public function script(bool $bIncludeJs = false, bool $bIncludeCss = false): string
267
    {
268
        return $this->di()->getCodeGenerator()->getScript($bIncludeJs, $bIncludeCss);
269
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
270
271
    /**
272
     * Determine if a call is a jaxon request or a page load request
273
     *
274
     * @return bool
275
     */
276
    public function canProcessRequest(): bool
277
    {
278
        return $this->di()->getRequestHandler()->canProcessRequest();
279
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
280
281
    /**
282
     * Get a registered response plugin
283
     *
284
     * @param string $sName    The name of the plugin
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
285
     *
286
     * @return ResponsePlugin|null
287
     */
288
    public function plugin(string $sName): ?ResponsePlugin
289
    {
290
        return $this->xPluginManager->getResponsePlugin($sName);
291
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
292
293
    /**
294
     * Get a package instance
295
     *
296
     * @param string $sClassName    The package class name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
297
     *
298
     * @return Package|null
299
     */
300
    public function package(string $sClassName): ?Package
301
    {
302
        return $this->di()->getPackageManager()->getPackage($sClassName);
303
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
304
305
    /**
306
     * Get the callback manager
307
     *
308
     * @return CallbackManager
309
     */
310
    public function callback(): CallbackManager
311
    {
312
        return $this->di()->getCallbackManager();
313
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
314
}
315