Passed
Push — main ( 48955e...53f2e5 )
by Daniel
04:08
created

CatalogDeleter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 16
c 1
b 0
f 0
dl 0
loc 38
ccs 20
cts 20
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A delete() 0 29 3
A __construct() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uxmp\Core\Component\Catalog\Manage;
6
7
use Ahc\Cli\IO\Interactor;
8
use Uxmp\Core\Component\Album\AlbumDeleterInterface;
9
use Uxmp\Core\Orm\Repository\AlbumRepositoryInterface;
10
use Uxmp\Core\Orm\Repository\CatalogRepositoryInterface;
11
12
/**
13
 * Deletes a catalog including all references
14
 */
15
final class CatalogDeleter implements CatalogDeleterInterface
16
{
17 2
    public function __construct(
18
        private readonly CatalogRepositoryInterface $catalogRepository,
19
        private readonly AlbumRepositoryInterface $albumRepository,
20
        private readonly AlbumDeleterInterface $albumDeleter,
21
    ) {
22 2
    }
23
24 2
    public function delete(
25
        Interactor $io,
26
        int $catalogId
27
    ): void {
28 2
        $catalog = $this->catalogRepository->find($catalogId);
29 2
        if ($catalog === null) {
30 1
            $io->error(
31 1
                sprintf('Catalog `%d` not found', $catalogId),
32 1
                true
33 1
            );
34 1
            return;
35
        }
36
37 1
        $io->info('Cleaning catalog', true);
38
39 1
        $albums = $this->albumRepository->findBy(['catalog' => $catalog]);
40
41 1
        foreach ($albums as $album) {
42 1
            $io->info(
43 1
                sprintf('Deleting album `%s`', $album->getTitle()),
44 1
                true
45 1
            );
46
47 1
            $this->albumDeleter->delete($album);
48
        }
49
50 1
        $this->catalogRepository->delete($catalog);
51
52 1
        $io->ok('Done', true);
53
    }
54
}
55