|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Uxmp\Core\Component\Catalog\Scanner; |
|
6
|
|
|
|
|
7
|
|
|
use DateTime; |
|
8
|
|
|
use Psr\Container\ContainerInterface; |
|
9
|
|
|
use Uxmp\Core\Component\Album\AlbumCoverUpdaterInterface; |
|
10
|
|
|
use Uxmp\Core\Component\Event\EventHandlerInterface; |
|
11
|
|
|
use Uxmp\Core\Component\Tag\Container\AudioFileInterface; |
|
12
|
|
|
use Uxmp\Core\Orm\Model\AlbumInterface; |
|
13
|
|
|
use Uxmp\Core\Orm\Model\CatalogInterface; |
|
14
|
|
|
use Uxmp\Core\Orm\Repository\AlbumRepositoryInterface; |
|
15
|
|
|
|
|
16
|
|
|
final class AlbumCache implements AlbumCacheInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var array<string, AlbumInterface> */ |
|
19
|
|
|
private array $cache = []; |
|
20
|
|
|
|
|
21
|
2 |
|
public function __construct( |
|
22
|
|
|
private readonly AlbumRepositoryInterface $albumRepository, |
|
23
|
|
|
private readonly ArtistCacheInterface $artistCache, |
|
24
|
|
|
private readonly EventHandlerInterface $eventHandler, |
|
25
|
|
|
private readonly GenreCacheInterface $genreCache, |
|
26
|
|
|
) { |
|
27
|
2 |
|
} |
|
28
|
|
|
|
|
29
|
2 |
|
public function retrieve( |
|
30
|
|
|
CatalogInterface $catalog, |
|
31
|
|
|
AudioFileInterface $audioFile, |
|
32
|
|
|
): AlbumInterface { |
|
33
|
2 |
|
$albumMbid = $audioFile->getAlbumMbid(); |
|
34
|
|
|
|
|
35
|
2 |
|
$album = $this->cache[$albumMbid] ?? null; |
|
36
|
2 |
|
if ($album === null) { |
|
37
|
2 |
|
$album = $this->albumRepository->findByMbId($albumMbid); |
|
38
|
|
|
|
|
39
|
2 |
|
$artist = $this->artistCache->retrieve($audioFile); |
|
40
|
|
|
|
|
41
|
2 |
|
if ($album === null) { |
|
42
|
1 |
|
$album = $this->albumRepository->prototype() |
|
43
|
1 |
|
->setMbid($albumMbid) |
|
44
|
1 |
|
->setCatalog($catalog) |
|
45
|
1 |
|
->setLastModified(new DateTime()) |
|
46
|
1 |
|
->setArtist($artist) |
|
47
|
1 |
|
; |
|
48
|
1 |
|
$artist->addAlbum($album); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
2 |
|
$album |
|
52
|
2 |
|
->setTitle($audioFile->getAlbumTitle()) |
|
53
|
2 |
|
->setSearchTitle($audioFile->getAlbumTitleClean()) |
|
54
|
2 |
|
->setYear($audioFile->getYear()); |
|
55
|
|
|
|
|
56
|
2 |
|
$this->albumRepository->save($album); |
|
57
|
|
|
|
|
58
|
2 |
|
$this->eventHandler->fire( |
|
59
|
2 |
|
static function (ContainerInterface $c) use ($album, $audioFile): void { |
|
60
|
2 |
|
$c->get(AlbumCoverUpdaterInterface::class)->update($album, $audioFile); |
|
61
|
2 |
|
} |
|
62
|
2 |
|
); |
|
63
|
|
|
|
|
64
|
2 |
|
$this->genreCache->enrich($album, $audioFile); |
|
65
|
|
|
|
|
66
|
2 |
|
$this->cache[$albumMbid] = $album; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
2 |
|
return $album; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|