Passed
Push — main ( 2d573b...fc5697 )
by Thierry
08:37
created

UploadResponse::addPluginCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
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 3
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Jaxon\Upload;
4
5
use Jaxon\Plugin\ResponsePlugin;
6
use Jaxon\Response\ResponseInterface;
7
use Nyholm\Psr7\Factory\Psr17Factory;
8
use Nyholm\Psr7\Stream;
9
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
10
11
use function addslashes;
12
use function array_reduce;
13
use function json_encode;
14
15
class UploadResponse implements ResponseInterface
16
{
17
    use \Jaxon\Response\Traits\CommandTrait;
18
    use \Jaxon\Response\Traits\DomTrait;
19
    use \Jaxon\Response\Traits\JsTrait;
0 ignored issues
show
Bug introduced by
The trait Jaxon\Response\Traits\JsTrait requires the property $xPluginManager which is not provided by Jaxon\Upload\UploadResponse.
Loading history...
20
21
    /**
22
     * @var Psr17Factory
23
     */
24
    protected $xPsr17Factory;
25
26
    /**
27
     * The path to the uploaded file
28
     *
29
     * @var string
30
     */
31
    private $sUploadedFile = '';
32
33
    /**
34
     * The error message
35
     *
36
     * @var string
37
     */
38
    private $sErrorMessage = '';
39
40
    /**
41
     * The debug messages
42
     *
43
     * @var array
44
     */
45
    private $aDebugMessages = [];
46
47
    /**
48
     * The constructor
49
     *
50
     * @param Psr17Factory $xPsr17Factory
51
     * @param string $sUploadedFile
52
     * @param string $sErrorMessage
53
     */
54
    public function __construct(Psr17Factory $xPsr17Factory, string $sUploadedFile, string $sErrorMessage = '')
55
    {
56
        $this->xPsr17Factory = $xPsr17Factory;
57
        $this->sUploadedFile = $sUploadedFile;
58
        $this->sErrorMessage = $sErrorMessage;
59
    }
60
61
    /**
62
     * @inheritDoc
63
     */
64
    public function getContentType(): string
65
    {
66
        return 'text/html';
67
    }
68
69
    /**
70
     * Get the path to the uploaded file
71
     *
72
     * @return string
73
     */
74
    public function getUploadedFile(): string
75
    {
76
        return $this->sUploadedFile;
77
    }
78
79
    /**
80
     * Get the error message
81
     *
82
     * @return string
83
     */
84
    public function getErrorMessage(): string
85
    {
86
        return $this->sErrorMessage;
87
    }
88
89
    /**
90
     * @inheritDoc
91
     */
92
    public function debug(string $sMessage): ResponseInterface
93
    {
94
        $this->aDebugMessages[] = $sMessage;
95
        return $this;
96
    }
97
98
    /**
99
     * @inheritDoc
100
     */
101
    public function getOutput(): string
102
    {
103
        $sResult = json_encode(($this->sUploadedFile) ?
104
            ['code' => 'success', 'upl' => $this->sUploadedFile] :
105
            ['code' => 'error', 'msg' => $this->sErrorMessage]) . ';';
106
        $sConsoleLog = array_reduce($this->aDebugMessages, function($sJsLog, $sMessage) {
107
            return "$sJsLog\n\t" . 'console.log("' . addslashes($sMessage) . '");';
108
        }, '');
109
110
        return '
111
<!DOCTYPE html>
112
<html>
113
<body>
114
<h1>HTTP Upload for Jaxon</h1>
115
</body>
116
<script>
117
    res = ' . $sResult . $sConsoleLog . '
118
</script>
119
</html>';
120
    }
121
122
    /**
123
     * Convert this response to a PSR7 response object
124
     *
125
     * @return PsrResponseInterface
126
     */
127
    public function toPsr(): PsrResponseInterface
128
    {
129
        return $this->xPsr17Factory->createResponse(($this->sUploadedFile) ? 200 : 500)
130
            ->withHeader('content-type', $this->getContentType())
131
            ->withBody(Stream::create($this->getOutput()));
132
    }
133
134
    /**
135
     * Empty method, just to have the ResponseInterface methods implemented.
136
     *
137
     * @param array $aAttributes
138
     * @param mixed $mData
139
     *
140
     * @return ResponseInterface
141
     */
142
    public function addCommand(array $aAttributes, $mData): ResponseInterface
0 ignored issues
show
Unused Code introduced by
The parameter $aAttributes 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

142
    public function addCommand(/** @scrutinizer ignore-unused */ array $aAttributes, $mData): ResponseInterface

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 $mData 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

142
    public function addCommand(array $aAttributes, /** @scrutinizer ignore-unused */ $mData): ResponseInterface

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...
143
    {
144
        return $this;
145
    }
146
147
    /**
148
     * Empty method, just to have the ResponseInterface methods implemented.
149
     *
150
     * @param string $sName
151
     * @param array $aAttributes
152
     * @param mixed $mData
153
     * @param bool $bRemoveEmpty
154
     *
155
     * @return ResponseInterface
156
     */
157
    protected function _addCommand(string $sName, array $aAttributes,
0 ignored issues
show
Unused Code introduced by
The parameter $aAttributes 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

157
    protected function _addCommand(string $sName, /** @scrutinizer ignore-unused */ array $aAttributes,

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 $sName 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

157
    protected function _addCommand(/** @scrutinizer ignore-unused */ string $sName, array $aAttributes,

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...
158
        $mData, bool $bRemoveEmpty = false): ResponseInterface
0 ignored issues
show
Unused Code introduced by
The parameter $bRemoveEmpty 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

158
        $mData, /** @scrutinizer ignore-unused */ bool $bRemoveEmpty = false): ResponseInterface

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 $mData 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

158
        /** @scrutinizer ignore-unused */ $mData, bool $bRemoveEmpty = false): ResponseInterface

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...
159
    {
160
        return $this;
161
    }
162
163
    /**
164
     * Empty method, just to have the ResponseInterface methods implemented.
165
     *
166
     * @param ResponsePlugin $xPlugin
167
     * @param array $aAttributes
168
     * @param mixed $mData
169
     *
170
     * @return ResponseInterface
171
     */
172
    public function addPluginCommand(ResponsePlugin $xPlugin, array $aAttributes, $mData): ResponseInterface
0 ignored issues
show
Unused Code introduced by
The parameter $xPlugin 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

172
    public function addPluginCommand(/** @scrutinizer ignore-unused */ ResponsePlugin $xPlugin, array $aAttributes, $mData): ResponseInterface

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 $aAttributes 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

172
    public function addPluginCommand(ResponsePlugin $xPlugin, /** @scrutinizer ignore-unused */ array $aAttributes, $mData): ResponseInterface

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 $mData 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

172
    public function addPluginCommand(ResponsePlugin $xPlugin, array $aAttributes, /** @scrutinizer ignore-unused */ $mData): ResponseInterface

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...
173
    {
174
        return $this;
175
    }
176
}
177