WebflowItemDataProvider.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace App\DataProvider;
  3. use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
  4. use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
  5. use App\Entity\WebflowCollection;
  6. use App\Http\WebflowApi\WebflowSite;
  7. use App\Http\WebflowApi\WebflowSites;
  8. use App\Http\WebflowApiClient;
  9. use Psr\Log\LoggerInterface;
  10. final class WebflowItemDataProvider implements ItemDataProviderInterface, RestrictedDataProviderInterface {
  11. private $site;
  12. private $apiClient;
  13. private $logger;
  14. public function __construct(WebflowApiClient $webflowApiClient, LoggerInterface $loggerInterface)
  15. {
  16. $this->site = WebflowSite::byId($webflowApiClient, '5ebabfe546c816388d66c03a');
  17. $this->logger = $loggerInterface;
  18. }
  19. public function supports(string $resourceClass, ?string $operationName = null, array $context = []): bool {
  20. return in_array($resourceClass, [
  21. WebflowCollection::class,
  22. WebflowItem::class,
  23. ]);
  24. }
  25. public function getItem(string $resourceClass, $id, ?string $operationName = null, array $context = []): ?WebflowCollection {
  26. switch ($resourceClass) {
  27. case WebflowCollection::class:
  28. foreach ($this->site->getCollections() as $col)
  29. if ($col->data['_id'] == $id)
  30. return new WebflowCollection($col);
  31. break;
  32. }
  33. return null;
  34. }
  35. }