Send   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 10
dl 0
loc 35
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A upload() 0 11 3
A __construct() 0 5 1
1
<?php
2
3
namespace CoffeeCode\Uploader;
4
5
/**
6
 * Class CoffeeCode Send
7
 *
8
 * @author Robson V. Leite <https://github.com/robsonvleite>
9
 * @package CoffeeCode\Uploader
10
 */
11
class Send extends Uploader
12
{
13
    /**
14
     * Send constructor.
15
     *
16
     * @param string $uploadDir
17
     * @param string $fileTypeDir
18
     * @param array $allowTypes
19
     * @param array $extensions
20
     * https://www.freeformatter.com/mime-types-list.html
21
     */
22
    public function __construct(string $uploadDir, string $fileTypeDir, array $allowTypes, array $extensions)
23
    {
24
        parent::__construct($uploadDir, $fileTypeDir);
25
        self::$allowTypes = $allowTypes;
26
        self::$extensions = $extensions;
27
    }
28
29
    /**
30
     * @param array $file
31
     * @param string $name
32
     * @return string
33
     * @throws \Exception
34
     */
35
    public function upload(array $file, string $name): string
36
    {
37
        $this->ext = mb_strtolower(pathinfo($file['name'])['extension']);
38
39
        if (!in_array($file['type'], static::$allowTypes) || !in_array($this->ext, static::$extensions)) {
40
            throw new \Exception("Not a valid file type or extension");
41
        }
42
43
        $this->name($name);
44
        move_uploaded_file($file['tmp_name'], "{$this->path}/{$this->name}");
45
        return "{$this->path}/{$this->name}";
46
    }
47
}