Passed
Branch master (6f0e43)
by Robson
02:45
created

Document::upload()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace CoffeeCode\Uploader;
4
5
/**
6
 * Class CoffeeCode Document
7
 *
8
 * @author Tiago Chini <https://github.com/tiagochini>
9
 * @package CoffeeCode\Uploader
10
 */
11
class Document extends Uploader
12
{
13
    /**
14
     * Allow pdf, docx, tfm, tex, sxc, odt, odp, doc, xls, xlsm, ppt, pptm documents
15
     *
16
     * @var array allowed file types
17
     */
18
    protected static $allowTypes = [
19
        "application/pdf",
20
        "application/msword",
21
        "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
22
        "application/vnd.ms-excel",
23
        "application/vnd.ms-excel.sheet.macroenabled.12",
24
        "application/vnd.openxmlformats-officedocument.presentationml.presentation",
25
        "application/vnd.openxmlformats-officedocument.presentationml.slide",
26
        "application/vnd.openxmlformats-officedocument.presentationml.slideshow",
27
        "application/vnd.ms-powerpoint.presentation.macroenabled.12",
28
        "application/vnd.ms-powerpoint",
29
        "application/vnd.openofficeorg.extension",
30
        "application/vnd.oasis.opendocument.presentation",
31
        "application/vnd.oasis.opendocument.text",
32
        "application/vnd.sun.xml.calc",
33
        "application/x-tex",
34
        "application/x-tex-tfm"
35
    ];
36
37
    /**
38
     * Send an document from a form
39
     *
40
     * @param array $document
41
     * @param string $name
42
     * @return null|string
43
     * @throws \Exception
44
     */
45
    public function upload(array $document, string $name): string
46
    {
47
        if (!in_array($document['type'], static::$allowTypes)) {
48
            throw new \Exception("{$document['type']} - Not a valid document type");
49
        } else {
50
            $this->ext = mb_strtolower(pathinfo($document['name'])['extension']);
51
            $this->name($name);
52
        }
53
54
        move_uploaded_file($document['tmp_name'], "{$this->path}/{$this->name}");
55
        return "{$this->path}/{$this->name}";
56
    }
57
}