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

UploadResponse::getOutput()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
nc 1
nop 0
dl 0
loc 17
rs 10
c 6
b 0
f 0
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Jaxon\Response;
4
5
use Nyholm\Psr7\Factory\Psr17Factory;
6
use Nyholm\Psr7\Stream;
7
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
8
9
use function addslashes;
10
use function array_reduce;
11
use function json_encode;
12
13
class UploadResponse implements ResponseInterface
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class UploadResponse
Loading history...
14
{
15
    use Traits\CommandTrait;
16
17
    /**
18
     * @var Psr17Factory
19
     */
20
    protected $xPsr17Factory;
21
22
    /**
23
     * The path to the uploaded file
24
     *
25
     * @var string
26
     */
27
    private $sUploadedFile = '';
28
29
    /**
30
     * The error message
31
     *
32
     * @var string
33
     */
34
    private $sErrorMessage = '';
35
36
    /**
37
     * The debug messages
38
     *
39
     * @var array
40
     */
41
    private $aDebugMessages = [];
42
43
    /**
44
     * The constructor
45
     *
46
     * @param Psr17Factory $xPsr17Factory
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
47
     * @param string $sUploadedFile
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...
48
     * @param string $sErrorMessage
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...
49
     */
50
    public function __construct(Psr17Factory $xPsr17Factory, string $sUploadedFile, string $sErrorMessage = '')
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
51
    {
52
        $this->xPsr17Factory = $xPsr17Factory;
53
        $this->sUploadedFile = $sUploadedFile;
54
        $this->sErrorMessage = $sErrorMessage;
55
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
56
57
    /**
58
     * @inheritDoc
59
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
60
    public function getContentType(): string
61
    {
62
        return 'text/html';
63
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
64
65
    /**
66
     * Get the path to the uploaded file
67
     *
68
     * @return string
69
     */
70
    public function getUploadedFile(): string
71
    {
72
        return $this->sUploadedFile;
73
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
74
75
    /**
76
     * Get the error message
77
     *
78
     * @return string
79
     */
80
    public function getErrorMessage(): string
81
    {
82
        return $this->sErrorMessage;
83
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
84
85
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $sMessage should have a doc-comment as per coding-style.
Loading history...
86
     * @inheritDoc
87
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
88
    public function debug(string $sMessage): ResponseInterface
89
    {
90
        $this->aDebugMessages[] = $sMessage;
91
        return $this;
92
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
93
94
    /**
95
     * @inheritDoc
96
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
97
    public function getOutput(): string
98
    {
99
        $sResult = json_encode(($this->sUploadedFile) ?
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 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...
Coding Style introduced by
Expected 1 space after "?"; newline found
Loading history...
100
            ['code' => 'success', 'upl' => $this->sUploadedFile] :
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ":"; newline found
Loading history...
101
            ['code' => 'error', 'msg' => $this->sErrorMessage]) . ';';
102
        $sConsoleLog = array_reduce($this->aDebugMessages, function($sJsLog, $sMessage) {
103
            return "$sJsLog\n\t" . 'console.log("' . addslashes($sMessage) . '");';
104
        }, '');
105
106
        return '
107
<!DOCTYPE html>
108
<html>
109
<body>
110
<h1>HTTP Upload for Jaxon</h1>
111
</body>
112
<script>
113
    res = ' . $sResult . $sConsoleLog . '
114
</script>
115
</html>';
116
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
117
118
    /**
119
     * Convert this response to a PSR7 response object
120
     *
121
     * @return PsrResponseInterface
122
     */
123
    public function toPsr(): PsrResponseInterface
124
    {
125
        return $this->xPsr17Factory->createResponse(($this->sUploadedFile) ? 200 : 500)
126
            ->withHeader('content-type', $this->getContentType())
0 ignored issues
show
Coding Style introduced by
Space found before object operator
Loading history...
127
            ->withBody(Stream::create($this->getOutput()));
0 ignored issues
show
Coding Style introduced by
Space found before object operator
Loading history...
128
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
129
}
130