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

GenreCache::enrich()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 51
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 30
c 1
b 0
f 1
dl 0
loc 51
ccs 32
cts 32
cp 1
rs 8.8177
cc 6
nc 10
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\Scanner;
6
7
use Uxmp\Core\Component\Tag\Container\AudioFileInterface;
8
use Uxmp\Core\Orm\Model\AlbumInterface;
9
use Uxmp\Core\Orm\Model\GenreMapEnum;
10
use Uxmp\Core\Orm\Repository\GenreMapRepositoryInterface;
11
use Uxmp\Core\Orm\Repository\GenreRepositoryInterface;
12
13
final class GenreCache implements GenreCacheInterface
14
{
15 4
    public function __construct(
16
        private readonly GenreRepositoryInterface $genreRepository,
17
        private readonly GenreMapRepositoryInterface $genreMapRepository,
18
    ) {
19 4
    }
20
21 4
    public function enrich(
22
        AlbumInterface $album,
23
        AudioFileInterface $audioFile,
24
    ): void {
25 4
        $genres = $audioFile->getGenres();
26
27 4
        if ($genres === []) {
28 1
            return;
29
        }
30
31 3
        $genres = array_map('ucwords', $genres);
32
33 3
        $albumId = $album->getId();
34
35 3
        $knownGenres = $this->genreMapRepository->findBy([
36 3
            'mapped_item_type' => GenreMapEnum::ALBUM,
37 3
            'mapped_item_id' => $albumId,
38 3
        ]);
39
40 3
        foreach ($knownGenres as $knownGenre) {
41 2
            $genreName = $knownGenre->getGenreTitle();
42 2
            $key = array_search($genreName, $genres, true);
43 2
            if ($key !== false) {
44 2
                unset($genres[$key]);
45
            } else {
46 1
                $this->genreMapRepository->delete($knownGenre);
47
            }
48
        }
49
50 3
        foreach ($genres as $genreName) {
51 1
            $genreName = ucwords($genreName);
52
53 1
            $cachedGenre = $this->genreRepository->findOneBy([
54 1
                'title' => $genreName,
55 1
            ]);
56
57 1
            if ($cachedGenre === null) {
58 1
                $cachedGenre = $this->genreRepository
59 1
                    ->prototype()
60 1
                    ->setTitle($genreName);
61
62 1
                $this->genreRepository->save($cachedGenre);
63
            }
64
65 1
            $mappedGenre = $this->genreMapRepository
66 1
                ->prototype()
67 1
                ->setGenre($cachedGenre)
68 1
                ->setMappedItemType(GenreMapEnum::ALBUM)
69 1
                ->setMappedItemId($albumId);
70
71 1
            $this->genreMapRepository->save($mappedGenre);
72
        }
73
    }
74
}
75