diff --git a/app/Services/BundleCreator/Creators/CollectibleCreators/LegoBundleCreator.php b/app/Services/BundleCreator/Creators/CollectibleCreators/LegoBundleCreator.php index b1d4df0..1d0c4ae 100644 --- a/app/Services/BundleCreator/Creators/CollectibleCreators/LegoBundleCreator.php +++ b/app/Services/BundleCreator/Creators/CollectibleCreators/LegoBundleCreator.php @@ -2,6 +2,7 @@ namespace App\Services\BundleCreator\Creators\CollectibleCreators; +use App\Classes\AttachmentsManager; use App\Classes\Bundle; use App\Exceptions\BundleAlreadyExists; use App\Services\BundleCreator\Creators\BaseBundleCreator; @@ -31,8 +32,16 @@ public function createBundle(): string $rebrickable = app()->make(RebrickableClient::class); $setData = $rebrickable->getSet($this->data['rebrickable_id']); $theme = $rebrickable->getTheme($setData['theme_id']); - $themeSlug = Str::slug($theme['name']); - $path = sprintf('%s/%s/%s/%s', static::$section, static::$brand, $themeSlug, $this->data['product_id']); + $minifigs = $rebrickable->getMinifigs($this->data['rebrickable_id']); + + $themeSlug = Str::slug($theme['name']); + $path = sprintf( + '%s/%s/%s/%s', + static::$section, + static::$brand, + $themeSlug, + $this->data['product_id'] + ); $bundle = new Bundle($path, $this->disk); @@ -43,15 +52,50 @@ public function createBundle(): string } $bundle->markdown()->set(''); + $bundle->metadata()->setMany([ 'title' => $setData['name'], 'date' => now()->toIso8601String(), ]); - $bundle->metadata('metadata')->set('details.Identifiant', $this->data['product_id']); - $bundle->metadata('rebrickable')->setMany([ - 'set' => $setData, + + $bundle->metadata('metadata')->setMany([ + 'details' => [ + 'Identifiant' => $this->data['product_id'], + 'Date de sortie' => $setData['year'], + 'Nombre de pièces' => $setData['num_parts'], + ], + 'theme' => [ + $theme['name'], + ], ]); + $bundle->metadata('rebrickable')->setMany([ + 'set' => $setData, + 'minifigs' => $minifigs, + ]); + + if (!empty($setData['set_img_url'])) { + $ref = $bundle->attachments(AttachmentsManager::Images)->add([ + 'url' => $setData['set_img_url'], + 'attribution' => '© [Rebrickable](https://rebrickable.com/)', + ]); + + $bundle->metadata()->set('cover', $ref); + } + + if (!empty($minifigs)) { + foreach ($minifigs as $minifig) { + if (empty($minifig['set_img_url'])) { + continue; + } + + $bundle->attachments(AttachmentsManager::Images)->add([ + 'url' => $minifig['set_img_url'], + 'attribution' => '© [Rebrickable](https://rebrickable.com/)', + ]); + } + } + $bundle->save(); return $path; diff --git a/app/Services/BundleCreator/Creators/LinkBundleCreator.php b/app/Services/BundleCreator/Creators/LinkBundleCreator.php index 45ef7f7..bf5238d 100644 --- a/app/Services/BundleCreator/Creators/LinkBundleCreator.php +++ b/app/Services/BundleCreator/Creators/LinkBundleCreator.php @@ -46,7 +46,7 @@ public function createBundle(): string $ref = $bundle->attachments(AttachmentsManager::Images)->addToHistory('screenshot', [ 'contents' => $screenshot, - 'url' => sprintf('screenshot-%s.jpg', now()->format('Y-m-d')), + 'filename' => sprintf('screenshot-%s.jpg', now()->format('Y-m-d')), ]); $bundle->metadata()->setMany([ @@ -55,7 +55,7 @@ public function createBundle(): string 'cover' => $ref, ]); - $bundle->metadata('metadata')['links']['Page originale'] = [$url]; + $bundle->metadata('metadata')->set('links.Page originale', [$url]); $bundle->markdown()->set('> ' . $description . "\n\n"); diff --git a/app/Services/Rebrickable/RebrickableClient.php b/app/Services/Rebrickable/RebrickableClient.php index 6627d6e..a282cfe 100644 --- a/app/Services/Rebrickable/RebrickableClient.php +++ b/app/Services/Rebrickable/RebrickableClient.php @@ -2,10 +2,14 @@ namespace App\Services\Rebrickable; +use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Http; +use Illuminate\Support\Str; class RebrickableClient { + protected static string $cachePrefix = 'rebrickable'; + public function __construct(protected array $config) { @@ -31,9 +35,7 @@ public function searchSet(string $setId) */ public function getSet(string $setId) { - $response = $this->get(sprintf('sets/%s/', $setId)); - - return $response; + return $this->get(sprintf('sets/%s/', $setId)); } /** @@ -41,18 +43,36 @@ public function getSet(string $setId) */ public function getTheme(string $themeId) { - $response = $this->get(sprintf('themes/%s/', $themeId)); + return $this->get(sprintf('themes/%s/', $themeId)); + } + + /** + * Return minifigs associated to the set + */ + public function getMinifigs(string $setId) + { + return $this->get(sprintf('sets/%s/minifigs/', $setId))['results']; + } + + /** + * Cache and return the result of a GET request + */ + private function get(string $url) + { + $key = $this->config['key']; + $baseUrl = $this->config['baseUrl']; + $cacheKey = sprintf('%s_%s', static::$cachePrefix, Str::slug($url)); + + if (Cache::has($cacheKey)) { + return Cache::get($cacheKey); + } + + $response = Http::throw() + ->withHeader('Authorization', sprintf('key %s', $key)) + ->get(sprintf('%s/%s', $baseUrl, $url))->json(); + + Cache::put($cacheKey, $response, now()->addDays(7)); return $response; } - - private function get(string $url) - { - $key = $this->config['key']; - $baseUrl = $this->config['baseUrl']; - - return Http::throw() - ->withHeader('Authorization', sprintf('key %s', $key)) - ->get(sprintf('%s/%s', $baseUrl, $url))->json(); - } }