for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace Uxmp\Core\Component\Catalog\Scanner;
use Uxmp\Core\Component\Tag\Container\AudioFileInterface;
use Uxmp\Core\Orm\Model\ArtistInterface;
use Uxmp\Core\Orm\Repository\ArtistRepositoryInterface;
/**
* Retrieves and caches artists for catalog scanning purposes
*
* If no artist was found, a new one will be created
*/
final class ArtistCache implements ArtistCacheInterface
{
/** @var array<string, ArtistInterface> */
private array $cache = [];
public function __construct(
private readonly ArtistRepositoryInterface $artistRepository,
) {
}
public function retrieve(AudioFileInterface $audioFile): ArtistInterface
$artistMbid = $audioFile->getArtistMbid();
$artist = $this->cache[$artistMbid] ?? null;
if ($artist === null) {
$artist = $this->artistRepository->findByMbId($artistMbid);
$artist = $this->artistRepository->prototype()
->setTitle($audioFile->getArtistTitle())
->setMbid($artistMbid)
;
$artist->setSearchTitle($audioFile->getArtistTitleClean());
$this->artistRepository->save($artist);
$this->cache[$artistMbid] = $artist;
return $artist;