Passed
Push — main ( 1d1c5b...83bc52 )
by Daniel
04:23
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 35
cts 35
cp 1
rs 10
c 1
b 0
f 0
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 readonly CatalogRepositoryInterface $catalogRepository,
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting T_VARIABLE on line 19 at column 25
Loading history...
20
        private readonly AlbumDeleterInterface $albumDeleter,
21
        private readonly SongRepositoryInterface $songRepository,
22
        private readonly SongDeleterInterface $songDeleter,
23
        private readonly DiscRepositoryInterface $discRepository,
24
        private readonly AlbumRepositoryInterface $albumRepository,
25
    ) {
26
    }
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
                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
                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
                    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
                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
    }
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
            true
95
        );
96
97 1
        $this->albumDeleter->delete($album);
98
    }
99
}
100