Passed
Push — master ( 65d999...598f6d )
by Thierry
02:20
created

AjaxTrait::ajaxResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 Closure;
18
use Jaxon\App\Config\ConfigManager;
19
use Jaxon\App\I18n\Translator;
20
use Jaxon\Di\Container;
21
use Jaxon\Exception\SetupException;
22
use Jaxon\Plugin\Manager\PluginManager;
23
use Jaxon\Plugin\Package;
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\Response\ResponseInterface;
30
use Jaxon\Utils\Http\UriException;
31
use Psr\Log\LoggerInterface;
32
33
use function trim;
34
35
trait AjaxTrait
0 ignored issues
show
Coding Style introduced by
Missing doc comment for trait AjaxTrait
Loading history...
36
{
37
    /**
38
     * @var Container
39
     */
40
    protected $xContainer = null;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
41
42
    /**
43
     * @var ConfigManager
44
     */
45
    protected $xConfigManager;
46
47
    /**
48
     * @var ResponseManager
49
     */
50
    protected $xResponseManager;
51
52
    /**
53
     * @var PluginManager
54
     */
55
    protected $xPluginManager;
56
57
    /**
58
     * @var Translator
59
     */
60
    protected $xTranslator;
61
62
    /**
63
     * @return Container
64
     */
65
    public function di(): ?Container
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
66
    {
67
        return $this->xContainer;
68
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
69
70
    /**
71
     * Read the options from the file, if provided, and return the config
72
     *
73
     * @param string $sConfigFile The full path to the config file
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
74
     * @param string $sConfigSection The section of the config file to be loaded
75
     *
76
     * @return ConfigManager
77
     */
78
    public function config(string $sConfigFile = '', string $sConfigSection = ''): ConfigManager
0 ignored issues
show
Unused Code introduced by
The parameter $sConfigSection is not used and could be removed. ( Ignorable by Annotation )

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

78
    public function config(string $sConfigFile = '', /** @scrutinizer ignore-unused */ string $sConfigSection = ''): ConfigManager

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $sConfigFile is not used and could be removed. ( Ignorable by Annotation )

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

78
    public function config(/** @scrutinizer ignore-unused */ string $sConfigFile = '', string $sConfigSection = ''): ConfigManager

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
    {
80
        return $this->xConfigManager;
81
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
82
83
    /**
84
     * @return LoggerInterface
85
     */
86
    public function logger(): LoggerInterface
87
    {
88
        return $this->di()->getLogger();
89
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
90
91
    /**
92
     * Set the logger.
93
     *
94
     * @param LoggerInterface $logger
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
95
     *
96
     * @return void
97
     */
98
    public function setLogger(LoggerInterface $logger)
99
    {
100
        $this->di()->setLogger($logger);
101
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
102
103
    /**
104
     * @return Translator
105
     */
106
    public function translator(): Translator
107
    {
108
        return $this->xTranslator;
109
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
110
111
    /**
112
     * Set the value of a config option
113
     *
114
     * @param string $sName    The option name
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
115
     * @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...
116
     *
117
     * @return void
118
     */
119
    public function setOption(string $sName, $sValue)
120
    {
121
        $this->xConfigManager->setOption($sName, $sValue);
122
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
123
124
    /**
125
     * Get the value of a config option
126
     *
127
     * @param string $sName    The option name
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
128
     * @param mixed|null $xDefault    The default value, to be returned if the option is not defined
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
129
     *
130
     * @return mixed
131
     */
132
    public function getOption(string $sName, $xDefault = null)
133
    {
134
        return $this->xConfigManager->getOption($sName, $xDefault);
135
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
136
137
    /**
138
     * Check the presence of a config option
139
     *
140
     * @param string $sName    The option name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
141
     *
142
     * @return bool
143
     */
144
    public function hasOption(string $sName): bool
145
    {
146
        return $this->xConfigManager->hasOption($sName);
147
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
148
149
    /**
150
     * Get the configured character encoding
151
     *
152
     * @return string
153
     */
154
    public function getCharacterEncoding(): string
155
    {
156
        return trim($this->getOption('core.encoding', ''));
157
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
158
159
    /**
160
     * Get the content type of the HTTP response
161
     *
162
     * @return string
163
     */
164
    public function getContentType(): string
165
    {
166
        return $this->xResponseManager->getContentType();
167
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
168
169
    /**
170
     * @return Factory
171
     */
172
    public function factory(): Factory
173
    {
174
        return $this->di()->getFactory();
175
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
176
177
    /**
178
     * Get a request to a registered class
179
     *
180
     * @param string $sClassName The class name
181
     *
182
     * @return RequestFactory|null
183
     * @throws SetupException
184
     */
185
    public function request(string $sClassName = ''): ?RequestFactory
186
    {
187
        return $this->factory()->request($sClassName);
188
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
189
190
    /**
191
     * Get the Jaxon ajax response
192
     *
193
     * @return ResponseInterface
194
     */
195
    public function ajaxResponse(): ResponseInterface
196
    {
197
        return $this->xResponseManager->getResponse();
198
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
199
200
    /**
201
     * Return the javascript header code and file includes
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->getJs();
218
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
219
220
    /**
221
     * Return the CSS header code and file includes
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  the javascript code
234
     */
235
    public function css(): string
236
    {
237
        return $this->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 is dependent on the plugins
244
     * that are included and the functions and classes that are registered.
245
     *
246
     * @param bool $bIncludeJs    Also get the JS files
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 files
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 files
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 files
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->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
     * @return CallbackManager
307
     */
308
    public function callback(): CallbackManager
309
    {
310
        return $this->di()->getCallbackManager();
311
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
312
313
    /**
314
     * @param Closure $xClosure    A closure to create the session manager instance
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
315
     *
316
     * @return void
317
     */
318
    public function setSessionManager(Closure $xClosure)
319
    {
320
        $this->di()->setSessionManager($xClosure);
321
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
322
}
323