ExternalFile   A
last analyzed

Complexity

Total Complexity 38

Size/Duplication

Total Lines 345
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 70
dl 0
loc 345
ccs 0
cts 145
cp 0
rs 9.36
c 0
b 0
f 0
wmc 38

27 Methods

Rating   Name   Duplication   Size   Complexity  
A getHash() 0 3 1
A getUpdatedAt() 0 3 1
A getFullName() 0 3 2
A setExtension() 0 3 1
A getModelId() 0 3 1
A setModelId() 0 3 1
A getMimeType() 0 3 1
A getName() 0 3 1
A setUpdatedAt() 0 3 1
A getCreatedAt() 0 3 1
A setSize() 0 3 1
A getExtension() 0 3 1
A setActualPath() 0 3 1
A setCreatedAt() 0 3 1
A getId() 0 3 1
A getType() 0 3 1
A setId() 0 3 1
A setMimeType() 0 3 1
A getSize() 0 3 1
A setType() 0 3 1
A setModelAlias() 0 3 1
A setHash() 0 3 1
A setName() 0 3 1
A getActualPath() 0 3 1
A getModelAlias() 0 3 1
A generateId() 0 13 2
B buildFromPath() 0 43 10
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: konstantin
5
 * Date: 1/6/18
6
 * Time: 3:12 PM
7
 */
