File   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 21
c 1
b 0
f 0
dl 0
loc 45
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A upload() 0 10 3
1
<?php
2
3
namespace CodeBlog\DataUploader;
4
5
/**
6
 * Class CodeBlog File
7
 *
8
 * @author Whallysson Avelino <https://github.com/whallysson>
9
 * @package CodeBlog\DataUploader
10
 */
11
class File extends DataUploader {
12
13
    /**
14
     * Allow zip, rar, bzip, pdf, doc, docx files
15
     * @var array allowed file types
16
     * https://www.freeformatter.com/mime-types-list.html
17
     */
18
    protected static $allowTypes = [
19
        "application/zip",
20
        'application/x-rar-compressed',
21
        'application/x-bzip',
22
        "application/pdf",
23
        "application/msword",
24
        "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
25
    ];
26
27
    /**
28
     * Allowed extensions to types.
29
     * @var array
30
     */
31
    protected static $extensions = [
32
        "zip",
33
        "rar",
34
        "bz",
35
        "pdf",
36
        "doc",
37
        "docx"
38
    ];
39
40
    /**
41
     * @param array $file
42
     * @param string $name
43
     * @return null|string
44
     * @throws \Exception
45
     */
46
    public function upload(array $file, string $name) {
47
        $this->ext = mb_strtolower(pathinfo($file['name'])['extension']);
48
49
        if (!in_array($file['type'], static::$allowTypes) || !in_array($this->ext, static::$extensions)) {
50
            throw new \Exception("Not a valid file type or extension");
51
        }
52
53
        $this->name($name);
54
        move_uploaded_file($file['tmp_name'], "{$this->path}/{$this->name}");
55
        return "{$this->path}/{$this->name}";
56
    }
57
58
}
59