Passed
Branch master (0c658f)
by Robson
02:02
created

Send::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
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
     */
20
    public function __construct(string $uploadDir, string $fileTypeDir, array $allowTypes)
21
    {
22
        parent::__construct($uploadDir, $fileTypeDir);
23
        self::$allowTypes = $allowTypes;
24
    }
25
26
    /**
27
     * @param array $file
28
     * @param string $name
29
     * @return string
30
     * @throws \Exception
31
     */
32
    public function upload(array $file, string $name): string
33
    {
34
        if (!in_array($file['type'], static::$allowTypes)) {
35
            throw new \Exception("{$file['type']} - Not a valid file type");
36
        } else {
37
            $this->ext = mb_strtolower(pathinfo($file['name'])['extension']);
38
            $this->name($name);
39
        }
40
41
        move_uploaded_file($file['tmp_name'], "{$this->path}/{$this->name}");
42
        return "{$this->path}/{$this->name}";
43
    }
44
}