Passed
Push — main ( e933de...5ef56a )
by Daniel
03:35
created

CatalogUpdater   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B update() 0 61 6
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\Catalog\Manage\Update\AudioFileRetrieverInterface;
9
use Uxmp\Core\Component\Catalog\Manage\Update\RecursiveFileReaderInterface;
10
use Uxmp\Core\Component\Catalog\Scanner\DiscCacheInterface;
11
use Uxmp\Core\Component\Tag\Container\AudioFileInterface;
12
use Uxmp\Core\Orm\Repository\CatalogRepositoryInterface;
13
use Uxmp\Core\Orm\Repository\SongRepositoryInterface;
14
15
/**
16
 * Updates the media files of a catalog
17
 */
18
final class CatalogUpdater implements CatalogUpdaterInterface
19
{
20 3
    public function __construct(
21
        private readonly CatalogRepositoryInterface $catalogRepository,
22
        private readonly SongRepositoryInterface $songRepository,
23
        private readonly DiscCacheInterface $discCache,
24
        private readonly RecursiveFileReaderInterface $recursiveFileReader,
25
        private readonly AudioFileRetrieverInterface $audioFileRetriever,
26
    ) {
27 3
    }
28
29 3
    public function update(Interactor $io, int $catalogId): void
30
    {
31 3
        $catalog = $this->catalogRepository->find($catalogId);
32 3
        if ($catalog === null) {
33 1
            $io->error(
34 1
                sprintf('Catalog `%d` not found', $catalogId),
35 1
                true
36 1
            );
37 1
            return;
38
        }
39
40 2
        $directory = $catalog->getPath();
41
42 2
        if (!is_dir($directory)) {
43 1
            $io->error(
44 1
                sprintf('The path `%s` is not accessible', $directory),
45 1
                true
46 1
            );
47 1
            return;
48
        }
49
50 1
        $io->info(
51 1
            sprintf('Updating catalog from `%s`', $directory),
52 1
            true
53 1
        );
54
55 1
        $reader = $this->audioFileRetriever->retrieve(
56 1
            $this->recursiveFileReader->read($directory)
57 1
        );
58
59 1
        foreach ($reader as $audioFile) {
60
            /** @var AudioFileInterface|null $audioFile */
61 1
            if ($audioFile === null) {
62 1
                $io->error('.');
63 1
                continue;
64
            }
65
66 1
            $io->info('.');
67
68 1
            $song = $this->songRepository->findByMbId($audioFile->getMbid());
69 1
            if ($song === null) {
70 1
                $song = $this->songRepository->prototype();
71
            }
72
73 1
            $disc = $this->discCache->retrieve($catalog, $audioFile);
74
75 1
            $song
76 1
                ->setDisc($disc)
77 1
                ->setArtist($disc->getAlbum()->getArtist())
78 1
                ->setCatalog($catalog)
79 1
            ;
80
81
            // apply the audiofile state onto the song
82 1
            $audioFile->apply($song);
83
84 1
            $this->songRepository->save($song);
85
86 1
            $disc->addSong($song);
87
        }
88
89 1
        $io->eol();
90
    }
91
}
92