WebflowDataProvider.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace App\DataProvider;
  3. use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
  4. use ApiPlatform\Core\DataProvider\ContextAwareCollectionDataProviderInterface;
  5. use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
  6. use App\Entity\WebflowCollection;
  7. use App\Entity\WebflowItem;
  8. use App\Http\WebflowApi\WebflowApiCollection;
  9. use App\Http\WebflowApi\WebflowSite;
  10. use App\Http\WebflowApiClient;
  11. use Psr\Log\LoggerInterface;
  12. final class WebflowDataProvider implements ContextAwareCollectionDataProviderInterface, RestrictedDataProviderInterface {
  13. private $site;
  14. private $apiClient;
  15. private $logger;
  16. public function __construct(WebflowApiClient $webflowApiClient, LoggerInterface $l) {
  17. $this->site = WebflowSite::byId($webflowApiClient, '5ebabfe546c816388d66c03a');
  18. $this->apiClient = $webflowApiClient;
  19. $this->logger= $l;
  20. }
  21. public function supports(string $resourceClass, ?string $operationName = null, array $context = []): bool
  22. {
  23. return in_array($resourceClass, [
  24. WebflowCollection::class,
  25. WebflowItem::class,
  26. ]);
  27. }
  28. public function getCollection(string $resourceClass, ?string $operationName = null, array $context = []): \Generator
  29. {
  30. switch ($resourceClass) {
  31. case WebflowCollection::class:
  32. foreach($this->site->getCollections() as $col)
  33. yield new WebflowCollection($col);
  34. break;
  35. case WebflowItem::class:
  36. if (isset($context['filters']) && isset($context['filters']['cid'])) {
  37. $col = WebflowApiCollection::byId($this->apiClient, $context['filters']['cid']);
  38. foreach ($col->getItems() as $item)
  39. yield new WebflowItem($item);
  40. break;
  41. }
  42. foreach ($this->site->getCollections() as $col)
  43. foreach ($col->getItems() as $item)
  44. yield new WebflowItem($item);
  45. break;
  46. }
  47. return null;
  48. }
  49. }