8
9
namespace tkanstantsin\fileupload\model;
10
11
use Mimey\MimeTypes;
12
use tkanstantsin\fileupload\config\InvalidConfigException;
13
use tkanstantsin\fileupload\model\Type as FileType;
14
15
class ExternalFile extends BaseObject implements IFile
16
{
17
    /**
18
     * Length of hash from path
19
     * @see ExternalFile::generateId()
20
     */
21
    protected const ID_PATH_HASH_LENGTH = 4;
22
23
    /**
24
     * @var int|null
25
     */
26
    protected $id;
27
    /**
28
     * @var string|null
29
     */
30
    protected $modelAlias;
31
    /**
32
     * @var int|null
33
     */
34
    protected $modelId;
35
    /**
36
     * @var string|null
37
     */
38
    protected $name;
39
    /**
40
     * @var string|null
41
     */
42
    protected $extension;
43
    /**
44
     * @var int|null
45
     */
46
    protected $size;
47
    /**
48
     * @see FileType
49
     * @var int|null
50
     */
51
    protected $type;
52
    /**
53
     * @var string|null
54
     */
55
    protected $mimeType;
56
    /**
57
     * @var string|null
58
     */
59
    protected $hash;
60
    /**
61
     * @var int|null
62
     */
63
    protected $createdAt;
64
    /**
65
     * @var int|null
66
     */
67
    protected $updatedAt;
68
69
    /**
70
     * Path to file in filesystem
71
     * @var string|null
72
     */
73
    protected $actualPath;
74
75
    /**
76
     * Build IFile object based only on file path, model alias and id
77
     * @param null|string $path
78
     * @param array $config
79
     * Config MUST contain following fields:
80
     * - modelAlias
81
     * - modelId
82
     *
83
     * @return ExternalFile|null
84
     * @throws \tkanstantsin\fileupload\config\InvalidConfigException
85
     */
86
    public static function buildFromPath(?string $path, array $config = []): ?self
87
    {
88
        // detect if file path actually is url
89
        if (preg_match('/^(https?:)?\/\//', $path)) {
90
            // TODO: create special file that will not be processed.
91
            return null;
92
        }
93
        if ($path === null) {
94
            return null;
95
        }
96
97
        $file = new static($config);
98
        if ($file->getModelAlias() === null) {
99
            throw new InvalidConfigException('Model alias must be defined');
100
        }
101
        if ($file->getModelId() === null) {
102
            throw new InvalidConfigException('Model id must be defined');
103
        }
104
105
        $file->setActualPath($file->getActualPath() ?? $path);
106
        $file->setId($file->generateId());
107
        $file->setHash(crc32($file->getId()));
108
        $file->setCreatedAt($file->getCreatedAt() ?? 0);
109
        $file->setUpdatedAt($file->getUpdatedAt() ?? 0);
110
111
        if ($file->getMimeType() === null && $file->getExtension() !== null) {
112
            $mimeType = (new MimeTypes())->getMimeType($file->getExtension());
113
            if ($mimeType !== null) {
114
                $file->setMimeType($mimeType);
115
            }
116
        }
117
118
        if ($file->getType() === null) {
119
            $file->setType(FileType::getByMimeType($file->getMimeType()));
120
        }
121
122
        $file->setName(pathinfo($path, PATHINFO_FILENAME));
123
        $extension = pathinfo($path, PATHINFO_EXTENSION);
124
        if ($extension !== '') {
125
            $file->setExtension($extension);
126
        }
127
128
        return $file;
129
    }
130
131
    /**
132
     * @return int|null
133
     */
134
    public function getId(): ?int
135
    {
136
        return $this->id;
137
    }
138
139
    /**
140
     * @param int $id
141
     *
142
     * @return void
143
     */
144
    public function setId(int $id): void
145
    {
146
        $this->id = $id;
147
    }
148
149
    /**
150
     * Alias of associated model
151
     * @return null|string
152
     */
153
    public function getModelAlias(): ?string
154
    {
155
        return $this->modelAlias;
156
    }
157
158
    /**
159
     * @param string $alias
160
     */
161
    public function setModelAlias(string $alias): void
162
    {
163
        $this->modelAlias = $alias;
164
    }
165
166
    /**
167
     * Id of associated model
168
     * @return int|null
169
     */
170
    public function getModelId(): ?int
171
    {
172
        return $this->modelId;
173
    }
174
175
    /**
176
     * @param int $modelId
177
     */
178
    public function setModelId(int $modelId): void
179
    {
180
        $this->modelId = $modelId;
181
    }
182
183
    /**
184
     * Get name without extension
185
     * @return null|string
186
     */
187
    public function getName(): ?string
188
    {
189
        return $this->name;
190
    }
191
192
    /**
193
     * Set name without extension
194
     *
195
     * @param string $name
196
     */
197
    public function setName(string $name): void
198
    {
199
        $this->name = $name;
200
    }
201
202
    /**
203
     * Generate full file name (name itself + extension if exists)
204
     * @return null|string
205
     */
206
    public function getFullName(): ?string
207
    {
208
        return $this->name . ($this->extension !== null ? '.' . $this->extension : '');
209
    }
210
211
    /**
212
     * Get file extension
213
     * @return null|string
214
     */
215
    public function getExtension(): ?string
216
    {
217
        return $this->extension;
218
    }
219
220
    /**
221
     * Set file extension
222
     *
223
     * @param string $extension
224
     */
225
    public function setExtension(string $extension): void
226
    {
227
        $this->extension = $extension;
228
    }
229
230
    /**
231
     * @return int
232
     */
233
    public function getSize(): ?int
234
    {
235
        return $this->size;
236
    }
237
238
    /**
239
     * @param int $size
240
     */
241
    public function setSize(int $size): void
242
    {
243
        $this->size = $size;
244
    }
245
246
    /**
247
     * @return int
248
     */
249
    public function getType(): ?int
250
    {
251
        return $this->type;
252
    }
253
254
    /**
255
     * @param int $type
256
     */
257
    public function setType(int $type): void
258
    {
259
        $this->type = $type;
260
    }
261
262
    /**
263
     * @return null|string
264
     */
265
    public function getMimeType(): ?string
266
    {
267
        return $this->mimeType;
268
    }
269
270
    /**
271
     * @param string $mimeType
272
     */
273
    public function setMimeType(string $mimeType): void
274
    {
275
        $this->mimeType = $mimeType;
276
    }
277
278
    /**
279
     * @return string
280
     */
281
    public function getHash(): ?string
282
    {
283
        return $this->hash;
284
    }
285
286
    /**
287
     * @param string $hash
288
     */
289
    public function setHash(string $hash): void
290
    {
291
        $this->hash = $hash;
292
    }
293
294
    /**
295
     * @return int
296
     */
297
    public function getCreatedAt(): ?int
298
    {
299
        return $this->createdAt;
300
    }
301
302
    /**
303
     * @param int $createdAt
304
     */
305
    public function setCreatedAt(int $createdAt): void
306
    {
307
        $this->createdAt = $createdAt;
308
    }
309
310
    /**
311
     * @return int|null
312
     */
313
    public function getUpdatedAt(): ?int
314
    {
315
        return $this->updatedAt;
316
    }
317
318
    /**
319
     * @param int $updatedAt
320
     */
321
    public function setUpdatedAt(int $updatedAt): void
322
    {
323
        $this->updatedAt = $updatedAt;
324
    }
325
326
    /**
327
     * @return string
328
     */
329
    public function getActualPath(): ?string
330
    {
331
        return $this->actualPath;
332
    }
333
334
    /**
335
     * @param string $actualPath
336
     */
337
    public function setActualPath(string $actualPath): void
338
    {
339
        $this->actualPath = $actualPath;
340
    }
341
342
343
    /**
344
     * Returns existed id or generate unique id based on model id and file path.
345
     * @return int
346
     */
347
    protected function generateId(): int
348
    {
349
        if ($this->getId() !== null) {
350
            return $this->getId();
351
        }
352
353
        $maxIdLength = \mb_strlen((string) PHP_INT_MAX) - 1;
354
355
        // try generate unique id for file
356
        $id = mb_substr(crc32($this->getModelId()), $maxIdLength - self::ID_PATH_HASH_LENGTH)
357
            . mb_substr(crc32($this->getActualPath()), self::ID_PATH_HASH_LENGTH);
358
359
        return (int) $id;
360
    }
361
}