Completed
Push — master ( 8852f1...9e9649 )
by Freek
05:07
created

ClearCommand::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
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 = 'clear medias';
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->getMedias()->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 getMedias()
70
    {
71
        if (($modelType = $this->argument('modelType')) == null) {
72
            return $this->mediaRepository->all();
73
        }
74
75
        if (($collectionName = $this->argument('collectionName')) == null) {
76
            return $this->mediaRepository->getByModelType($modelType);
0 ignored issues
show
Bug introduced by
It seems like $modelType defined by $this->argument('modelType') on line 71 can also be of type array; however, Spatie\MediaLibrary\Medi...itory::getByModelType() does only seem to accept string, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
77
        }
78
79
        return $this->mediaRepository->getByModelTypeAndCollectionName($modelType, $collectionName);
80
    }
81
}
82