1 | <?php |
||
2 | |||
3 | namespace Thinktomorrow\AssetLibrary\Application; |
||
4 | |||
5 | use Thinktomorrow\AssetLibrary\Asset; |
||
6 | use Spatie\MediaLibrary\MediaCollections\Models\Media; |
||
7 | use Thinktomorrow\AssetLibrary\Exceptions\FileNotAccessibleException; |
||
8 | use Thinktomorrow\AssetLibrary\HasAsset; |
||
9 | |||
10 | class DeleteAsset |
||
11 | { |
||
12 | /** |
||
13 | * Removes an asset completely. |
||
14 | * |
||
15 | * @param $ids |
||
16 | 5 | */ |
|
17 | public function delete($ids): void |
||
18 | 5 | { |
|
19 | 2 | if (is_array($ids)) { |
|
20 | 2 | foreach ($ids as $id) { |
|
21 | self::remove($id); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
22 | } |
||
23 | 4 | } else { |
|
24 | 1 | if (! $ids) { |
|
25 | return; |
||
26 | 3 | } |
|
27 | self::remove($ids); |
||
28 | 4 | } |
|
29 | } |
||
30 | 5 | ||
31 | public function remove($id) |
||
32 | 5 | { |
|
33 | 1 | if (! $id) { |
|
34 | return false; |
||
35 | } |
||
36 | 4 | ||
37 | if (! $asset = Asset::find($id)) { |
||
38 | return false; |
||
39 | } |
||
40 | 4 | ||
41 | 4 | $media = $asset->media; |
|
42 | 4 | ||
43 | 4 | /** @var Media $file */ |
|
44 | foreach ($media as $file) { |
||
45 | |||
46 | $file_path = $file->getPath(); |
||
47 | 3 | ||
48 | 3 | if (! is_file($file_path) || ! is_writable($file_path)) { |
|
49 | throw new FileNotAccessibleException(); |
||
50 | } |
||
51 | } |
||
52 | |||
53 | $asset->delete(); |
||
54 | } |
||
55 | 1 | ||
56 | /** |
||
57 | 1 | * Removes all assets completely. |
|
58 | 1 | * |
|
59 | * @param $ids |
||
60 | */ |
||
61 | public function deleteAll(HasAsset $model): void |
||
62 | { |
||
63 | $model->assetRelation->each->delete(); |
||
0 ignored issues
–
show
|
|||
64 | } |
||
65 | } |
||
66 |