Passed
Push — main ( 7dcca1...e70bce )
by Daniel
04:22
created

ArtistCoverUpdater   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 29
c 1
b 0
f 0
dl 0
loc 60
ccs 35
cts 35
cp 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uxmp\Core\Component\Artist;
6
7
use DateTime;
8
use Intervention\Image\Image;
9
use Tzsk\Collage\MakeCollage;
10
use Uxmp\Core\Component\Config\ConfigProviderInterface;
11
use Uxmp\Core\Orm\Model\ArtistInterface;
12
use Uxmp\Core\Orm\Repository\ArtistRepositoryInterface;
13
14
final readonly class ArtistCoverUpdater implements ArtistCoverUpdaterInterface
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_READONLY, expecting T_CLASS on line 14 at column 6
Loading history...
15
{
16 2
    public function __construct(
17
        private ConfigProviderInterface $config,
18
        private ArtistRepositoryInterface $artistRepository,
19
        private MakeCollage $collageMaker,
20
    ) {
21 2
    }
22
23 2
    public function update(
24
        ArtistInterface $artist
25
    ): void {
26 2
        $images = [];
27 2
        $assetPath = $this->config->getAssetPath();
28
29 2
        $albumDestination = sprintf(
30 2
            '%s/img/album',
31 2
            $assetPath
32 2
        );
33
34 2
        foreach ($artist->getAlbums() as $album) {
35 2
            $albumImage = sprintf('%s/%s.jpg', $albumDestination, $album->getMbid());
36
37 2
            if (file_exists($albumImage)) {
38 1
                $images[] = $albumImage;
39
            }
40
        }
41
42 2
        if ($images === []) {
43 1
            return;
44
        }
45
46
        // MakeCollage only support up to 4 images, to take the first ones
47 1
        if (count($images) > 4) {
48 1
            $images = array_slice($images, 0, 4);
49
        }
50
51
        /** @var Image $image */
52 1
        $image = $this->collageMaker
53 1
            ->make(600, 600)
54 1
            ->padding(10)
55 1
            ->background('#000')
56 1
            ->from($images);
57
58 1
        $artistDestination = sprintf(
59 1
            '%s/img/artist',
60 1
            $assetPath
61 1
        );
62
63 1
        $image->save(
64 1
            sprintf(
65 1
                '%s/%s.jpg',
66 1
                $artistDestination,
67 1
                $artist->getMbid()
68 1
            )
69 1
        );
70
71 1
        $artist->setLastModified(new DateTime());
72
73 1
        $this->artistRepository->save($artist);
74
    }
75
}
76