Completed
Push — master ( 465cbd...e0d13e )
by Marco
06:44 queued 11s
created

File::save()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 28
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 28
c 0
b 0
f 0
ccs 0
cts 14
cp 0
rs 8.439
cc 6
eloc 10
nc 9
nop 2
crap 42
1
<?php namespace Comodojo\Dispatcher\Request;
2
3
use \Exception;
4
5
/**
6
 * @package     Comodojo Dispatcher
7
 * @author      Marco Giovinazzi <[email protected]>
8
 * @author      Marco Castiello <[email protected]>
9
 * @license     MIT
10
 *
11
 * LICENSE:
12
 *
13
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
 * THE SOFTWARE.
20
 */
21
22
class File {
23
24
    private $slug;
25
26
    private $fname;
27
28
    private $tname;
29
30
    private $upld;
31
32
    private $ctype;
33
34
    private $size = 0;
35
36
    public function __construct($fileControl) {
37
38
        $this->load($fileControl);
39
40
    }
41
42
    /**
43
     * @return string
44
     */
45
    public function getTemporaryName() {
46
47
        return $this->tname;
48
49
    }
50
51
    public function getFileName() {
52
53
        return $this->fname;
54
55
    }
56
57
    public function getSlug() {
58
59
        return $this->slug;
60
61
    }
62
63
    public function getContentType() {
64
65
        return $this->ctype;
66
67
    }
68
69
    public function getSize() {
70
71
        return $this->size;
72
73
    }
74
75
    public function getUploadTime() {
76
77
        return $this->upld;
78
79
    }
80
81
    public function getFileData() {
82
83
        $file = $this->getTemporaryName();
84
85
        if (file_exists($file)) {
86
            return file_get_contents($file);
87
        }
88
89
        throw new Exception("File does not exists");
90
91
    }
92
93
    public function load($slugOrControl) {
94
95
        if (isset($_FILES) && isset($_FILES[$slugOrControl])) {
96
97
            $this->loadFromUploadedFile($slugOrControl);
98
99
            return $this;
100
101
        }
102
103
        throw new Exception("The requested file has not been uploaded");
104
105
    }
106
107
    public function save($path, $as_slug = false) {
108
109
        if (!empty($path) && file_exists($path)) {
110
111
            $local_name = "$path/".($as_slug ? $this->getSlug() : $this->getFileName());
112
113
            if (file_exists($local_name)) {
114
115
                $files = glob("$local_name*");
116
117
                $count = count($files);
118
119
                $local_name .= "-$count";
120
121
            }
122
123
            if (move_uploaded_file($this->getTemporaryName(), $local_name)) {
124
125
                // return file_exists($local_name);
126
                return true;
127
128
            }
129
130
            throw new Exception("Unable to save file");
131
132
        }
133
134
        throw new Exception("Repository path not available");
135
136
    }
137
138
    private function loadFromUploadedFile($fileControl) {
139
140
        $file = $_FILES[$fileControl];
141
142
        $this->tname = $file['tmp_name'];
143
        $this->fname = $file['name'];
144
        $this->ctype = $file['type'];
145
        $this->size = intval($file['size']);
146
        $this->upld = filectime($file['tmp_name']);
147
        $this->slug = self::createSlug($this->fname);
148
149
        return $this;
150
151
    }
152
153
    private static function createSlug($filename) {
154
155
        preg_match_all("/[a-z0-9]+/", iconv("UTF-8", "ASCII//TRANSLIT", strtolower(preg_replace('/\..*?$/', '', $filename))), $matches);
156
157
        return implode('-', $matches[0]);
158
159
    }
160
161
    public static function fromUploadedFiles($repository = '') {
162
163
        $files = array();
164
165
        foreach ($_FILES as $idx => $data) {
166
167
            $files[] = new File($idx, $repository);
0 ignored issues
show
Unused Code introduced by
The call to Comodojo\Dispatcher\Request\File::__construct() has too many arguments starting with $repository. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

167
            $files[] = /** @scrutinizer ignore-call */ new File($idx, $repository);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
168
169
        }
170
171
        return $files;
172
173
    }
174
175
}
176