Media   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 13
dl 0
loc 38
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A upload() 0 11 3
1
<?php
2
3
namespace CoffeeCode\Uploader;
4
5
/**
6
 * Class CoffeeCode Media
7
 *
8
 * @author Robson V. Leite <https://github.com/robsonvleite>
9
 * @package CoffeeCode\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
        "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): string
39
    {
40
        $this->ext = mb_strtolower(pathinfo($media['name'])['extension']);
41
42
        if (!in_array($media['type'], static::$allowTypes) || !in_array($this->ext, static::$extensions)) {
43
            throw new \Exception("Not a valid media type or extension");
44
        }
45
46
        $this->name($name);
47
        move_uploaded_file($media['tmp_name'], "{$this->path}/{$this->name}");
48
        return "{$this->path}/{$this->name}";
49
    }
50
}