1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\MediaLibrary\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Console\ConfirmableTrait; |
7
|
|
|
use Spatie\MediaLibrary\FileManipulator; |
8
|
|
|
use Spatie\MediaLibrary\Media; |
9
|
|
|
use Spatie\MediaLibrary\MediaRepository; |
10
|
|
|
|
11
|
|
|
class ClearCommand extends Command |
12
|
|
|
{ |
13
|
|
|
use ConfirmableTrait; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The console command name. |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $signature = 'medialibrary:clear {modelType?} {collectionName?}'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The console command description. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $description = 'Delete all items in a media collection.'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \Spatie\MediaLibrary\MediaRepository |
31
|
|
|
*/ |
32
|
|
|
protected $mediaRepository; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var \Spatie\MediaLibrary\FileManipulator |
36
|
|
|
*/ |
37
|
|
|
protected $fileManipulator; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param MediaRepository $mediaRepository |
41
|
|
|
* @param FileManipulator $fileManipulator |
42
|
|
|
*/ |
43
|
|
|
public function __construct(MediaRepository $mediaRepository, FileManipulator $fileManipulator) |
44
|
|
|
{ |
45
|
|
|
parent::__construct(); |
46
|
|
|
$this->mediaRepository = $mediaRepository; |
47
|
|
|
$this->fileManipulator = $fileManipulator; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Handle command. |
52
|
|
|
*/ |
53
|
|
|
public function handle() |
54
|
|
|
{ |
55
|
|
|
if (!$this->confirmToProceed()) { |
56
|
|
|
return; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->getMediaItems()->each(function (Media $media) { |
60
|
|
|
$media->delete(); |
61
|
|
|
}); |
62
|
|
|
|
63
|
|
|
$this->info('All done!'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
68
|
|
|
*/ |
69
|
|
|
public function getMediaItems() |
70
|
|
|
{ |
71
|
|
|
$modelType = $this->argument('modelType'); |
72
|
|
|
$collectionName = $this->argument('collectionName'); |
73
|
|
|
|
74
|
|
|
if (!is_null($modelType) && !is_null($collectionName)) { |
75
|
|
|
return $this->mediaRepository->getByModelTypeAndCollectionName( |
76
|
|
|
$modelType, |
|
|
|
|
77
|
|
|
$collectionName |
|
|
|
|
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (!is_null($modelType)) { |
82
|
|
|
return $this->mediaRepository->getByModelType($modelType); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if (!is_null($collectionName)) { |
86
|
|
|
return $this->mediaRepository->getByCollectionName($collectionName); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $this->mediaRepository->all(); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.