RegenerateCommand::getMediaIds()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 4
nc 4
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\Arr;
9
use Illuminate\Support\Collection;
10
use Illuminate\Support\Str;
11
use Spatie\MediaLibrary\FileManipulator;
12
use Spatie\MediaLibrary\MediaRepository;
13
use Spatie\MediaLibrary\Models\Media;
14
15
class RegenerateCommand extends Command
16
{
17
    use ConfirmableTrait;
18
19
    protected $signature = 'medialibrary:regenerate {modelType?} {--ids=*}
20
    {--only=* : Regenerate specific conversions}
21
    {--only-missing : Regenerate only missing conversions}
22
    {--force : Force the operation to run when in production}';
23
24
    protected $description = 'Regenerate the derived images of media';
25
26
    /** @var \Spatie\MediaLibrary\MediaRepository */
27
    protected $mediaRepository;
28
29
    /** @var \Spatie\MediaLibrary\FileManipulator */
30
    protected $fileManipulator;
31
32
    /** @var array */
33
    protected $erroredMediaIds = [];
34
35
    public function __construct(MediaRepository $mediaRepository, FileManipulator $fileManipulator)
36
    {
37
        parent::__construct();
38
39
        $this->mediaRepository = $mediaRepository;
40
        $this->fileManipulator = $fileManipulator;
41
    }
42
43
    public function handle()
44
    {
45
        if (! $this->confirmToProceed()) {
46
            return;
47
        }
48
49
        $mediaFiles = $this->getMediaToBeRegenerated();
50
51
        $progressBar = $this->output->createProgressBar($mediaFiles->count());
52
53
        $this->errorMessages = [];
0 ignored issues
show
Bug introduced by
The property errorMessages does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
54
55
        $mediaFiles->each(function (Media $media) use ($progressBar) {
56
            try {
57
                $this->fileManipulator->createDerivedFiles(
58
                    $media,
59
                    Arr::wrap($this->option('only')),
60
                    $this->option('only-missing')
61
                );
62
            } catch (Exception $exception) {
63
                $this->errorMessages[$media->getKey()] = $exception->getMessage();
64
            }
65
66
            $progressBar->advance();
67
        });
68
69
        $progressBar->finish();
70
71
        if (count($this->errorMessages)) {
72
            $this->warn('All done, but with some error messages:');
73
74
            foreach ($this->errorMessages as $mediaId => $message) {
75
                $this->warn("Media id {$mediaId}: `{$message}`");
76
            }
77
        }
78
79
        $this->info('All done!');
80
    }
81
82
    public function getMediaToBeRegenerated(): Collection
83
    {
84
        $modelType = $this->argument('modelType') ?? '';
85
        $mediaIds = $this->getMediaIds();
86
87
        if ($modelType === '' && count($mediaIds) === 0) {
88
            return $this->mediaRepository->all();
89
        }
90
91
        if (! count($mediaIds)) {
92
            return $this->mediaRepository->getByModelType($modelType);
0 ignored issues
show
Bug introduced by
It seems like $modelType defined by $this->argument('modelType') ?? '' on line 84 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...
93
        }
94
95
        return $this->mediaRepository->getByIds($mediaIds);
96
    }
97
98
    protected function getMediaIds(): array
99
    {
100
        $mediaIds = $this->option('ids');
101
102
        if (! is_array($mediaIds)) {
103
            $mediaIds = explode(',', $mediaIds);
104
        }
105
106
        if (count($mediaIds) === 1 && Str::contains($mediaIds[0], ',')) {
107
            $mediaIds = explode(',', $mediaIds[0]);
108
        }
109
110
        return $mediaIds;
111
    }
112
}
113