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

UploadResponse   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 86
c 0
b 0
f 0
rs 10
wmc 6
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getContentType() 0 4 1
A setUploadedFile() 0 4 1
A setErrorMessage() 0 4 1
A debug() 0 5 1
A getOutput() 0 18 2
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