File::upload()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 11
rs 10
1
<?php
2
3
namespace SimpSyst\Uploader;
4
5
/**
6
 * Class SimpSyst File
7
 *
8
 * @author Diego Matos <https://github.com/diegoamatos>
9
 * @package SimpSyst\Uploader
10
 */
11
class File extends Uploader
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): string
47
    {
48
        $this->ext = mb_strtolower(pathinfo($file['name'])['extension']);
49
50
        if (!in_array($file['type'], static::$allowTypes) || !in_array($this->ext, static::$extensions)) {
51
            throw new \Exception("Not a valid file type or extension");
52
        }
53
54
        $this->name($name);
55
        move_uploaded_file($file['tmp_name'], "{$this->path}/{$this->name}");
56
        return "{$this->path}/{$this->name}";
57
    }
58
}