|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Uxmp\Core\Component\Catalog\Scanner; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
|
8
|
|
|
use Uxmp\Core\Component\Disc\DiscLengthUpdaterInterface; |
|
9
|
|
|
use Uxmp\Core\Component\Event\EventHandlerInterface; |
|
10
|
|
|
use Uxmp\Core\Component\Tag\Container\AudioFileInterface; |
|
11
|
|
|
use Uxmp\Core\Orm\Model\CatalogInterface; |
|
12
|
|
|
use Uxmp\Core\Orm\Model\DiscInterface; |
|
13
|
|
|
use Uxmp\Core\Orm\Repository\DiscRepositoryInterface; |
|
14
|
|
|
|
|
15
|
|
|
final class DiscCache implements DiscCacheInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var array<string, DiscInterface> */ |
|
18
|
|
|
private array $cache = []; |
|
19
|
|
|
|
|
20
|
2 |
|
public function __construct( |
|
21
|
|
|
private readonly DiscRepositoryInterface $discRepository, |
|
22
|
|
|
private readonly AlbumCacheInterface $albumCache, |
|
23
|
|
|
private readonly EventHandlerInterface $eventHandler |
|
24
|
|
|
) { |
|
25
|
2 |
|
} |
|
26
|
|
|
|
|
27
|
2 |
|
public function retrieve( |
|
28
|
|
|
CatalogInterface $catalog, |
|
29
|
|
|
AudioFileInterface $audioFile, |
|
30
|
|
|
): DiscInterface { |
|
31
|
2 |
|
$discMbId = $audioFile->getDiscMbid(); |
|
32
|
2 |
|
$discNumber = $audioFile->getDiscNumber(); |
|
33
|
2 |
|
$album = $this->albumCache->retrieve($catalog, $audioFile); |
|
34
|
|
|
|
|
35
|
2 |
|
$cacheKey = sprintf('%s_%d', $discMbId, $discNumber); |
|
36
|
|
|
|
|
37
|
2 |
|
$disc = $this->cache[$cacheKey] ?? null; |
|
38
|
2 |
|
if ($disc === null) { |
|
39
|
2 |
|
$disc = $this->discRepository->findUniqueDisc($discMbId, $discNumber); |
|
40
|
2 |
|
if ($disc === null) { |
|
41
|
1 |
|
$disc = $this->discRepository->prototype(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
2 |
|
$disc->setMbid($discMbId) |
|
45
|
2 |
|
->setAlbum($album) |
|
46
|
2 |
|
->setNumber($discNumber) |
|
47
|
2 |
|
; |
|
48
|
|
|
|
|
49
|
2 |
|
$this->discRepository->save($disc); |
|
50
|
|
|
|
|
51
|
2 |
|
$this->cache[$cacheKey] = $disc; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
2 |
|
$this->eventHandler->fire( |
|
55
|
2 |
|
static function (ContainerInterface $c) use ($disc): void { |
|
56
|
2 |
|
$c->get(DiscLengthUpdaterInterface::class)->update($disc); |
|
57
|
2 |
|
} |
|
58
|
2 |
|
); |
|
59
|
|
|
|
|
60
|
2 |
|
return $disc; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|