Passed
Push — main ( 4d334c...0416fa )
by Seth
12:13 queued 10:38
created

FileManager::upload()   B

Complexity

Conditions 8
Paths 2

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 21
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 30
rs 8.4444
1
<?php
2
3
namespace SaasReady\Services\FileManager;
4
5
use Carbon\Carbon;
0 ignored issues
show
Bug introduced by
The type Carbon\Carbon 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\Support\Facades\Storage;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Facades\Storage 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 Illuminate\Support\Str;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Str 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...
8
use SaasReady\Models\File;
9
10
class FileManager
11
{
12
    /**
13
     * Upload the file
14
     */
15
    public function upload(UploadOption $uploadOption): ?File
16
    {
17
        $driver = $uploadOption->driver ?: Storage::getDefaultDriver();
18
        $storage = Storage::disk($driver);
19
20
        $file = $uploadOption->file;
21
        $newFileName = $uploadOption->newFileName ?: Str::orderedUuid() . '.' . $file->getExtension();
22
        $storagePath = $uploadOption->storePath ?: 'files/';
23
24
        // upload
25
        $uploadResult = $storage->putFileAs(
26
            $storagePath,
27
            $file,
28
            $newFileName
29
        );
30
31
        if ($uploadResult === false) {
32
            return null;
33
        }
34
35
        return File::create([
36
            'category' => $uploadOption->category,
37
            'model_id' => $uploadOption->source?->getKey(),
38
            'model_type' => $uploadOption->source?->getMorphClass(),
39
            'mime_type' => $uploadOption->fileMimeType ?: $file->getType(),
40
            'path' => $storagePath . $newFileName,
41
            'filename' => $newFileName,
42
            'original_filename' => $uploadOption->originalFileName ?: '',
43
            'size' => $uploadOption->fileSize ?: $file->getSize(),
44
            'source' => $driver,
45
        ]);
46
    }
47
48
    /**
49
     * Get the URL of the File
50
     */
51
    public function getUrl(File $file): ?string
52
    {
53
        $disk = $file->getFilesystemDisk();
54
        if (!$disk->exists($file->path)) {
55
            return null;
56
        }
57
58
        return $disk->url($file->path);
59
    }
60
61
    /**
62
     * Get the URL of the File
63
     *
64
     * Note: only s3 is supporting this
65
     */
66
    public function getTemporaryUrl(File $file, Carbon $expiredAt): ?string
67
    {
68
        $disk = $file->getFilesystemDisk();
69
        if (!$disk->exists($file->path)) {
70
            return null;
71
        }
72
73
        return $disk->temporaryUrl($file->path, $expiredAt);
74
    }
75
}
76