Passed
Push — main ( 7dcca1...e70bce )
by Daniel
04:22
created

CatalogUpdater::update()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 67
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 43
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 37
c 0
b 0
f 0
dl 0
loc 67
ccs 43
cts 43
cp 1
rs 8.7057
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 DateTime;
9
use Uxmp\Core\Component\Catalog\Manage\Update\AudioFileRetrieverInterface;
10
use Uxmp\Core\Component\Catalog\Manage\Update\RecursiveFileReaderInterface;
11
use Uxmp\Core\Component\Catalog\Scanner\DiscCacheInterface;
12
use Uxmp\Core\Component\Tag\Container\AudioFileInterface;
13
use Uxmp\Core\Orm\Repository\CatalogRepositoryInterface;
14
use Uxmp\Core\Orm\Repository\SongRepositoryInterface;
15
16
/**
17
 * Updates the media files of a catalog
18
 */
19
final readonly class CatalogUpdater implements CatalogUpdaterInterface
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_READONLY, expecting T_CLASS on line 19 at column 6
Loading history...
20
{
21 3
    public function __construct(
22
        private CatalogRepositoryInterface $catalogRepository,
23
        private SongRepositoryInterface $songRepository,
24
        private DiscCacheInterface $discCache,
25
        private RecursiveFileReaderInterface $recursiveFileReader,
26
        private AudioFileRetrieverInterface $audioFileRetriever,
27
    ) {
28 3
    }
29
30 3
    public function update(Interactor $io, int $catalogId): void
31
    {
32 3
        $catalog = $this->catalogRepository->find($catalogId);
33 3
        if ($catalog === null) {
34 1
            $io->error(
35 1
                sprintf('Catalog `%d` not found', $catalogId),
36 1
                true
37 1
            );
38 1
            return;
39
        }
40
41 2
        $directory = $catalog->getPath();
42
43 2
        if (!is_dir($directory)) {
44 1
            $io->error(
45 1
                sprintf('The path `%s` is not accessible', $directory),
46 1
                true
47 1
            );
48 1
            return;
49
        }
50
51 1
        $io->info(
52 1
            sprintf('Updating catalog from `%s`', $directory),
53 1
            true
54 1
        );
55
56 1
        $reader = $this->audioFileRetriever->retrieve(
57 1
            $this->recursiveFileReader->read($directory),
58 1
            $catalog->getLastUpdated()
59 1
        );
60
61
        // set the last-update date at the beginning
62 1
        $catalog->setLastUpdated(new DateTime());
63
64 1
        foreach ($reader as $audioFile) {
65
            /** @var AudioFileInterface|null $audioFile */
66 1
            if ($audioFile === null) {
67 1
                $io->error('.');
68 1
                continue;
69
            }
70
71 1
            $io->info('.');
72
73 1
            $song = $this->songRepository->findByMbId($audioFile->getMbid());
74 1
            if ($song === null) {
75 1
                $song = $this->songRepository->prototype();
76
            }
77
78 1
            $disc = $this->discCache->retrieve($catalog, $audioFile);
79
80 1
            $song
81 1
                ->setDisc($disc)
82 1
                ->setArtist($disc->getAlbum()->getArtist())
83 1
                ->setCatalog($catalog)
84 1
            ;
85
86
            // apply the audio-file state onto the song
87 1
            $audioFile->apply($song);
88
89 1
            $this->songRepository->save($song);
90
91 1
            $disc->addSong($song);
92
        }
93
94 1
        $this->catalogRepository->save($catalog);
95
96 1
        $io->eol();
97
    }
98
}
99