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

PsrAjaxMiddleware::process()   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 2
1
<?php
2
3
/**
4
 * PsrAjaxMiddleware.php
5
 *
6
 * A Psr7 middleware to process Jaxon ajax requests.
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 Psr\Http\Message\ResponseInterface;
22
use Psr\Http\Message\ServerRequestInterface;
23
use Psr\Http\Server\MiddlewareInterface;
24
use Psr\Http\Server\RequestHandlerInterface;
25
26
class PsrAjaxMiddleware implements MiddlewareInterface
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class PsrAjaxMiddleware
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
     * The constructor
45
     *
46
     * @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...
47
     * @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...
48
     * @param ResponseManager $xResponseManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
49
     */
50
    public function __construct(Container $di, RequestHandler $xRequestHandler, ResponseManager $xResponseManager)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
51
    {
52
        $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...
53
        $this->xRequestHandler = $xRequestHandler;
54
        $this->xResponseManager = $xResponseManager;
55
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
56
57
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $request should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $handler should have a doc-comment as per coding-style.
Loading history...
58
     * @inheritDoc
59
     * @throws RequestException
60
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
61
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
62
    {
63
        // Save the request in the container. This will override the default request,
64
        // and the other classes will get this request from there.
65
        $this->di->val(ServerRequestInterface::class, $request);
66
67
        if(!$this->xRequestHandler->canProcessRequest())
68
        {
69
            // Unable to find a plugin to process the request
70
            return $handler->handle($request);
71
        }
72
73
        // Process the request
74
        $this->xRequestHandler->processRequest();
75
        // Return a Psr7 response
76
        return $this->xResponseManager->getResponse()->toPsr();
77
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
78
}
79