Passed
Push — master ( a07882...2b8759 )
by Thierry
02:21
created

UploadHandler   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 145
rs 10
c 0
b 0
f 0
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A canProcessRequest() 0 3 2
A files() 0 3 1
A __construct() 0 13 3
A isHttpUpload() 0 3 1
A sanitizer() 0 3 1
A processRequest() 0 32 4
1
<?php
2
3
/**
4
 * UploadHandler.php - This class implements file upload with Ajax.
5
 *
6
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
7
 * @author Thierry Feuzeu <[email protected]>
8
 * @copyright 2017 Thierry Feuzeu <[email protected]>
9
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
10
 * @link https://github.com/jaxon-php/jaxon-core
11
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
PHP version not specified
Loading history...
12
13
namespace Jaxon\Request\Handler;
14
15
use Jaxon\Request\Upload\UploadManager;
16
use Jaxon\Response\ResponseManager;
17
use Jaxon\Response\UploadResponse;
18
use Jaxon\Utils\Translation\Translator;
19
use Jaxon\Exception\RequestException;
20
21
use Closure;
22
use Exception;
23
24
use function count;
25
use function trim;
26
27
class UploadHandler
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class UploadHandler
Loading history...
28
{
29
    /**
30
     * The response manager
31
     *
32
     * @var ResponseManager
33
     */
34
    protected $xResponseManager;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
35
36
    /**
37
     * HTTP file upload manager
38
     *
39
     * @var UploadManager
40
     */
41
    protected $xUploadManager = null;
42
43
    /**
44
     * @var Translator
45
     */
46
    protected $xTranslator;
47
48
    /**
49
     * The uploaded files copied in the user dir
50
     *
51
     * @var array
52
     */
53
    protected $aUserFiles = [];
54
55
    /**
56
     * The name of file containing upload data
57
     *
58
     * @var string
59
     */
60
    protected $sTempFile = '';
61
62
    /**
63
     * Is the current request an HTTP upload
64
     *
65
     * @var bool
0 ignored issues
show
Bug introduced by
Expected "boolean" but found "bool" for @var tag in member variable comment
Loading history...
66
     */
67
    protected $bIsAjaxRequest = true;
68
69
    /**
70
     * The constructor
71
     *
72
     * @param UploadManager $xUploadManager    HTTP file upload manager
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
73
     * @param Translator $xTranslator
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
74
     * @param ResponseManager $xResponseManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
75
     */
76
    public function __construct(UploadManager $xUploadManager, Translator $xTranslator, ResponseManager $xResponseManager)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
77
    {
78
        $this->xResponseManager = $xResponseManager;
79
        $this->xUploadManager = $xUploadManager;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 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...
80
        $this->xTranslator = $xTranslator;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 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...
81
82
        if(isset($_POST['jxnupl']))
83
        {
84
            $this->sTempFile = trim($_POST['jxnupl']);
85
        }
86
        elseif(isset($_GET['jxnupl']))
87
        {
88
            $this->sTempFile = trim($_GET['jxnupl']);
89
        }
90
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
91
92
    /**
93
     * Set the uploaded file name sanitizer
94
     *
95
     * @param Closure $cSanitizer    The closure
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
96
     *
97
     * @return void
98
     */
99
    public function sanitizer(Closure $cSanitizer)
100
    {
101
        $this->xUploadManager->setNameSanitizer($cSanitizer);
102
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
103
104
    /**
105
     * Get the uploaded files
106
     *
107
     * @return array
108
     */
109
    public function files(): array
110
    {
111
        return $this->aUserFiles;
112
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
113
114
    /**
115
     * Inform this plugin that other plugin can process the current request
116
     *
117
     * @return void
118
     */
119
    public function isHttpUpload()
120
    {
121
        $this->bIsAjaxRequest = false;
122
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
123
124
    /**
125
     * Check if the current request contains uploaded files
126
     *
127
     * @return bool
128
     */
129
    public function canProcessRequest(): bool
130
    {
131
        return (count($_FILES) > 0 || ($this->sTempFile));
132
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
133
134
    /**
135
     * Process the uploaded files in the HTTP request
136
     *
137
     * @return bool
138
     * @throws RequestException
139
     */
140
    public function processRequest(): bool
141
    {
142
        if(($this->sTempFile))
143
        {
144
            // Ajax request following a normal HTTP upload.
145
            // Copy the previously uploaded files' location from the temp file.
146
            $this->aUserFiles = $this->xUploadManager->readFromTempFile($this->sTempFile);
147
            return true;
148
        }
149
150
        // Ajax or Http request with upload; copy the uploaded files.
151
        $this->aUserFiles = $this->xUploadManager->readFromHttpData();
152
153
        // For Ajax requests, there is nothing else to do here.
154
        if($this->bIsAjaxRequest)
155
        {
156
            return true;
157
        }
158
        // For HTTP requests, save the files' location to a temp file,
159
        // and return a response with a reference to this temp file.
160
        $xResponse = new UploadResponse();
161
        try
162
        {
163
            $sTempFile = $this->xUploadManager->saveToTempFile($this->aUserFiles);
164
            $xResponse->setUploadedFile($sTempFile);
165
        }
166
        catch(Exception $e)
167
        {
168
            $xResponse->setErrorMessage($e->getMessage());
169
        }
170
        $this->xResponseManager->append($xResponse);
171
        return true;
172
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
173
}
174