ArtistCache   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 14
c 0
b 0
f 0
dl 0
loc 32
ccs 16
cts 16
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A retrieve() 0 22 3
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\ArtistInterface;
9
use Uxmp\Core\Orm\Repository\ArtistRepositoryInterface;
10
11
/**
12
 * Retrieves and caches artists for catalog scanning purposes
13
 *
14
 * If no artist was found, a new one will be created
15
 */
16
final class ArtistCache implements ArtistCacheInterface
17
{
18
    /** @var array<string, ArtistInterface> */
19
    private array $cache = [];
20
21 2
    public function __construct(
22
        private readonly ArtistRepositoryInterface $artistRepository,
23
    ) {
24 2
    }
25
26 2
    public function retrieve(AudioFileInterface $audioFile): ArtistInterface
27
    {
28 2
        $artistMbid = $audioFile->getArtistMbid();
29
30 2
        $artist = $this->cache[$artistMbid] ?? null;
31 2
        if ($artist === null) {
32 2
            $artist = $this->artistRepository->findByMbId($artistMbid);
33 2
            if ($artist === null) {
34 1
                $artist = $this->artistRepository->prototype()
35 1
                    ->setTitle($audioFile->getArtistTitle())
36 1
                    ->setMbid($artistMbid)
37 1
                ;
38
            }
39
40 2
            $artist->setSearchTitle($audioFile->getArtistTitleClean());
41
42 2
            $this->artistRepository->save($artist);
43
44 2
            $this->cache[$artistMbid] = $artist;
45
        }
46
47 2
        return $artist;
48
    }
49
}
50