Completed
Push — master ( 2896e1...c51be8 )
by Freek
03:08
created

RegenerateCommand::getMediaToBeRegenerated()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 3
nop 0
1
<?php
2
3
namespace Spatie\MediaLibrary\Commands;
4
5
use Exception;
6
use Illuminate\Console\Command;
7
use Illuminate\Support\Collection;
8
use Spatie\MediaLibrary\Models\Media;
9
use Illuminate\Console\ConfirmableTrait;
10
use Spatie\MediaLibrary\FileManipulator;
11
use Spatie\MediaLibrary\MediaRepository;
12
13
class RegenerateCommand extends Command
14
{
15
    use ConfirmableTrait;
16
17
    protected $signature = 'medialibrary:regenerate {modelType?} {--ids=*}
18
    {--only=* : Regenerate specific conversions}
19
    {--only-missing : Regenerate only missing conversions}
20
    {--force : Force the operation to run when in production}';
21
22
    protected $description = 'Regenerate the derived images of media';
23
24
    /** @var \Spatie\MediaLibrary\MediaRepository */
25
    protected $mediaRepository;
26
27
    /** @var \Spatie\MediaLibrary\FileManipulator */
28
    protected $fileManipulator;
29
30
    /** @var array */
31
    protected $erroredMediaIds = [];
32
33
    public function __construct(MediaRepository $mediaRepository, FileManipulator $fileManipulator)
34
    {
35
        parent::__construct();
36
37
        $this->mediaRepository = $mediaRepository;
38
        $this->fileManipulator = $fileManipulator;
39
    }
40
41
    public function handle()
42
    {
43
        if (!$this->confirmToProceed()) {
44
            return;
45
        }
46
47
        $mediaFiles = $this->getMediaToBeRegenerated();
48
49
        $progressBar = $this->output->createProgressBar($mediaFiles->count());
50
51
        $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...
52
53
        $mediaFiles->each(function (Media $media) use ($progressBar) {
54
            try {
55
                $this->fileManipulator->createDerivedFiles(
56
                    $media,
57
                    array_wrap($this->option('only')),
58
                    $this->option('only-missing')
0 ignored issues
show
Documentation introduced by
$this->option('only-missing') is of type string|array, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

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