1
0
cms11/app/Services/BundleUpdaters/Updaters/LegoUpdater.php

129 lines
3.9 KiB
PHP
Raw Normal View History

<?php
namespace App\Services\BundleUpdaters\Updaters;
use App\Classes\AttachmentsManager;
use App\Classes\Bundle;
use App\Services\Rebrickable\RebrickableClient;
use function Laravel\Prompts\select;
class LegoUpdater extends BaseUpdater
{
/**
* Return a boolean value indicating if the updater can actually make the
* update using known data.
*/
public function canUpdateBundle(): bool
{
return !empty($this->bundle->metadata()->get('rebrickableId'));
}
/**
* Return an array describing what kind of data the updater needs in
* addition to the one it already has
*/
public function formSpecs(): ?array
{
$specs = [];
if (empty($this->bundle->metadata()->get('rebrickableId'))) {
$productId = basename($this->bundle->getPath());
$specs['rebrickableId'] = fn () => select(
'Rebrickable Set ID',
app()->make(RebrickableClient::class)->searchSet($productId)
);
}
return $specs;
}
/**
* Updates bundle's extended metadata
*/
public function update()
{
$productId = basename($this->bundle->getPath());
$rebrickable = app()->make(RebrickableClient::class);
$setData = $rebrickable->getSet($this->bundle->metadata()->get('rebrickableId'));
$theme = $rebrickable->getTheme($setData['theme_id']);
$minifigs = $rebrickable->getMinifigs($this->bundle->metadata()->get('rebrickableId'));
$this->bundle->metadata()->setMany([
'title' => $setData['name'],
'productId' => $productId,
]);
$this->bundle->metadata('metadata')->setMany([
2024-04-26 12:50:38 +02:00
'Détails' => [
'Identifiant' => $productId,
'Date de sortie' => $setData['year'],
'Nombre de pièces' => $setData['num_parts'],
],
2024-04-26 12:50:38 +02:00
'Thème' => [
$theme['name'],
],
]);
$this->bundle->metadata('rebrickable/set')->setMany($setData);
$this->bundle->metadata('rebrickable/theme')->setMany($theme);
$this->bundle->metadata('rebrickable/minifigs')->setMany($minifigs);
if (!empty($setData['set_img_url'])) {
$ref = $this->bundle->attachments(AttachmentsManager::Images)->add([
2024-05-12 14:47:44 +02:00
'original_url' => $setData['set_img_url'],
]);
2024-05-12 21:09:12 +02:00
$this->bundle->attachments(AttachmentsManager::Images)->manager()
->set(sprintf('files.%s.attribution', $ref), '&copy; [Rebrickable](https://rebrickable.com/)');
$this->bundle->metadata()->set('cover', $ref);
}
if (!empty($minifigs)) {
foreach ($minifigs as $minifig) {
if (empty($minifig['set_img_url'])) {
continue;
}
2024-05-12 21:09:12 +02:00
$minifigRef = $this->bundle->attachments(AttachmentsManager::Images)->add([
2024-05-12 14:47:44 +02:00
'original_url' => $minifig['set_img_url'],
]);
2024-05-12 21:09:12 +02:00
$this->bundle->attachments(AttachmentsManager::Images)->manager()
->set(sprintf('files.%s.attribution', $minifigRef), '&copy; [Rebrickable](https://rebrickable.com/)');
}
}
2024-05-05 17:31:32 +02:00
$saved = $this->bundle->save();
if ($saved) {
$this->bundle->repair();
} else {
$saved = $this->bundle->repair();
}
return $saved;
}
/**
* Return a boolean value indicating if this updater in particular can
* update specified bundle
*/
public static function handles(Bundle $bundle): bool
{
$parts = preg_split('#/#', $bundle->getPath(), -1, PREG_SPLIT_NO_EMPTY);
2024-04-27 00:04:50 +02:00
if (empty($parts)) {
return false;
}
if (empty($parts) || $parts[0] !== 'collections' || empty($parts[1]) || $parts[1] !== 'lego' || count($parts) !== 4) {
return false;
}
return true;
}
}