Passed
Push — main ( b86851...42a9a8 )
by Daniel
04:14
created

AlbumCoverUpdater::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 2
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uxmp\Core\Component\Album;
6
7
use Uxmp\Core\Component\Artist\ArtistCoverUpdaterInterface;
8
use Uxmp\Core\Component\Config\ConfigProviderInterface;
9
use Uxmp\Core\Component\Tag\Container\AudioFileInterface;
10
use Uxmp\Core\Orm\Model\AlbumInterface;
11
12
final class AlbumCoverUpdater implements AlbumCoverUpdaterInterface
13
{
14 5
    public function __construct(
15
        private readonly ConfigProviderInterface $config,
16
        private readonly ArtistCoverUpdaterInterface $artistCoverUpdater
17
    ) {
18
    }
19
20 5
    public function update(
21
        AlbumInterface $album,
22
        AudioFileInterface $audioFile,
23
    ): void {
24 5
        $image = $audioFile->getAlbumCover();
25 5
        if ($image !== null) {
26 4
            $extension = match ($image['image_mime']) {
27 1
                'image/jpeg' => 'jpg',
28 1
                'image/png' => 'png',
29 1
                'image/gif' => 'gif',
30 1
                default => null,
31
            };
32
33 4
            if ($extension === null) {
34 1
                return;
35
            }
36
37 3
            $destination = sprintf(
38
                '%s/img/album',
39 3
                $this->config->getAssetPath()
40
            );
41
42 3
            $filename = sprintf(
43
                '%s/%s.%s',
44
                $destination,
45 3
                $album->getMbid(),
46
                $extension
47
            );
48
49 3
            if (!file_exists($filename)) {
50 3
                file_put_contents($filename, $image['data']);
51
52 3
                $this->artistCoverUpdater->update($album->getArtist());
53
            }
54
        }
55
    }
56
}
57