Passed
Push — master ( 888339...c604af )
by Whallysson
07:18 queued 06:08
created

DataUploader   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
eloc 24
c 2
b 0
f 0
dl 0
loc 70
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A name() 0 13 3
A dir() 0 3 3
A path() 0 6 1
A isAllowed() 0 2 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
    /** @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
    /**
26
     * @param string $uploadDir
27
     * @param string $fileTypeDir
28
     * @example $uploade = new DataUploader("storage/uploads", "images");
29
     */
30
    public function __construct(string $uploadDir, string $fileTypeDir) {
31
        $this->dir($uploadDir);
32
        $this->dir("{$uploadDir}/{$fileTypeDir}");
33
        $this->path("{$uploadDir}/{$fileTypeDir}");
34
    }
35
    
36
    /**
37
     * @return array
38
     */
39
    public static function isAllowed() {
40
        return array_merge(static::$allowJPG, static::$allowPNG, static::$allowGIF, static::$allowTypes);
41
    }
42
43
    /**
44
     * @param string $name
45
     * @return string
46
     */
47
    protected function name(string $name) {
48
        $name = filter_var(mb_strtolower($name), FILTER_SANITIZE_STRIPPED);
49
        $formats = 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜüÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûýýþÿRr"!@#$%&*()_-+={[}]/?;:.,\\\'<>°ºª¹²³';
50
        $replace = 'aaaaaaaceeeeiiiidnoooooouuuuuybsaaaaaaaceeeeiiiidnoooooouuuyybyRr                                    ';
51
        $name = str_replace(["-----", "----", "---", "--"], "-",
52
                str_replace(" ", "-", trim(strtr(utf8_decode($name), utf8_decode($formats), $replace))));
53
54
        $this->name = "{$name}." . $this->ext;
55
56
        if (file_exists("{$this->path}/{$this->name}") && is_file("{$this->path}/{$this->name}")) {
57
            $this->name = "{$name}-" . time() . ".{$this->ext}";
58
        }
59
        return $this->name;
60
    }
61
62
    /**
63
     * @param string $dir
64
     * @param int $mode
65
     */
66
    protected function dir(string $dir, int $mode = 0755) {
67
        if (!file_exists($dir) || !is_dir($dir)) {
68
            mkdir($dir, $mode);
69
        }
70
    }
71
72
    /**
73
     * @param string $path
74
     */
75
    protected function path(string $path) {
76
        list($yearPath, $mothPath) = explode("/", date("Y/m"));
77
78
        $this->dir("{$path}/{$yearPath}");
79
        $this->dir("{$path}/{$yearPath}/{$mothPath}");
80
        $this->path = "{$path}/{$yearPath}/{$mothPath}";
81
    }
82
83
}
84