Passed
Push — main ( 1d1c5b...83bc52 )
by Daniel
04:23
created

AlbumCoverUpdater::update()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 26
ccs 0
cts 15
cp 0
rs 9.7998
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 20
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\Orm\Model\AlbumInterface;
10
11
final class AlbumCoverUpdater implements AlbumCoverUpdaterInterface
12
{
13
    public function __construct(
14
        private readonly ConfigProviderInterface $config,
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting T_VARIABLE on line 14 at column 25
Loading history...
15
        private readonly ArtistCoverUpdaterInterface $artistCoverUpdater
16
    ) {
17
    }
18
19
    public function update(
20
        AlbumInterface $album,
21
        array $metadata
22
    ): void {
23
        $destination = realpath($this->config->getAssetPath() . '/img/album');
24
25
        // search in comments
26
        /** @var null|array{image_mime: string, data: string} $image */
27
        $image = $metadata['comments']['picture'][0] ?? null;
28
        if ($image !== null) {
29
            $filename = $destination . '/' . $album->getMbid();
30
            if (!file_exists($filename)) {
31
                $extension = match ($image['image_mime']) {
32
                    'image/jpeg' => 'jpg',
33
                    'image/png' => 'png',
34
                    'image/gif' => 'gif',
35
                    default => null,
36
                };
37
38
                if ($extension === null) {
39
                    return;
40
                }
41
42
                file_put_contents($filename. '.' . $extension, $image['data']);
43
44
                $this->artistCoverUpdater->update($album->getArtist());
45
            }
46
        }
47
    }
48
}
49