WfAddDatesCommand.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace App\Command;
  3. use App\Entity\WebflowCollectionDates;
  4. use App\Entity\WebflowCollectionEvent;
  5. use App\Http\WebflowApi\WebflowApiCollection;
  6. use App\Http\WebflowApi\WebflowApiCollectionItem;
  7. use App\Http\WebflowApi\WebflowSites;
  8. use Symfony\Component\Console\Command\Command;
  9. use Symfony\Component\Console\Input\InputArgument;
  10. use Symfony\Component\Console\Input\InputInterface;
  11. use Symfony\Component\Console\Input\InputOption;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. use Symfony\Component\Console\Style\SymfonyStyle;
  14. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  15. class ExcausedException extends \Exception {}
  16. class WfAddDatesCommand extends Command
  17. {
  18. protected static $defaultName = 'wf:add-dates';
  19. public $site;
  20. public function __construct(WebflowSites $webflowSites) {
  21. $this->site = $webflowSites->getSiteByShortName('tromsotid');
  22. parent::__construct();
  23. }
  24. protected function configure()
  25. {
  26. $this
  27. ->setDescription('Add a short description for your command')
  28. ->addArgument('arg1', InputArgument::OPTIONAL, 'Argument description')
  29. ->addOption('option1', null, InputOption::VALUE_NONE, 'Option description')
  30. ;
  31. }
  32. protected function execute(InputInterface $input, OutputInterface $output): int
  33. {
  34. $io = new SymfonyStyle($input, $output);
  35. $fn = getcwd() . "/var/dates";
  36. if (!file_exists($fn)) {
  37. $io->success("File do not exist, but that all ok! :)");
  38. return static::SUCCESS;
  39. }
  40. if (filesize($fn) <= 0) {
  41. return static::SUCCESS;
  42. }
  43. $data = null;
  44. try {
  45. $dateApi = WebflowApiCollection::byId($this->site, WebflowCollectionDates::cid())->load(true);
  46. } catch (\Exception $e) {
  47. $io->error($e->getMessage());
  48. return static::FAILURE;
  49. }
  50. $fh = fopen($fn, "r+");
  51. fseek($fh, 0, SEEK_SET);
  52. flock($fh, LOCK_EX);
  53. if (filesize($fn) <= 0) {
  54. flock($fh, LOCK_UN);
  55. fclose($fh);
  56. return static::SUCCESS;
  57. }
  58. $fc = fread($fh, filesize($fn));
  59. $entries = array_filter(array_map(function (string $line) {
  60. return json_decode($line, true);
  61. }, explode("\n", $fc)), function ($json) {
  62. return !is_null($json);
  63. });
  64. foreach ($entries as $lineno => $line) {
  65. try {
  66. $event = WebflowApiCollectionItem::byId($this->site,
  67. $line['id'], [
  68. '_cid' => WebflowCollectionEvent::cid()
  69. ]
  70. )->load(true);
  71. } catch (NotFoundHttpException $e) {
  72. $io->caution($e->getMessage());
  73. unset($entries[$lineno]);
  74. continue;
  75. }
  76. try {
  77. foreach ($line['entries'] as $key => $entry) {
  78. $limit = ($dateApi->getLastResponseHeaders()['x-ratelimit-limit']?? [60])[0];
  79. $remaining = ($dateApi->getLastResponseHeaders()['x-ratelimit-remaining'] ?? [60])[0];
  80. $ratio = $remaining / $limit;
  81. if ($ratio < 0.3)
  82. throw new ExcausedException("Ratio met.");
  83. $item = $dateApi->createItem([
  84. 'name' => $entry['title'],
  85. 'dato' => $entry['date'],
  86. 'varighet' => $entry['duration'],
  87. 'arrangement' => [$line['id']],
  88. '_draft' => true,
  89. '_archived' => false
  90. ]);
  91. unset($line['entries'][$key]);
  92. }
  93. } catch (\Exception $e) {
  94. $io->caution($e->getMessage());
  95. } finally {
  96. $entries[$lineno] = $line;
  97. }
  98. }
  99. foreach ($entries as $key => $entry)
  100. if (sizeof($entry['entries']) == 0)
  101. unset($entries[$key]);
  102. fseek($fh, 0, SEEK_SET);
  103. ftruncate($fh, 0);
  104. if (sizeof($entries) != 0) {
  105. fwrite($fh, implode("\n", array_map(function ($json) {
  106. return json_encode($json);
  107. }, $entries)));
  108. }
  109. flock($fh, LOCK_UN);
  110. fclose($fh);
  111. return static::SUCCESS;
  112. }
  113. }