PsrPlugin::getHash()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
/**
4
 * PaginatorPlugin.php
5
 *
6
 * A plugin to convert a response content to PSR.
7
 *
8
 * @package jaxon-core
9
 * @copyright 2024 Thierry Feuzeu
10
 * @license https://opensource.org/licenses/MIT MIT License
11
 * @link https://github.com/jaxon-php/jaxon-core
12
 */
13
14
namespace Jaxon\Plugin\Response\Psr;
15
16
use Jaxon\Plugin\AbstractResponsePlugin;
17
use Nyholm\Psr7\Factory\Psr17Factory;
18
use Nyholm\Psr7\Stream;
19
use Psr\Http\Message\ResponseInterface;
20
use Psr\Http\Message\ServerRequestInterface as RequestInterface;
21
22
use function gmdate;
23
24
class PsrPlugin extends AbstractResponsePlugin
25
{
26
    /**
27
     * @const The plugin name
28
     */
29
    const NAME = 'psr';
30
31
    /**
32
     * @var Psr17Factory
33
     */
34
    private $xPsr17Factory;
35
36
    /**
37
     * @var RequestInterface
38
     */
39
    protected $xRequest;
40
41
    /**
42
     * The class constructor
43
     *
44
     * @param Psr17Factory $xPsr17Factory
45
     * @param RequestInterface $xRequest
46
     */
47
    public function __construct(Psr17Factory $xPsr17Factory, RequestInterface $xRequest)
48
    {
49
        $this->xPsr17Factory = $xPsr17Factory;
50
        $this->xRequest = $xRequest;
51
    }
52
53
    /**
54
     * @inheritDoc
55
     */
56
    public function getName(): string
57
    {
58
        return self::NAME;
59
    }
60
61
    /**
62
     * @inheritDoc
63
     */
64
    public function getHash(): string
65
    {
66
        // Use the version number as hash
67
        return '5.0.0';
68
    }
69
70
    /**
71
     * Convert an ajax response to a PSR7 response object
72
     *
73
     * @return ResponseInterface
74
     */
75
    public function ajax(): ResponseInterface
76
    {
77
        $xPsrResponse = $this->xPsr17Factory->createResponse(200);
78
        if($this->xRequest->getMethod() === 'GET')
79
        {
80
            $xPsrResponse = $xPsrResponse
81
                ->withHeader('Expires', 'Mon, 26 Jul 1997 05:00:00 GMT')
82
                ->withHeader('Last-Modified', gmdate("D, d M Y H:i:s") . ' GMT')
83
                ->withHeader('Cache-Control', 'no-cache, must-revalidate')
84
                ->withHeader('Pragma', 'no-cache');
85
        }
86
        return $xPsrResponse
87
            ->withHeader('content-type', $this->response()->getContentType())
88
            ->withBody(Stream::create($this->response()->getOutput()));
89
    }
90
91
    /**
92
     * Convert an upload response to a PSR7 response object
93
     *
94
     * @param int $nHttpCode The response HTTP code
95
     *
96
     * @return ResponseInterface
97
     */
98
    public function upload(int $nHttpCode): ResponseInterface
99
    {
100
        return $this->xPsr17Factory->createResponse($nHttpCode)
101
            ->withHeader('content-type', $this->response()->getContentType())
102
            ->withBody(Stream::create($this->response()->getOutput()));
103
    }
104
}
105