WebflowDataProvider.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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\WebflowCollectionAudience;
  8. use App\Entity\WebflowCollectionCategory;
  9. use App\Entity\WebflowCollectionDates;
  10. use App\Entity\WebflowCollectionEvent;
  11. use App\Http\WebflowApi\WebflowApiCollection;
  12. use App\Http\WebflowApi\WebflowSite;
  13. use App\Http\WebflowApi\WebflowSites;
  14. use App\Http\WebflowApiClient;
  15. use App\Serializer\ItemSerializer;
  16. use Doctrine\Common\Annotations\Reader;
  17. use phpDocumentor\Reflection\Types\Resource_;
  18. use Psr\Log\LoggerInterface;
  19. final class WebflowDataProvider implements ContextAwareCollectionDataProviderInterface, RestrictedDataProviderInterface {
  20. private $site;
  21. private $apiClient;
  22. private $logger;
  23. private $reader;
  24. public function __construct(WebflowApiClient $webflowApiClient, LoggerInterface $l, Reader $reader) {
  25. $this->site = WebflowSite::byId(new WebflowSites($webflowApiClient), '5ebabfe546c816388d66c03a');
  26. $this->apiClient = $webflowApiClient;
  27. $this->logger= $l;
  28. $this->reader = $reader;
  29. }
  30. public function supports(string $resourceClass, ?string $operationName = null, array $context = []): bool
  31. {
  32. $this->logger->debug(__METHOD__, [$resourceClass, $operationName, $context]);
  33. return in_array($resourceClass, [
  34. WebflowCollection::class,
  35. WebflowCollectionAudience::class,
  36. WebflowCollectionCategory::class,
  37. WebflowCollectionDates::class,
  38. WebflowCollectionEvent::class,
  39. ]);
  40. }
  41. public function getCollection(string $resourceClass, ?string $operationName = null, array $context = []): \Generator
  42. {
  43. switch ($resourceClass) {
  44. case WebflowCollection::class:
  45. foreach($this->site->getCollections() as $col) {
  46. $class = WebflowCollection::fromClient($col, $this->reader);
  47. yield $class;
  48. }
  49. break;
  50. case WebflowCollectionAudience::class:
  51. case WebflowCollectionCategory::class:
  52. case WebflowCollectionDates::class:
  53. case WebflowCollectionEvent::class:
  54. $col = WebflowApiCollection::byId($this->site, $resourceClass::cid())->load();
  55. foreach ($col->getItems() as $item) {
  56. $class = $resourceClass::fromClient($item, $this->reader);
  57. yield $class;
  58. }
  59. }
  60. return null;
  61. }
  62. }