|
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
|
|
|
|