Completed
Push — master ( f67f4f...77dcfd )
by Curtis
40s queued 12s
created

Files::extensions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace App\Service\enso\files;
4
5
use Illuminate\Http\File;
6
use Illuminate\Http\UploadedFile;
7
use Illuminate\Support\Facades\DB;
8
use Illuminate\Support\Facades\Storage;
9
use Illuminate\Support\Str;
10
use App\Models\User;
11
use App\Contracts\enso\files\Attachable;
12
use Symfony\Component\HttpFoundation\File\File as BaseFile;
13
use LaravelEnso\Files\App\Services\FileValidator;
14
use LaravelEnso\Files\App\Services\UploadedFileValidator;
15
use LaravelEnso\Files\App\Services\ImageProcessor;
16
17
class Files
18
{
19
    private Attachable $attachable;
20
    private BaseFile $file;
21
    private string $disk;
22
    private array $extensions;
23
    private array $mimeTypes;
24
    private bool $optimize;
25
    private array $resize;
26
27
    public function __construct(Attachable $attachable)
28
    {
29
        $this->attachable = $attachable;
30
        $this->disk = config('filesystems.default');
31
        $this->extensions = [];
32
        $this->mimeTypes = [];
33
        $this->optimize = false;
34
        $this->resize = [];
35
    }
36
37
    public function inline()
38
    {
39
        return Storage::disk($this->disk)
40
            ->response($this->attachedFile());
41
    }
42
43
    public function download()
44
    {
45
        return Storage::disk($this->disk)
46
            ->download(
47
                $this->attachedFile(),
48
                Str::ascii($this->attachable->file->original_name)
0 ignored issues
show
Bug introduced by
Accessing file on the interface App\Contracts\enso\files\Attachable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
49
            );
50
    }
51
52
    public function delete()
53
    {
54
        if ($this->attachable->file) {
0 ignored issues
show
Bug introduced by
Accessing file on the interface App\Contracts\enso\files\Attachable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
55
            DB::transaction(function () {
56
                Storage::disk($this->disk)->delete($this->attachedFile());
57
                $this->attachable->file->delete();
0 ignored issues
show
Bug introduced by
Accessing file on the interface App\Contracts\enso\files\Attachable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
58
            });
59
        }
60
    }
61
62
    public function attach(File $file, string $originalName, ?User $user)
63
    {
64
        $this->file($file)
65
            ->validateFile()
66
            ->processImage()
67
            ->persistAttachedFile($originalName, $user);
68
    }
69
70
    public function upload(UploadedFile $file)
71
    {
72
        $this->file($file)
73
            ->validateUploadedFile()
74
            ->processImage()
75
            ->persistUploadedFile();
76
    }
77
78
    public function optimize($optimize)
79
    {
80
        $this->optimize = $optimize;
81
82
        return $this;
83
    }
84
85
    public function resize(array $resize)
86
    {
87
        $this->resize = $resize;
88
89
        return $this;
90
    }
91
92
    public function disk(string $disk)
93
    {
94
        $this->disk = $disk;
95
96
        return $this;
97
    }
98
99
    public function extensions(array $extensions)
100
    {
101
        $this->extensions = $extensions;
102
103
        return $this;
104
    }
105
106
    public function mimeTypes(array $mimeTypes)
107
    {
108
        $this->mimeTypes = $mimeTypes;
109
110
        return $this;
111
    }
112
113
    private function file(BaseFile $file)
114
    {
115
        $this->file = $file;
116
117
        return $this;
118
    }
119
120
    private function validateFile()
121
    {
122
        (new FileValidator(
123
            $this->file,
124
            $this->extensions,
125
            $this->mimeTypes
126
        ))->handle();
127
128
        return $this;
129
    }
130
131
    private function validateUploadedFile()
132
    {
133
        (new UploadedFileValidator(
134
            $this->file,
135
            $this->extensions,
136
            $this->mimeTypes
137
        ))->handle();
138
139
        return $this;
140
    }
141
142
    private function processImage()
143
    {
144
        (new ImageProcessor(
145
            $this->file,
146
            $this->optimize,
147
            $this->resize
148
        ))->handle();
149
150
        return $this;
151
    }
152
153
    private function persistAttachedFile(string $originalName, ?User $user)
154
    {
155
        $this->attachable->file()->create([
156
            'original_name' => $originalName,
157
            'saved_name' => $this->file->getBaseName(),
158
            'size' => $this->file->getSize(),
159
            'mime_type' => $this->file->getMimeType(),
160
            'created_by' => optional($user)->id,
161
        ]);
162
    }
163
164
    private function persistUploadedFile()
165
    {
166
        DB::transaction(function () {
167
            $this->attachable->file()->create([
168
                'original_name' => $this->file->getClientOriginalName(),
0 ignored issues
show
Bug introduced by
The method getClientOriginalName() does not exist on Symfony\Component\HttpFoundation\File\File. It seems like you code against a sub-type of Symfony\Component\HttpFoundation\File\File such as Symfony\Component\HttpFoundation\File\UploadedFile. ( Ignorable by Annotation )

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

168
                'original_name' => $this->file->/** @scrutinizer ignore-call */ getClientOriginalName(),
Loading history...
169
                'saved_name' => $this->file->hashName(),
0 ignored issues
show
Bug introduced by
The method hashName() does not exist on Symfony\Component\HttpFoundation\File\File. It seems like you code against a sub-type of Symfony\Component\HttpFoundation\File\File such as Illuminate\Http\File or Illuminate\Http\UploadedFile. ( Ignorable by Annotation )

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

169
                'saved_name' => $this->file->/** @scrutinizer ignore-call */ hashName(),
Loading history...
170
                'size' => $this->file->getSize(),
171
                'mime_type' => $this->file->getMimeType(),
172
            ]);
173
174
            $this->file->store(
0 ignored issues
show
Bug introduced by
The method store() does not exist on Symfony\Component\HttpFoundation\File\File. It seems like you code against a sub-type of Symfony\Component\HttpFoundation\File\File such as Illuminate\Http\UploadedFile. ( Ignorable by Annotation )

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

174
            $this->file->/** @scrutinizer ignore-call */ 
175
                         store(
Loading history...
175
                $this->attachable->folder(),
176
                ['disk' => $this->disk]
177
            );
178
        });
179
    }
180
181
    private function attachedFile()
182
    {
183
        return $this->attachable->folder()
184
            .DIRECTORY_SEPARATOR
185
            .$this->attachable->file->saved_name;
0 ignored issues
show
Bug introduced by
Accessing file on the interface App\Contracts\enso\files\Attachable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
186
    }
187
}
188