Passed
Push — main ( 320ab1...caada2 )
by Thierry
07:18 queued 04:09
created

UploadResponse::getErrorMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Jaxon\Upload;
4
5
use Jaxon\Plugin\Manager\PluginManager;
6
use Jaxon\Plugin\Response\Psr\PsrPlugin;
0 ignored issues
show
Bug introduced by
The type Jaxon\Plugin\Response\Psr\PsrPlugin was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Jaxon\Response\AbstractResponse;
0 ignored issues
show
Bug introduced by
The type Jaxon\Response\AbstractResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Jaxon\Response\ResponseManager;
0 ignored issues
show
Bug introduced by
The type Jaxon\Response\ResponseManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Psr\Http\Message\ResponseInterface;
10
11
use function addslashes;
12
use function array_reduce;
13
use function json_encode;
14
15
class UploadResponse extends AbstractResponse
16
{
17
    /**
18
     * The path to the uploaded file
19
     *
20
     * @var string
21
     */
22
    private $sUploadedFile = '';
23
24
    /**
25
     * The constructor
26
     *
27
     * @param ResponseManager $xManager
28
     * @param PluginManager $xPluginManager
29
     * @param string $sUploadedFile
30
     */
31
    public function __construct(ResponseManager $xManager, PluginManager $xPluginManager,
32
        string $sUploadedFile = '')
33
    {
34
        parent::__construct($xManager, $xPluginManager);
35
        $this->sUploadedFile = $sUploadedFile;
36
    }
37
38
    /**
39
     * @inheritDoc
40
     */
41
    public function getContentType(): string
42
    {
43
        return 'text/html';
44
    }
45
46
    /**
47
     * Get the path to the uploaded file
48
     *
49
     * @return string
50
     */
51
    public function getUploadedFile(): string
52
    {
53
        return $this->sUploadedFile;
54
    }
55
56
    /**
57
     * @inheritDoc
58
     */
59
    public function getOutput(): string
60
    {
61
        $aResult = ($this->sUploadedFile) ?
62
            ['code' => 'success', 'upl' => $this->sUploadedFile] :
63
            ['code' => 'error', 'msg' => $this->getErrorMessage()];
64
        $aMessages = $this->xManager->getDebugMessages();
65
        $sMessages = array_reduce($aMessages, function($sJsLog, $sMessage) {
66
            return "$sJsLog\n\t" . 'console.log("' . addslashes($sMessage) . '");';
67
        }, '');
68
69
        return '
70
<!DOCTYPE html>
71
<html>
72
<body>
73
<h1>HTTP Upload for Jaxon</h1>
74
</body>
75
<script>
76
    res = ' . json_encode($aResult) . ';
77
    // Debug messages ' . $sMessages . '
78
</script>
79
</html>';
80
    }
81
82
    /**
83
     * Convert this response to a PSR7 response object
84
     *
85
     * @return ResponseInterface
86
     */
87
    public function toPsr(): ResponseInterface
88
    {
89
        /** @var PsrPlugin */
90
        $xPlugin = $this->plugin('psr');
91
        return $xPlugin->upload(($this->sUploadedFile) ? 200 : 500);
92
    }
93
}
94