Passed
Push — master ( 6f7a23...2cf817 )
by Thierry
02:29
created

PsrRequestHandler::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 16
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * PsrRequestHandler.php
5
 *
6
 * A Psr7 Jaxon ajax request handler.
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\Request\Handler\Psr;
16
17
use Jaxon\Di\Container;
18
use Jaxon\Exception\RequestException;
19
use Jaxon\Request\Handler\RequestHandler;
20
use Jaxon\Response\Manager\ResponseManager;
21
use Jaxon\Utils\Translation\Translator;
22
use Psr\Http\Message\ResponseInterface;
23
use Psr\Http\Message\ServerRequestInterface;
24
use Psr\Http\Server\RequestHandlerInterface;
25
26
class PsrRequestHandler implements RequestHandlerInterface
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class PsrRequestHandler
Loading history...
27
{
28
    /**
29
     * @var Container
30
     */
31
    private $di;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
32
33
    /**
34
     * @var RequestHandler
35
     */
36
    private $xRequestHandler;
37
38
    /**
39
     * @var ResponseManager
40
     */
41
    private $xResponseManager;
42
43
    /**
44
     * @var Translator
45
     */
46
    private $xTranslator;
47
48
    /**
49
     * The constructor
50
     *
51
     * @param Container $di
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
52
     * @param RequestHandler $xRequestHandler
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
53
     * @param ResponseManager $xResponseManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
54
     * @param Translator $xTranslator
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
55
     */
56
    public function __construct(Container $di, RequestHandler $xRequestHandler,
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
57
        ResponseManager $xResponseManager, Translator $xTranslator)
58
    {
59
        $this->di = $di;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 15 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...
60
        $this->xRequestHandler = $xRequestHandler;
61
        $this->xResponseManager = $xResponseManager;
62
        $this->xTranslator = $xTranslator;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 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...
63
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
64
65
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $request should have a doc-comment as per coding-style.
Loading history...
66
     * @inheritDoc
67
     * @throws RequestException
68
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
69
    public function handle(ServerRequestInterface $request): ResponseInterface
70
    {
71
        // Save the request in the container. This will override the default request,
72
        // and the other classes will get this request from there.
73
        $this->di->val(ServerRequestInterface::class, $request);
74
75
        if(!$this->xRequestHandler->canProcessRequest())
76
        {
77
            // Unable to find a plugin to process the request
78
            throw new RequestException($this->xTranslator->trans('errors.request.plugin'));
79
        }
80
81
        // Process the request
82
        $this->xRequestHandler->processRequest();
83
        // Return a Psr7 response
84
        return $this->xResponseManager->getResponse()->toPsr();
85
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
86
}
87