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

ArtistCoverUpdater::update()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 51
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 51
ccs 23
cts 23
cp 1
rs 9.1608
cc 5
nc 9
nop 1
crap 5

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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