Passed
Push — main ( 97bfd7...43e821 )
by Daniel
04:18
created

CatalogCleaner   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
B clean() 0 60 8
A deleteAlbum() 0 8 1
A __construct() 0 8 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\Component\Song\SongDeleterInterface;
10
use Uxmp\Core\Orm\Model\AlbumInterface;
11
use Uxmp\Core\Orm\Repository\AlbumRepositoryInterface;
12
use Uxmp\Core\Orm\Repository\CatalogRepositoryInterface;
13
use Uxmp\Core\Orm\Repository\DiscRepositoryInterface;
14
use Uxmp\Core\Orm\Repository\SongRepositoryInterface;
15
16
final class CatalogCleaner implements CatalogCleanerInterface
17
{
18 3
    public function __construct(
19
        private CatalogRepositoryInterface $catalogRepository,
20
        private AlbumDeleterInterface $albumDeleter,
21
        private SongRepositoryInterface $songRepository,
22
        private SongDeleterInterface $songDeleter,
23
        private DiscRepositoryInterface $discRepository,
24
        private AlbumRepositoryInterface $albumRepository,
25
    ) {
26 3
    }
27
28 3
    public function clean(Interactor $io, int $catalogId): void
29
    {
30 3
        $catalog = $this->catalogRepository->find($catalogId);
31 3
        if ($catalog === null) {
32 1
            $io->error(
33 1
                sprintf('Catalog `%d` not found', $catalogId),
34 1
                true
35
            );
36 1
            return;
37
        }
38
39 2
        $directory = $catalog->getPath();
40
41 2
        if (!is_dir($directory)) {
42 1
            $io->error(
43 1
                sprintf('The path `%s` is not accessible', $directory),
44 1
                true
45
            );
46 1
            return;
47
        }
48
49 1
        $io->info('Cleaning catalog', true);
50
51 1
        $songs = $this->songRepository->findBy(['catalog' => $catalog]);
52
53 1
        foreach ($songs as $song) {
54 1
            if (!file_exists($song->getFilename())) {
55 1
                $io->info(
56 1
                    sprintf('Delete `%s - %s`', $song->getArtist()->getTitle(), $song->getTitle()),
57 1
                    true
58
                );
59
60 1
                $this->songDeleter->delete($song);
61
            }
62
        }
63
64 1
        $discs = $this->discRepository->findEmptyDiscs($catalog);
65
66 1
        foreach ($discs as $disc) {
67 1
            $album = $disc->getAlbum();
68
69 1
            $io->info(
70 1
                sprintf('Delete disc number `%d` of `%s`', $disc->getNumber(), $album->getTitle()),
71 1
                true
72
            );
73
74 1
            $this->discRepository->delete($disc);
75
76 1
            if ($album->getDiscCount() === 0) {
77 1
                $this->deleteAlbum($album, $io);
78
            }
79
        }
80
81 1
        $albums = $this->albumRepository->findEmptyAlbums($catalog);
82
83 1
        foreach ($albums as $album) {
84 1
            $this->deleteAlbum($album, $io);
85
        }
86
87 1
        $io->ok('Done', true);
88 1
    }
89
90 1
    private function deleteAlbum(AlbumInterface $album, Interactor $io): void
91
    {
92 1
        $io->info(
93 1
            sprintf('Delete orphaned album `%s`', $album->getTitle()),
94 1
            true
95
        );
96
97 1
        $this->albumDeleter->delete($album);
98 1
    }
99
}
100