Passed
Push — main ( e933de...5ef56a )
by Daniel
03:35
created

ArtistCache::retrieve()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 19
ccs 13
cts 13
cp 1
rs 9.9
cc 3
nc 3
nop 1
crap 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 1
                $this->artistRepository->save($artist);
39
            }
40
41 2
            $this->cache[$artistMbid] = $artist;
42
        }
43
44 2
        return $artist;
45
    }
46
}
47