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

CatalogUpdater::update()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 61
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 40
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 34
c 0
b 0
f 0
dl 0
loc 61
ccs 40
cts 40
cp 1
rs 8.7537
cc 6
nc 6
nop 2
crap 6

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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