DataUploader   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 10
eloc 30
c 3
b 0
f 1
dl 0
loc 99
rs 10

6 Methods

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