Passed
Push — master ( 5198fb...6eb156 )
by Thierry
02:34
created

UploadHandler::files()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\Di\Container;
16
use Jaxon\Exception\RequestException;
17
use Jaxon\Response\Manager\ResponseManager;
18
use Jaxon\Response\UploadResponse;
19
use Jaxon\Utils\Translation\Translator;
20
use Psr\Http\Message\ServerRequestInterface;
21
22
use Closure;
23
use Exception;
24
25
use function count;
26
use function trim;
27
28
class UploadHandler
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class UploadHandler
Loading history...
29
{
30
    /**
31
     * DI container
32
     *
33
     * @var Container
34
     */
35
    protected $di;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
36
37
    /**
38
     * The response manager
39
     *
40
     * @var ResponseManager
41
     */
42
    protected $xResponseManager;
43
44
    /**
45
     * @var Translator
46
     */
47
    protected $xTranslator;
48
49
    /**
50
     * The uploaded files copied in the user dir
51
     *
52
     * @var array
53
     */
54
    protected $aUserFiles = [];
55
56
    /**
57
     * The name of file containing upload data
58
     *
59
     * @var string
60
     */
61
    protected $sTempFile = '';
62
63
    /**
64
     * Is the current request an HTTP upload
65
     *
66
     * @var bool
0 ignored issues
show
Bug introduced by
Expected "boolean" but found "bool" for @var tag in member variable comment
Loading history...
67
     */
68
    protected $bIsAjaxRequest = true;
69
70
    /**
71
     * The constructor
72
     *
73
     * @param Container $di
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 7 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
     * @param Translator $xTranslator
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
76
     */
77
    public function __construct(Container $di, ResponseManager $xResponseManager, Translator $xTranslator)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
78
    {
79
        $this->di = $di;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 15 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->xResponseManager = $xResponseManager;
81
        $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...
82
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
83
84
    /**
85
     * Set the uploaded file name sanitizer
86
     *
87
     * @param Closure $cSanitizer    The closure
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
88
     *
89
     * @return void
90
     */
91
    public function sanitizer(Closure $cSanitizer)
92
    {
93
        $this->di->getUploadManager()->setNameSanitizer($cSanitizer);
94
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
95
96
    /**
97
     * Get the uploaded files
98
     *
99
     * @return array
100
     */
101
    public function files(): array
102
    {
103
        return $this->aUserFiles;
104
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
105
106
    /**
107
     * Inform this plugin that other plugin can process the current request
108
     *
109
     * @return void
110
     */
111
    public function isHttpUpload()
112
    {
113
        $this->bIsAjaxRequest = false;
114
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
115
116
    /**
117
     * Check if the current request contains uploaded files
118
     *
119
     * @param ServerRequestInterface $xRequest
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
120
     *
121
     * @return bool
122
     */
123
    public function canProcessRequest(ServerRequestInterface $xRequest): bool
124
    {
125
        if(count($xRequest->getUploadedFiles()) > 0)
126
        {
127
            return true;
128
        }
129
        $aBody = $xRequest->getParsedBody();
130
        if(is_array($aBody))
131
        {
132
            return isset($aBody['jxnupl']);
133
        }
134
        $aParams = $xRequest->getQueryParams();
135
        return isset($aParams['jxnupl']);
136
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
137
138
    /**
139
     * Read the upload temp file name from the HTTP request
140
     *
141
     * @param ServerRequestInterface $xRequest
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
142
     *
143
     * @return bool
144
     */
145
    private function setTempFile(ServerRequestInterface $xRequest): bool
146
    {
147
        $aBody = $xRequest->getParsedBody();
148
        if(is_array($aBody))
149
        {
150
            $this->sTempFile = trim($aBody['jxnupl'] ?? '');
151
            return $this->sTempFile !== '';
152
        }
153
        $aParams = $xRequest->getQueryParams();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 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...
154
        $this->sTempFile = trim($aParams['jxnupl'] ?? '');
155
        return $this->sTempFile !== '';
156
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
157
158
    /**
159
     * Process the uploaded files in the HTTP request
160
     *
161
     * @param ServerRequestInterface $xRequest
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
162
     *
163
     * @return bool
164
     * @throws RequestException
165
     */
166
    public function processRequest(ServerRequestInterface $xRequest): bool
167
    {
168
        $xUploadManager = $this->di->getUploadManager();
169
        if($this->setTempFile($xRequest))
170
        {
171
            // Ajax request following a normal HTTP upload.
172
            // Copy the previously uploaded files' location from the temp file.
173
            $this->aUserFiles = $xUploadManager->readFromTempFile($this->sTempFile);
174
            return true;
175
        }
176
177
        if($this->bIsAjaxRequest)
178
        {
179
            // Ajax request with upload.
180
            // Copy the uploaded files from the HTTP request.
181
            $this->aUserFiles = $xUploadManager->readFromHttpData($xRequest);
182
            return true;
183
        }
184
185
        // For HTTP requests, save the files' location to a temp file,
186
        // and return a response with a reference to this temp file.
187
        try
188
        {
189
            // Copy the uploaded files from the HTTP request, and create the temp file.
190
            $this->aUserFiles = $xUploadManager->readFromHttpData($xRequest);
191
            $sTempFile = $xUploadManager->saveToTempFile($this->aUserFiles);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 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...
192
            $this->xResponseManager->append(new UploadResponse($sTempFile));
193
        }
194
        catch(Exception $e)
195
        {
196
            $this->xResponseManager->append(new UploadResponse('', $e->getMessage()));
197
        }
198
        return true;
199
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
200
}
201