Send   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 41
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A upload() 0 11 3
1
<?php
2
3
namespace SimpSyst\Uploader;
4
5
/**
6
 * Class SimpSyst Send
7
 *
8
 * @author Diego Matos <https://github.com/diegoamatos>
9
 * @package SimpSyst\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
     * @param bool $monthYearPath
21
     * https://www.freeformatter.com/mime-types-list.html
22
     */
23
    public function __construct(
24
        string $uploadDir,
25
        string $fileTypeDir,
26
        array $allowTypes,
27
        array $extensions,
28
        bool $monthYearPath = true
29
    ) {
30
        parent::__construct($uploadDir, $fileTypeDir, $monthYearPath);
31
        self::$allowTypes = $allowTypes;
32
        self::$extensions = $extensions;
33
    }
34
35
    /**
36
     * @param array $file
37
     * @param string $name
38
     * @return string
39
     * @throws \Exception
40
     */
41
    public function upload(array $file, string $name): string
42
    {
43
        $this->ext = mb_strtolower(pathinfo($file['name'])['extension']);
44
45
        if (!in_array($file['type'], static::$allowTypes) || !in_array($this->ext, static::$extensions)) {
46
            throw new \Exception("Not a valid file type or extension");
47
        }
48
49
        $this->name($name);
50
        move_uploaded_file($file['tmp_name'], "{$this->path}/{$this->name}");
51
        return "{$this->path}/{$this->name}";
52
    }
53
}