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

CatalogCleaner::deleteAlbum()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 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