Completed
Push — master ( c795de...c72eae )
by Freek
02:46 queued 31s
created

RegenerateCommand::handle()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 22
rs 8.9197
c 1
b 0
f 1
cc 4
eloc 13
nc 3
nop 0
1
<?php
2
3
namespace Spatie\MediaLibrary\Commands;
4
5
use Exception;
6
use Illuminate\Console\Command;
7
use Illuminate\Console\ConfirmableTrait;
8
use Illuminate\Support\Collection;
9
use Spatie\{
10
    MediaLibrary\FileManipulator,
11
    MediaLibrary\Media,
12
    MediaLibrary\MediaRepository
13
};
14
15
class RegenerateCommand extends Command
16
{
17
    use ConfirmableTrait;
18
19
    /**
20
     * The console command name.
21
     *
22
     * @var string
23
     */
24
    protected $signature = 'medialibrary:regenerate {modelType?} {--ids=*}
25
    {-- force : Force the operation to run when in production}';
26
27
    /**
28
     * The console command description.
29
     *
30
     * @var string
31
     */
32
    protected $description = 'Regenerate the derived images of media';
33
34
    /**
35
     * @var \Spatie\MediaLibrary\MediaRepository
36
     */
37
    protected $mediaRepository;
38
39
    /**
40
     * @var \Spatie\MediaLibrary\FileManipulator
41
     */
42
    protected $fileManipulator;
43
44
    /**
45
     * @var array
46
     */
47
    protected $erroredMediaIds = [];
48
49
    /**
50
     * RegenerateCommand constructor.
51
     *
52
     * @param MediaRepository $mediaRepository
53
     * @param FileManipulator $fileManipulator
54
     */
55
    public function __construct(MediaRepository $mediaRepository, FileManipulator $fileManipulator)
56
    {
57
        parent::__construct();
58
59
        $this->mediaRepository = $mediaRepository;
60
        $this->fileManipulator = $fileManipulator;
61
    }
62
63
    /**
64
     * Handle regeneration.
65
     */
66
    public function handle()
67
    {
68
        if (!$this->confirmToProceed()) {
69
            return;
70
        }
71
72
        $this->getMediaToBeRegenerated()->each(function (Media $media) {
73
            try {
74
                $this->fileManipulator->createDerivedFiles($media);
75
                $this->info("Media {$media->id} regenerated");
76
            } catch (Exception $exception) {
77
                $this->error("Media {$media->id} could not be regenerated because `{$exception->getMessage()}`");
78
                $this->erroredMediaIds[] = $media->id;
79
            }
80
        });
81
82
        if (count($this->erroredMediaIds)) {
83
            $this->warn('The derived files of these media ids could not be regenerated: ' . implode(',', $this->erroredMediaIds));
84
        }
85
86
        $this->info('All done!');
87
    }
88
89
    public function getMediaToBeRegenerated(): Collection
90
    {
91
        $modelType = $this->argument('modelType') ?? '';
92
        $mediaIds = $this->option('ids');
93
94
        if ($modelType === '' && !$mediaIds) {
95
            return $this->mediaRepository->all();
96
        }
97
98
        if ($mediaIds) {
99
100
            if (!is_array($mediaIds)) {
101
                $mediaIds = explode(',', $mediaIds);
102
            }
103
104
            return $this->mediaRepository->getByIds($mediaIds);
105
        }
106
107
        return $this->mediaRepository->getByModelType($modelType);
0 ignored issues
show
Bug introduced by
It seems like $modelType defined by $this->argument('modelType') ?? '' on line 91 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...
108
    }
109
}
110