Skip to content

Commit

Permalink
Add collection name option, fix phpstan, add extra test
Browse files Browse the repository at this point in the history
  • Loading branch information
mbardelmeijer committed Oct 24, 2023
1 parent f6a74f2 commit fb6c24f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/MediaCollections/Commands/CleanCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function getMediaItems(): Collection

protected function deleteOrphanedMediaItems(): void
{
$this->mediaRepository->getOrphans()->each(function (Media $media) {
$this->getOrphanedMediaItems()->each(function (Media $media): void {
if ($this->isDryRun) {
$this->info("Orphaned Media[id={$media->id}] found");

Expand All @@ -107,6 +107,18 @@ protected function deleteOrphanedMediaItems(): void
});
}

/** @return Collection<int, Media> */
protected function getOrphanedMediaItems(): Collection
{
$collectionName = $this->argument('collectionName');

if (is_string($collectionName)) {
return $this->mediaRepository->getOrphansByCollectionName($collectionName);
}

return $this->mediaRepository->getOrphans();
}

protected function deleteFilesGeneratedForDeprecatedConversions(): void
{
$this->getMediaItems()->each(function (Media $media) {
Expand Down
8 changes: 8 additions & 0 deletions src/MediaCollections/MediaRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ public function getOrphans(): DbCollection
->get();
}

public function getOrphansByCollectionName(string $collectionName): DbCollection
{
return $this->query()
->whereDoesntHave('model')
->where('collection_name', $collectionName)
->get();
}

protected function query(): Builder
{
return $this->model->newQuery();
Expand Down
31 changes: 31 additions & 0 deletions tests/Conversions/Commands/CleanCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,37 @@
]);
});

it('can clean orphaned media items when enabled for specific collections', function () {
$mediaToClean = TestModel::create(['name' => 'test.jpg'])
->addMedia($this->getTestJpg())
->preservingOriginal()
->toMediaCollection('collection-to-clean');

$mediaToKeep = TestModel::create(['name' => 'test.jpg'])
->addMedia($this->getTestJpg())
->preservingOriginal()
->toMediaCollection('collection-to-keep');

// Delete quietly to avoid deleting the related media file.
$mediaToClean->model->deleteQuietly();
$mediaToKeep->model->deleteQuietly();

$this->artisan('media-library:clean', [
'--delete-orphaned' => 'true',
'collectionName' => 'collection-to-clean',
]);

// Media should be deleted from the database.
$this->assertDatabaseMissing('media', [
'id' => $mediaToClean->id,
]);

// This media should still exist.
$this->assertDatabaseHas('media', [
'id' => $mediaToKeep->id,
]);
});

it('can won\'t clean orphaned media items when disabled', function () {
$media = TestModel::create(['name' => 'test.jpg'])
->addMedia($this->getTestJpg())
Expand Down

0 comments on commit fb6c24f

Please sign in to comment.