UploadOption::prepareFromPath()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 13
rs 10
1
<?php
2
3
namespace SaasReady\Services\FileManager;
4
5
use Illuminate\Database\Eloquent\Model;
0 ignored issues
show
Bug introduced by
The type Illuminate\Database\Eloquent\Model was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Illuminate\Http\UploadedFile;
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\UploadedFile was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use RuntimeException;
8
use SplFileInfo;
9
10
final class UploadOption
11
{
12
    /**
13
     * The file that will be uploaded to storage disk
14
     */
15
    public SplFileInfo $file;
16
17
    public ?string $originalFileName = null;
18
    public ?string $fileMimeType = null;
19
    public ?int $fileSize = null;
20
21
    /**
22
     * Desired path to store the file
23
     *
24
     * @note: require a '/' at the end
25
     * @example user/$user->id/avatar/
26
     */
27
    public string $storePath = 'files/';
28
29
    /**
30
     * New filename after uploaded
31
     *
32
     * By default, auto generate by Ready
33
     */
34
    public ?string $newFileName = null;
35
36
    /**
37
     * Source model of the File
38
     */
39
    public ?Model $source = null;
40
41
    /**
42
     * Category name of the file.
43
     */
44
    public string $category = 'file';
45
46
    /**
47
     * Custom driver of the file.
48
     *
49
     * By default, it will use the default driver from ENV
50
     */
51
    public ?string $driver = null;
52
53
    /**
54
     * Create the upload option from UploadedFile
55
     */
56
    public static function prepareFromUploadedFile(UploadedFile $uploadedFile): self
57
    {
58
        $option = new self();
59
        $option->file = $uploadedFile->getFileInfo();
60
        $option->originalFileName = $uploadedFile->getClientOriginalName();
61
        $option->fileMimeType = $uploadedFile->getMimeType();
62
        $option->fileSize = $uploadedFile->getSize();
63
64
        return $option;
65
    }
66
67
    /**
68
     * Create the upload option from a specific file in your server/machine
69
     */
70
    public static function prepareFromPath(string $filePath): self
71
    {
72
        $option = new self();
73
        $option->file = new SplFileInfo($filePath);
74
75
        if (!$option->file->isReadable()) {
76
            throw new RuntimeException("The file $filePath is not readable. Cannot process further.");
77
        }
78
79
        $option->originalFileName = basename($filePath);
80
        $option->fileSize = $option->file->getSize();
81
82
        return $option;
83
    }
84
}
85