Media::upload()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 3
nc 2
nop 2
1
<?php
2
3
namespace CodeBlog\DataUploader;
4
5
/**
6
 * Class CodeBlog Media
7
 *
8
 * @author Whallysson Avelino <https://github.com/whallysson>
9
 * @package CodeBlog\DataUploader
10
 */
11
class Media extends DataUploader {
12
13
    /**
14
     * Allow mp4 video and mp3 audio
15
     * @var array allowed media types
16
     * https://www.freeformatter.com/mime-types-list.html
17
     */
18
    protected static $allowTypes = [
19
        "audio/mp3",
20
        "video/mp4",
21
    ];
22
23
    /**
24
     * Allowed extensions to types.
25
     * @var array
26
     */
27
    protected static $extensions = [
28
        "mp3",
29
        "mp4"
30
    ];
31
32
    /**
33
     * @param array $media
34
     * @param string $name
35
     * @return null|string
36
     * @throws \Exception
37
     */
38
    public function upload(array $media, string $name) {
39
        $this->ext = mb_strtolower(pathinfo($media['name'])['extension']);
40
41
        if (!in_array($media['type'], static::$allowTypes) || !in_array($this->ext, static::$extensions)) {
42
            throw new \Exception("Not a valid media type or extension");
43
        }
44
45
        $this->name($name);
46
        move_uploaded_file($media['tmp_name'], "{$this->path}/{$this->name}");
47
        return "{$this->path}/{$this->name}";
48
    }
49
50
}
51