1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mostafaznv\Larupload\Actions\Cover; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\UploadedFile; |
6
|
|
|
use Illuminate\Support\Facades\Storage; |
7
|
|
|
use Mostafaznv\Larupload\Actions\GuessLaruploadFileTypeAction; |
8
|
|
|
use Mostafaznv\Larupload\DTOs\CoverActionData; |
9
|
|
|
|
10
|
|
|
class SetCoverAction |
11
|
|
|
{ |
12
|
|
|
public function __construct( |
13
|
|
|
private readonly ?UploadedFile $file, |
14
|
|
|
private readonly mixed $cover, |
15
|
|
|
private readonly CoverActionData $data |
16
|
|
|
) {} |
17
|
|
|
|
18
|
|
|
public static function make(?UploadedFile $file, mixed $cover, CoverActionData $data): static |
19
|
|
|
{ |
20
|
|
|
return new static($file, $cover, $data); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
public function run(string $path): array |
25
|
|
|
{ |
26
|
|
|
Storage::disk($this->data->disk)->deleteDirectory($path); |
27
|
|
|
|
28
|
|
|
$output = $this->data->output; |
29
|
|
|
|
30
|
|
|
if ($this->shouldDeleteCover()) { |
31
|
|
|
$output = DeleteCoverAction::make($this->data->type, $output)->run(); |
32
|
|
|
} |
33
|
|
|
else if ($this->shouldUploadCover()) { |
34
|
|
|
$output = UploadCoverAction::make($this->cover, $this->data)->run($path); |
35
|
|
|
} |
36
|
|
|
else if ($this->data->generateCover) { |
37
|
|
|
$output = GenerateCoverFromFileAction::make($this->file, $this->data)->run($path); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $output; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private function shouldDeleteCover(): bool |
44
|
|
|
{ |
45
|
|
|
return isset($this->cover) and $this->cover == LARUPLOAD_NULL; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
private function shouldUploadCover(): bool |
49
|
|
|
{ |
50
|
|
|
return file_has_value($this->cover) and GuessLaruploadFileTypeAction::make($this->cover)->isImage(); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|