WebflowDataPersister.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace App\DataPersister;
  3. use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface;
  4. use App\Entity\WebflowCollectionEvent;
  5. use App\Http\WebflowApi\WebflowApiCollection;
  6. use App\Http\WebflowApi\WebflowSites;
  7. use App\Serializer\ItemSerializer;
  8. use Doctrine\Common\Annotations\Reader;
  9. use Psr\Log\LoggerInterface;
  10. use Symfony\Component\HttpClient\HttpClient;
  11. use Symfony\Component\Security\Core\Exception\InsufficientAuthenticationException;
  12. final class WebflowDataPersister implements ContextAwareDataPersisterInterface{
  13. private $logger;
  14. private $site;
  15. private $reader;
  16. public function __construct(LoggerInterface $loggerInterface, WebflowSites $webflowSites, Reader $reader)
  17. {
  18. $this->logger = $loggerInterface;
  19. $this->logger->debug(__METHOD__, $webflowSites->getSites());
  20. $this->site = $webflowSites->getSiteByShortName('tromsotid');
  21. $this->reader = $reader;
  22. }
  23. public function supports($data, array $context = []): bool
  24. {
  25. $this->logger->debug(__METHOD__, [
  26. 'data' => $data,
  27. ] + $context);
  28. return $data instanceof WebflowCollectionEvent;
  29. }
  30. public function persist($data, array $context = [])
  31. {
  32. $val = [
  33. 'id' => $data->getId(),
  34. 'cid' => $data->getCid(),
  35. 'name' => $data->getTitle(),
  36. 'place' => $data->getPlace(),
  37. ];
  38. $client = HttpClient::create();
  39. $resp = $client->request('POST', "https://www.google.com/recaptcha/api/siteverify", [
  40. 'body' => [
  41. 'secret' => '6LfRxgAVAAAAAJBFO492ap_OUfQz3kJotl2Xad_V',
  42. 'response' => $data->getCaptcha(),
  43. 'remoteip' => $_SERVER['REMOTE_ADDR'],
  44. ]
  45. ]);
  46. $content = $resp->toArray();
  47. if (!$content['success'])
  48. throw new InsufficientAuthenticationException(sprintf("Error-codes\nAuthentication: %s (captcha)", join(", ", $content['error-codes'])));
  49. $dates = array_map(function ($data) {
  50. $data['date'] = new \DateTime($data['date']);
  51. return $data;
  52. }, $data->getHappensOn());
  53. $itemSerializer = new ItemSerializer($this->reader, $data);
  54. $data->setHappensOn([]);
  55. $item = WebflowApiCollection::byId($this->site, $data->getCid())->createItem(
  56. $itemSerializer->restoreProperties()
  57. );
  58. $data = WebflowCollectionEvent::fromClient($item, $this->reader);
  59. $fh = fopen(getcwd() . '/../var/dates', "a+");
  60. flock($fh, LOCK_EX);
  61. $entries = [];
  62. foreach ($dates as $date) {
  63. $entries[] = [
  64. 'date' => $date['date']->format(\DateTime::RFC3339_EXTENDED),
  65. 'duration' => $date['duration'],
  66. 'title' => sprintf("%s, %s -> %dh", $data->getTitle(), $date['date']->format('d-m-Y H:i'), $date['duration']),
  67. ];
  68. }
  69. fwrite($fh, json_encode([
  70. 'id' => $data->getId(),
  71. 'entries' => $entries,
  72. ]) . "\n");
  73. flock($fh, LOCK_UN);
  74. fclose($fh);
  75. return $data;
  76. }
  77. public function remove($data, array $context = [])
  78. {
  79. }
  80. }