41 lines
1.0 KiB
PHP
41 lines
1.0 KiB
PHP
<?php
|
|
|
|
namespace App\Service;
|
|
|
|
use App\Repository\AlbumRepository;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
class CatalogResetService
|
|
{
|
|
public function __construct(
|
|
private readonly EntityManagerInterface $entityManager,
|
|
private readonly AlbumRepository $albumRepository,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* Deletes all reviews and albums from the catalog and returns summary counts.
|
|
*
|
|
* @return array{albums:int,reviews:int}
|
|
*/
|
|
public function resetCatalog(): array
|
|
{
|
|
$deletedReviews = $this->entityManager->createQuery('DELETE FROM App\Entity\Review r')->execute();
|
|
|
|
$albums = $this->albumRepository->findAll();
|
|
$albumCount = count($albums);
|
|
foreach ($albums as $album) {
|
|
// Remove entities one-by-one so Doctrine cascades delete related tracks/reviews as configured.
|
|
$this->entityManager->remove($album);
|
|
}
|
|
$this->entityManager->flush();
|
|
|
|
return [
|
|
'albums' => $albumCount,
|
|
'reviews' => $deletedReviews,
|
|
];
|
|
}
|
|
}
|
|
|
|
|