Completed
Push — master ( f42a52...0a9499 )
by Thierry
02:50 queued 01:16
created

UploadResponse   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 113
Duplicated Lines 3.54 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
dl 4
loc 113
rs 10
c 0
b 0
f 0
wmc 11
lcom 2
cbo 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getContentType() 0 4 1
A setUploadedFile() 0 4 1
A setErrorMessage() 0 4 1
A getCharacterEncoding() 0 4 1
A debug() 0 4 1
A sendHeaders() 4 11 3
A getOutput() 0 7 2
A printOutput() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Jaxon\Response;
4
5
use Jaxon\Contracts\Response as ResponseContract;
6
7
class UploadResponse implements ResponseContract
8
{
9
    use \Jaxon\Features\Config;
10
11
    /**
12
     * The response type
13
     *
14
     * @var string
15
     */
16
    private $sContentType = 'text/html';
17
18
    /**
19
     * The path to the uploaded file
20
     *
21
     * @var string
22
     */
23
    private $sUploadedFile = '';
24
25
    /**
26
     * The error message
27
     *
28
     * @var string
29
     */
30
    private $sErrorMessage = '';
31
32
    /**
33
     * Get the content type, which is always set to 'text/json'
34
     *
35
     * @return string
36
     */
37
    public function getContentType()
38
    {
39
        return $this->sContentType;
40
    }
41
42
    /**
43
     * Set the path to the uploaded file
44
     */
45
    public function setUploadedFile($sUploadedFile)
46
    {
47
        $this->sUploadedFile = $sUploadedFile;
48
    }
49
50
    /**
51
     * Set the error message
52
     */
53
    public function setErrorMessage($sErrorMessage)
54
    {
55
        $this->sErrorMessage = $sErrorMessage;
56
    }
57
58
    /**
59
     * Get the configured character encoding
60
     *
61
     * @return string
62
     */
63
    public function getCharacterEncoding()
64
    {
65
        return $this->getOption('core.encoding');
66
    }
67
68
    /**
69
     * Add a command to display a debug message to the user
70
     *
71
     * @param string        $sMessage            The message to be displayed
72
     *
73
     * @return \Jaxon\Plugin\Response
74
     */
75
    public function debug($sMessage)
76
    {
77
        // Todo: send this message to the console log.
78
    }
79
80
    /**
81
     * Used internally to generate the response headers
82
     *
83
     * @return void
84
     */
85
    public function sendHeaders()
86
    {
87
        $sCharacterSet = '';
88
        $sCharacterEncoding = trim($this->getOption('core.encoding'));
89 View Code Duplication
        if(($sCharacterEncoding) && strlen($sCharacterEncoding) > 0)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
        {
91
            $sCharacterSet = '; charset="' . trim($sCharacterEncoding) . '"';
92
        }
93
94
        header('content-type: ' . $this->sContentType . ' ' . $sCharacterSet);
95
    }
96
97
    /**
98
     * Return the output, generated from the commands added to the response, that will be sent to the browser
99
     *
100
     * @return string
101
     */
102
    public function getOutput()
103
    {
104
        $aResponse = ($this->sUploadedFile) ?
105
            ['code' => 'success', 'upl' => $this->sUploadedFile] :
106
            ['code' => 'error', 'msg' => $this->sErrorMessage];
107
        return '<script>var res = ' . json_encode($aResponse) . '; </script>';
108
    }
109
110
    /**
111
     * Print the output, generated from the commands added to the response, that will be sent to the browser
112
     *
113
     * @return void
114
     */
115
    public function printOutput()
116
    {
117
        print $this->getOutput();
118
    }
119
}
120