Uploader   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 27
dl 0
loc 89
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A dir() 0 4 3
A isExtension() 0 3 1
A path() 0 7 1
A isAllowed() 0 3 1
A __construct() 0 5 1
A name() 0 14 3
1
<?php
2
3
namespace CoffeeCode\Uploader;
4
5
/**
6
 * Class CoffeeCode Uploader
7
 *
8
 * @author Robson V. Leite <https://github.com/robsonvleite>
9
 * @package CoffeeCode\Uploader
10
 */
11
abstract class Uploader
12
{
13
    /** @var string */
14
    protected $path;
15
16
    /** @var resource */
17
    protected $file;
18
19
    /** @var string */
20
    protected $name;
21
22
    /** @var string */
23
    protected $ext;
24
25
    /** @var array */
26
    protected static $allowTypes = [];
27
28
    /** @var array */
29
    protected static $extensions = [];
30
31
    /**
32
     * @param string $uploadDir
33
     * @param string $fileTypeDir
34
     * @example $u = new Upload("storage/uploads", "images");
35
     */
36
    public function __construct(string $uploadDir, string $fileTypeDir)
37
    {
38
        $this->dir($uploadDir);
39
        $this->dir("{$uploadDir}/{$fileTypeDir}");
40
        $this->path("{$uploadDir}/{$fileTypeDir}");
41
    }
42
43
    /**
44
     * @return array
45
     */
46
    public static function isAllowed(): array
47
    {
48
        return static::$allowTypes;
49
    }
50
51
    /**
52
     * @return array
53
     */
54
    public static function isExtension(): array
55
    {
56
        return static::$extensions;
57
    }
58
59
    /**
60
     * @param string $name
61
     * @return string
62
     */
63
    protected function name(string $name): string
64
    {
65
        $name = filter_var(mb_strtolower($name), FILTER_SANITIZE_STRIPPED);
66
        $formats = 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜüÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûýýþÿRr"!@#$%&*()_-+={[}]/?;:.,\\\'<>°ºª';
67
        $replace = 'aaaaaaaceeeeiiiidnoooooouuuuuybsaaaaaaaceeeeiiiidnoooooouuuyybyRr                                 ';
68
        $name = str_replace(["-----", "----", "---", "--"], "-",
69
            str_replace(" ", "-", trim(strtr(utf8_decode($name), utf8_decode($formats), $replace))));
70
71
        $this->name = "{$name}." . $this->ext;
72
73
        if (file_exists("{$this->path}/{$this->name}") && is_file("{$this->path}/{$this->name}")) {
74
            $this->name = "{$name}-" . time() . ".{$this->ext}";
75
        }
76
        return $this->name;
77
    }
78
79
    /**
80
     * @param string $dir
81
     * @param int $mode
82
     */
83
    protected function dir(string $dir, int $mode = 0755): void
84
    {
85
        if (!file_exists($dir) || !is_dir($dir)) {
86
            mkdir($dir, $mode);
87
        }
88
    }
89
90
    /**
91
     * @param string $path
92
     */
93
    protected function path(string $path): void
94
    {
95
        list($yearPath, $mothPath) = explode("/", date("Y/m"));
96
97
        $this->dir("{$path}/{$yearPath}");
98
        $this->dir("{$path}/{$yearPath}/{$mothPath}");
99
        $this->path = "{$path}/{$yearPath}/{$mothPath}";
100
    }
101
}