Passed
Push — main ( f761c5...ad2327 )
by Alex
11:39
created

Resource::setFiles()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 46
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 34
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 46
rs 8.7537
1
<?php
2
3
namespace Erykai\Upload;
4
5
6
/**
7
 * Class resource upload
8
 */
9
abstract class Resource
10
{
11
    use TraitUpload;
12
13
    /**
14
     * @var object|null
15
     */
16
    private ?object $files;
17
    /**
18
     * @var string
19
     */
20
    private string $path;
21
    /**
22
     * @var array|null
23
     */
24
    private ?array $mimeType;
25
    /**
26
     * @var array|object
27
     */
28
    private array|object $data;
29
    /**
30
     * @var object
31
     */
32
    private object $response;
33
    /**
34
     * @var string|null
35
     */
36
    protected ?string $url;
37
    /**
38
     * @var string|null
39
     */
40
    protected ?string $key;
41
42
    /**
43
     *
44
     */
45
    public function __construct(?string $url = null, ?string $key = null)
46
    {
47
        $this->url = $url;
48
        $this->key = $key;
49
        $this->setFiles();
50
    }
51
    /**
52
     * @return object|null
53
     */
54
    protected function getFiles(): ?object
55
    {
56
        return $this->files;
57
    }
58
59
    /**
60
     * convert in object and count files
61
     */
62
    private function setFiles(): bool
63
    {
64
        $files = [];
65
        if (!empty($_FILES)) {
66
            $this->setMimeType();
67
            $upload = $_FILES;
68
69
            foreach ($upload as $key => $file) {
70
                if (!in_array($file['type'], $this->getMimeType(), true) && !$this->url) {
71
                    $this->files = null;
72
                    $this->setResponse(400, "error","invalid file format ".$file['type'], "upload", dynamic: $file['type'] );
73
                    return false;
74
                }
75
76
                if($this->url)
77
                {
78
                    $url_array = explode("/", $this->url);
79
                    $image_name = end($url_array);
80
                    $image_array = explode(".", $image_name);
81
82
                    $this->setPath('img');
83
                    $file['key'] = $this->key;
84
                    $file['ext'] = end($image_array);
85
                    $file['name'] = $this->slug($image_name);
86
                    $file['path'] = $this->getPath();
87
                    $file['directory'] = dirname(__DIR__, 4) . "/" . $this->getPath();
88
                    $files[] = (object)$file;
89
                }else{
90
                    [$type] = explode("/", $file['type']);
91
                    $this->setPath($type);
92
                    $file['key'] = $key;
93
                    $file['ext'] = pathinfo($file['name'], PATHINFO_EXTENSION);
94
                    $file['name'] = $this->slug(pathinfo($file['name'], PATHINFO_FILENAME));
0 ignored issues
show
Bug introduced by
It seems like pathinfo($file['name'], ...load\PATHINFO_FILENAME) can also be of type array; however, parameter $name of Erykai\Upload\Resource::slug() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

94
                    $file['name'] = $this->slug(/** @scrutinizer ignore-type */ pathinfo($file['name'], PATHINFO_FILENAME));
Loading history...
95
                    $file['path'] = $this->getPath();
96
                    $file['directory'] = dirname(__DIR__, 4) . "/" . $this->getPath();
97
                    $files[] = (object)$file;
98
                }
99
100
101
            }
102
            $this->files = (object)$files;
103
        } else {
104
            $this->files = null;
105
        }
106
        $this->setResponse(200, "success","defined attribute", "upload");
107
        return true;
108
    }
109
110
    /**
111
     * @return string|null
112
     */
113
    private function getPath(): ?string
114
    {
115
        return $this->path;
116
    }
117
118
    /**
119
     * @param string $type
120
     */
121
    private function setPath(string $type): void
122
    {
123
        $this->path = UPLOAD_DIR . "/$type/" . date('Y') . "/" . date('m') . "/" . date('d');
124
    }
125
126
    /**
127
     * @return array|null
128
     */
129
    private function getMimeType(): ?array
130
    {
131
        return $this->mimeType;
132
    }
133
134
    /**
135
     * valide mimetype accept
136
     */
137
    private function setMimeType(): void
138
    {
139
        $this->mimeType = UPLOAD_MIMETYPE;
140
    }
141
142
    /**
143
     * @return object
144
     */
145
    protected function getData(): object
146
    {
147
        return (object) $this->data;
148
    }
149
150
    /**
151
     * @param string $key
152
     * @param string $value
153
     */
154
    protected function setData(string $key, string $value): void
155
    {
156
        $this->data[$key] = $value;
157
    }
158
159
    /**
160
     * @return object
161
     */
162
    protected function getResponse(): object
163
    {
164
        return $this->response;
165
    }
166
167
    /**
168
     * @param int $code
169
     * @param string $type
170
     * @param string $text
171
     * @param string $model
172
     * @param object|null $data
173
     * @param string|null $dynamic
174
     */
175
    protected function setResponse(int $code, string $type, string $text, string $model, ?object $data = null, ?string $dynamic = null): void
176
    {
177
        $this->response = (object)[
178
            "code" => $code,
179
            "type" => $type,
180
            "text" => $text,
181
            "model" => $model,
182
            "data" => $data,
183
            "dynamic" => $dynamic
184
        ];
185
    }
186
}