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

ArtistCache   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 13
c 0
b 0
f 0
dl 0
loc 29
ccs 15
cts 15
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A retrieve() 0 19 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