| Total Complexity | 6 |
| Total Lines | 60 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 14 | final readonly class ArtistCoverUpdater implements ArtistCoverUpdaterInterface |
||
|
|
|||
| 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 | } |
||
| 76 |