Passed
Push — master ( 8eafb5...9551a9 )
by Thierry
02:09
created

UploadPlugin::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
nc 3
nop 3
dl 0
loc 13
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * UploadPlugin.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\Plugin\Upload;
14
15
use Jaxon\Plugin\Request as RequestPlugin;
16
use Jaxon\Response\Manager as 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 UploadPlugin extends RequestPlugin
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class UploadPlugin
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 support
38
     *
39
     * @var Manager
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 Manager $xUploadManager    HTTP file upload manager
0 ignored issues
show
Coding Style introduced by
Expected 9 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter name; 4 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(Manager $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
     * @inheritDoc
94
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
95
    public function getName(): string
96
    {
97
        return 'UploadPlugin';
98
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
99
100
    /**
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...
101
     * @inheritDoc
102
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
103
    public function checkOptions(string $sCallable, $xOptions): array
104
    {
105
        return [];
106
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
107
108
    /**
109
     * Set the uploaded file name sanitizer
110
     *
111
     * @param Closure $cSanitizer    The closure
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
112
     *
113
     * @return void
114
     */
115
    public function sanitizer(Closure $cSanitizer)
116
    {
117
        $this->xUploadManager->setNameSanitizer($cSanitizer);
118
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
119
120
    /**
121
     * Get the uploaded files
122
     *
123
     * @return array
124
     */
125
    public function files(): array
126
    {
127
        return $this->aUserFiles;
128
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
129
130
    /**
131
     * Inform this plugin that other plugin can process the current request
132
     *
133
     * @return void
134
     */
135
    public function isHttpUpload()
136
    {
137
        $this->bIsAjaxRequest = false;
138
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
139
140
    /**
141
     * @inheritDoc
142
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
143
    public function canProcessRequest(): bool
144
    {
145
        return (count($_FILES) > 0 || ($this->sTempFile));
146
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
147
148
    /**
149
     * Process the uploaded files in the HTTP request
150
     *
151
     * @return bool
152
     * @throws RequestException
153
     */
154
    public function processRequest(): bool
155
    {
156
        if(!$this->canProcessRequest())
157
        {
158
            return false;
159
        }
160
161
        if(($this->sTempFile))
162
        {
163
            // Ajax request following a normal HTTP upload.
164
            // Copy the previously uploaded files' location from the temp file.
165
            $this->aUserFiles = $this->xUploadManager->readFromTempFile($this->sTempFile);
166
            return true;
167
        }
168
169
        // Ajax or Http request with upload; copy the uploaded files.
170
        $this->aUserFiles = $this->xUploadManager->readFromHttpData();
171
172
        // For Ajax requests, there is nothing else to do here.
173
        if($this->bIsAjaxRequest)
174
        {
175
            return true;
176
        }
177
        // For HTTP requests, save the files' location to a temp file,
178
        // and return a response with a reference to this temp file.
179
        $xResponse = new UploadResponse();
180
        try
181
        {
182
            $sTempFile = $this->xUploadManager->saveToTempFile($this->aUserFiles);
183
            $xResponse->setUploadedFile($sTempFile);
184
        }
185
        catch(Exception $e)
186
        {
187
            $xResponse->setErrorMessage($e->getMessage());
188
        }
189
        $this->xResponseManager->append($xResponse);
190
        return true;
191
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
192
}
193