Passed
Push — main ( 33c779...46c527 )
by Daniel
04:17
created

CatalogUpdater::update()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 62
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 39
c 0
b 0
f 0
nc 6
nop 2
dl 0
loc 62
ccs 0
cts 37
cp 0
crap 42
rs 8.6737

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
final class CatalogUpdater implements CatalogUpdaterInterface
16
{
17
    public function __construct(
18
        private readonly CatalogRepositoryInterface $catalogRepository,
19
        private readonly SongRepositoryInterface $songRepository,
20
        private readonly DiscCacheInterface $discCache,
21
        private readonly RecursiveFileReaderInterface $recursiveFileReader,
22
        private readonly AudioFileRetrieverInterface $audioFileRetriever,
23
    ) {
24
    }
25
26
    public function update(Interactor $io, int $catalogId): void
27
    {
28
        $catalog = $this->catalogRepository->find($catalogId);
29
        if ($catalog === null) {
30
            $io->error(
31
                sprintf('Catalog `%d` not found', $catalogId),
32
                true
33
            );
34
            return;
35
        }
36
37
        $directory = $catalog->getPath();
38
39
        if (!is_dir($directory)) {
40
            $io->error(
41
                sprintf('The path `%s` is not accessible', $directory),
42
                true
43
            );
44
            return;
45
        }
46
47
        $io->info(sprintf('Updating catalog from `%s`', $directory), true);
48
49
        $reader = $this->audioFileRetriever->retrieve(
50
            $this->recursiveFileReader->read($directory)
51
        );
52
53
        foreach ($reader as $audioFile) {
54
            /** @var AudioFileInterface|null $audioFile */
55
            if ($audioFile === null) {
56
                $io->error('.');
57
                continue;
58
            }
59
            $io->info('.');
60
61
            $song = $this->songRepository->findByMbId($audioFile->getMbid());
62
            if ($song === null) {
63
                $song = $this->songRepository->prototype();
64
            }
65
66
            $disc = $this->discCache->retrieve($catalog, $audioFile);
67
68
            $song
69
                ->setTitle($audioFile->getTitle())
70
                ->setTrackNumber($audioFile->getTrackNumber())
71
                ->setDisc($disc)
72
                ->setArtist($disc->getAlbum()->getArtist())
73
                ->setFilename($audioFile->getFilename())
74
                ->setMbid($audioFile->getMbid())
75
                ->setCatalog($catalog)
76
                ->setLength($audioFile->getLength())
77
                ->setYear($audioFile->getYear())
78
                ->setMimeType($audioFile->getMimeType())
79
                ->setFileSize($audioFile->getFileSize())
80
            ;
81
82
            $this->songRepository->save($song);
83
84
            $disc->addSong($song);
85
        }
86
87
        $io->eol();
88
    }
89
}
90