Passed
Push — main ( d7832a...2d8ac8 )
by Daniel
04:45
created

GenreCache   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 38
ccs 0
cts 18
cp 0
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A enrich() 0 30 4
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\GenreInterface;
10
use Uxmp\Core\Orm\Model\GenreMapEnum;
0 ignored issues
show
Bug introduced by
The type Uxmp\Core\Orm\Model\GenreMapEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Uxmp\Core\Orm\Repository\GenreMapRepositoryInterface;
12
use Uxmp\Core\Orm\Repository\GenreRepositoryInterface;
13
14
final class GenreCache implements GenreCacheInterface
15
{
16
    public function __construct(
17
        private readonly GenreRepositoryInterface $genreRepository,
18
        private readonly GenreMapRepositoryInterface $genreMapRepository,
19
    ) {
20
    }
21
22
    public function enrich(
23
        AlbumInterface $album,
24
        AudioFileInterface $audioFile,
25
    ): void {
26
        $genres = $audioFile->getGenres();
27
28
        if ($genres === []) {
29
            return;
30
        }
31
32
        foreach ($genres as $genre_name) {
33
            $cachedGenre = $this->genreRepository->findOneBy([
34
                    'title' => $genre_name,
35
                ]);
36
37
            if ($cachedGenre === null) {
38
                $cachedGenre = $this->genreRepository
39
                        ->prototype()
40
                        ->setTitle($genre_name);
41
42
                $this->genreRepository->save($cachedGenre);
43
            }
44
45
            $mapped_genre = $this->genreMapRepository
46
                ->prototype()
47
                ->setGenre($cachedGenre)
48
                ->setMappedItemType(GenreMapEnum::ALBUM)
49
                ->setMappedItemId($album->getId());
50
51
            $this->genreMapRepository->save($mapped_genre);
52
        }
53
    }
54
}
55