Passed
Push — master ( f79a4b...8eafb5 )
by Thierry
02:11
created

Plugin::files()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * Plugin.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\Upload;
14
15
use Jaxon\Jaxon;
16
use Jaxon\Plugin\Request as RequestPlugin;
17
use Jaxon\Response\Manager as ResponseManager;
18
use Jaxon\Response\UploadResponse;
19
use Jaxon\Utils\Translation\Translator;
20
use Jaxon\Exception\RequestException;
21
22
use Closure;
23
use Exception;
24
25
use function count;
26
use function trim;
27
28
class Plugin extends RequestPlugin
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Plugin
Loading history...
29
{
30
    /**
31
     * The response manager
32
     *
33
     * @var ResponseManager
34
     */
35
    protected $xResponseManager;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
36
37
    /**
38
     * HTTP file upload support
39
     *
40
     * @var Upload
41
     */
42
    protected $xUpload = null;
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 Upload $xUpload    HTTP file upload support
0 ignored issues
show
Coding Style introduced by
Expected 10 spaces after parameter name; 4 found
Loading history...
Coding Style introduced by
Expected 10 spaces after parameter type; 1 found
Loading history...
74
     * @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...
75
     * @param ResponseManager $xResponseManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
76
     */
77
    public function __construct(Upload $xUpload,
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
78
        Translator $xTranslator, ResponseManager $xResponseManager)
79
    {
80
        $this->xResponseManager = $xResponseManager;
81
        $this->xUpload = $xUpload;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 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
        $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...
83
84
        if(isset($_POST['jxnupl']))
85
        {
86
            $this->sTempFile = trim($_POST['jxnupl']);
87
        }
88
        elseif(isset($_GET['jxnupl']))
89
        {
90
            $this->sTempFile = trim($_GET['jxnupl']);
91
        }
92
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
93
94
    /**
95
     * @inheritDoc
96
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
97
    public function getName(): string
98
    {
99
        return 'UploadPlugin';
100
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
101
102
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $xOptions should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $sCallable should have a doc-comment as per coding-style.
Loading history...
103
     * @inheritDoc
104
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
105
    public function checkOptions(string $sCallable, $xOptions): array
106
    {
107
        return [];
108
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
109
110
    /**
111
     * Set the uploaded file name sanitizer
112
     *
113
     * @param Closure $cSanitizer    The closure
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
114
     *
115
     * @return void
116
     */
117
    public function sanitizer(Closure $cSanitizer)
118
    {
119
        $this->xUpload->setNameSanitizer($cSanitizer);
120
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
121
122
    /**
123
     * Get the uploaded files
124
     *
125
     * @return array
126
     */
127
    public function files(): array
128
    {
129
        return $this->aUserFiles;
130
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
131
132
    /**
133
     * Inform this plugin that other plugin can process the current request
134
     *
135
     * @return void
136
     */
137
    public function isHttpUpload()
138
    {
139
        $this->bIsAjaxRequest = false;
140
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
141
142
    /**
143
     * @inheritDoc
144
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
145
    public function canProcessRequest(): bool
146
    {
147
        return (count($_FILES) > 0 || ($this->sTempFile));
148
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
149
150
    /**
151
     * Process the uploaded files in the HTTP request
152
     *
153
     * @return bool
154
     * @throws RequestException
155
     */
156
    public function processRequest(): bool
157
    {
158
        if(!$this->canProcessRequest())
159
        {
160
            return false;
161
        }
162
163
        if(($this->sTempFile))
164
        {
165
            // Ajax request following a normal HTTP upload.
166
            // Copy the previously uploaded files' location from the temp file.
167
            $this->aUserFiles = $this->xUpload->readFromTempFile($this->sTempFile);
168
            return true;
169
        }
170
171
        // Ajax or Http request with upload; copy the uploaded files.
172
        $this->aUserFiles = $this->xUpload->readFromHttpData();
173
174
        // For Ajax requests, there is nothing else to do here.
175
        if($this->bIsAjaxRequest)
176
        {
177
            return true;
178
        }
179
        // For HTTP requests, save the files' location to a temp file,
180
        // and return a response with a reference to this temp file.
181
        $xResponse = new UploadResponse();
182
        try
183
        {
184
            $sTempFile = $this->xUpload->saveToTempFile($this->aUserFiles);
185
            $xResponse->setUploadedFile($sTempFile);
186
        }
187
        catch(Exception $e)
188
        {
189
            $xResponse->setErrorMessage($e->getMessage());
190
        }
191
        $this->xResponseManager->append($xResponse);
192
        return true;
193
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
194
}
195