| Total Complexity | 6 |
| Total Lines | 60 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 13 | final class ArtistCoverUpdater implements ArtistCoverUpdaterInterface |
||
| 14 | { |
||
| 15 | 2 | public function __construct( |
|
| 16 | private readonly ConfigProviderInterface $config, |
||
|
|
|||
| 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 | } |
||
| 75 |