Media::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 Media
7
 *
8
 * @author Diego Matos <https://github.com/diegoamatos>
9
 * @package SimpSyst\Uploader
10
 */
11
class Media extends Uploader
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
        "audio/mpeg",
21
        "video/mp4",
22
    ];
23
24
    /**
25
     * Allowed extensions to types.
26
     * @var array
27
     */
28
    protected static $extensions = [
29
        "mp3",
30
        "mp4"
31
    ];
32
33
    /**
34
     * @param array $media
35
     * @param string $name
36
     * @return null|string
37
     * @throws \Exception
38
     */
39
    public function upload(array $media, string $name): string
40
    {
41
        $this->ext = mb_strtolower(pathinfo($media['name'])['extension']);
42
43
        if (!in_array($media['type'], static::$allowTypes) || !in_array($this->ext, static::$extensions)) {
44
            throw new \Exception("Not a valid media type or extension");
45
        }
46
47
        $this->name($name);
48
        move_uploaded_file($media['tmp_name'], "{$this->path}/{$this->name}");
49
        return "{$this->path}/{$this->name}";
50
    }
51
}
52