Completed
Push — master ( 4b47aa...fa442a )
by Freek
02:18
created

RegenerateCommand::getMediaToBeRegenerated()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 20
rs 8.8571
cc 5
eloc 10
nc 4
nop 0
1
<?php
2
3
namespace Spatie\MediaLibrary\Commands;
4
5
use Illuminate\Console\Command;
6
use Spatie\{
7
    MediaLibrary\FileManipulator, 
8
    MediaLibrary\Media, 
9
    MediaLibrary\MediaRepository
10
};
11
12
class RegenerateCommand extends Command
13
{
14
    /**
15
     * The console command name.
16
     *
17
     * @var string
18
     */
19
    protected $signature = 'medialibrary:regenerate {modelType?} {--ids=*}';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Regenerate the derived images of media';
27
28
    /**
29
     * @var \Spatie\MediaLibrary\MediaRepository
30
     */
31
    protected $mediaRepository;
32
33
    /**
34
     * @var \Spatie\MediaLibrary\FileManipulator
35
     */
36
    protected $fileManipulator;
37
38
    /**
39
     * RegenerateCommand constructor.
40
     *
41
     * @param MediaRepository $mediaRepository
42
     * @param FileManipulator $fileManipulator
43
     */
44
    public function __construct(MediaRepository $mediaRepository, FileManipulator $fileManipulator)
45
    {
46
        parent::__construct();
47
48
        $this->mediaRepository = $mediaRepository;
49
        $this->fileManipulator = $fileManipulator;
50
    }
51
52
    /**
53
     * Handle regeneration.
54
     */
55
    public function handle()
56
    {
57
        $this->getMediaToBeRegenerated()->map(function (Media $media) {
58
            $this->fileManipulator->createDerivedFiles($media);
59
            $this->info(sprintf('Media %s regenerated', $media->id));
60
        });
61
62
        $this->info('All done!');
63
    }
64
65
    /**
66
     * @return \Illuminate\Database\Eloquent\Collection
67
     */
68
    public function getMediaToBeRegenerated()
69
    {
70
        $modelType = $this->argument('modelType') ?? '';
71
        $mediaIds = $this->option('ids');
72
73
        if ($modelType === '' && !$mediaIds) {
74
            return $this->mediaRepository->all();
75
        }
76
77
        if ($mediaIds) {
78
79
            if (! is_array($mediaIds)) {
80
                $mediaIds = explode(',', $mediaIds);
81
            }
82
83
            return $this->mediaRepository->getByIds($mediaIds);
84
        }
85
86
        return $this->mediaRepository->getByModelType($modelType);
0 ignored issues
show
Bug introduced by
It seems like $modelType defined by $this->argument('modelType') ?? '' on line 70 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...
87
    }
88
}
89