Completed
Push — master ( 9e9649...0ca17c )
by Freek
02:12
created

ClearCommand::getMediaItems()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
rs 8.6737
cc 5
eloc 12
nc 4
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 = '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,
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...TypeAndCollectionName() 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
                $collectionName
0 ignored issues
show
Bug introduced by
It seems like $collectionName defined by $this->argument('collectionName') on line 72 can also be of type array; however, Spatie\MediaLibrary\Medi...TypeAndCollectionName() 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...
78
            );
79
        }
80
81
        if (!is_null($modelType)) {
82
            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...
83
        }
84
85
        if (!is_null($collectionName)) {
86
            return $this->mediaRepository->getByCollectionName($collectionName);
0 ignored issues
show
Bug introduced by
It seems like $collectionName defined by $this->argument('collectionName') on line 72 can also be of type array; however, Spatie\MediaLibrary\Medi...::getByCollectionName() 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...
87
        }
88
89
        return $this->mediaRepository->all();
90
    }
91
}
92