Completed
Push — master ( 8e87e9...3db3b2 )
by Thierry
01:29
created

UploadResponse::appendResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Jaxon\Response;
4
5
class UploadResponse extends AbstractResponse
6
{
7
    /**
8
     * The path to the uploaded file
9
     *
10
     * @var string
11
     */
12
    private $sUploadedFile = '';
13
14
    /**
15
     * The error message
16
     *
17
     * @var string
18
     */
19
    private $sErrorMessage = '';
20
21
    /**
22
     * The debug messages
23
     *
24
     * @var array
25
     */
26
    private $aDebugMessages = [];
27
28
    /**
29
     * Get the content type, which is always set to 'text/json'
30
     *
31
     * @return string
32
     */
33
    public function getContentType()
34
    {
35
        return 'text/html';
36
    }
37
38
    /**
39
     * Set the path to the uploaded file
40
     */
41
    public function setUploadedFile($sUploadedFile)
42
    {
43
        $this->sUploadedFile = $sUploadedFile;
44
    }
45
46
    /**
47
     * Set the error message
48
     */
49
    public function setErrorMessage($sErrorMessage)
50
    {
51
        $this->sErrorMessage = $sErrorMessage;
52
    }
53
54
    /**
55
     * Add a command to display a debug message to the user
56
     *
57
     * @param string        $sMessage            The message to be displayed
58
     *
59
     * @return AbstractResponse
60
     */
61
    public function debug($sMessage)
62
    {
63
        $this->aDebugMessages[] = $sMessage;
64
        return $this;
65
    }
66
67
    /**
68
     * Return the output, generated from the commands added to the response, that will be sent to the browser
69
     *
70
     * @return string
71
     */
72
    public function getOutput()
73
    {
74
        $aResponse = ($this->sUploadedFile) ?
75
            ['code' => 'success', 'upl' => $this->sUploadedFile] :
76
            ['code' => 'error', 'msg' => $this->sErrorMessage];
77
78
        $sConsoleLog = '';
79
        array_walk($this->aDebugMessages, function ($sMessage) use (&$sConsoleLog) {
80
            $sConsoleLog .= '
81
    console.log("' . addslashes($sMessage) . '");';
82
        });
83
84
        return '
85
<script>
86
    var res = ' . json_encode($aResponse) . ';' . $sConsoleLog . '
87
</script>
88
';
89
    }
90
}
91