37 lines
1.0 KiB
PHP
37 lines
1.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\State;
|
|
|
|
use ApiPlatform\Metadata\Operation;
|
|
use ApiPlatform\State\ProviderInterface;
|
|
use App\ApiResource\BookStackShelf;
|
|
use App\Exception\BookStackApiException;
|
|
use App\Service\BookStackApiService;
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
|
|
|
final readonly class BookStackShelfProvider implements ProviderInterface
|
|
{
|
|
public function __construct(
|
|
private BookStackApiService $bookStackApiService,
|
|
) {}
|
|
|
|
public function provide(Operation $operation, array $uriVariables = [], array $context = []): array
|
|
{
|
|
try {
|
|
$shelves = $this->bookStackApiService->listShelves();
|
|
} catch (BookStackApiException $e) {
|
|
throw new BadRequestHttpException($e->getMessage(), $e);
|
|
}
|
|
|
|
return array_map(static function (array $shelf): BookStackShelf {
|
|
$dto = new BookStackShelf();
|
|
$dto->id = $shelf['id'] ?? 0;
|
|
$dto->name = $shelf['name'] ?? '';
|
|
|
|
return $dto;
|
|
}, $shelves);
|
|
}
|
|
}
|