src/EventSubscriber/TwitterEwsEndEventListner.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Carbon\Carbon;
  4. use Pimcore\Mail;
  5. use Pimcore\Model\DataObject;
  6. use Pimcore\Event\Model\DataObjectEvent;
  7. use Pimcore\Model\DataObject\EwsNotification;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use App\Service\TwitterService;
  10. use App\C2IntegrationBundle\Service\C2Service;
  11. use Symfony\Component\Templating\EngineInterface;
  12. use Knp\Snappy\Pdf;
  13. use Knp\Snappy\Image;
  14. use Pimcore\Log\ApplicationLogger;
  15. class TwitterEwsEndEventListner
  16. {
  17. private TwitterService $twitterService;
  18. private C2Service $c2Service;
  19. private ApplicationLogger $logger;
  20. public function __construct(
  21. private Pdf $snappy,
  22. private Image $snappyImage,
  23. private EngineInterface $twig,
  24. ApplicationLogger $logger
  25. ) {
  26. $this->twitterService = new TwitterService();
  27. $this->c2Service = new C2Service();
  28. $this->logger = $logger;
  29. }
  30. public function onObjectUpdate(DataObjectEvent $ewsNotificationObject)
  31. {
  32. $ewsNotification = $ewsNotificationObject->getObject();
  33. if (
  34. ($ewsNotification instanceof EwsNotification) &&
  35. ($ewsNotification->isPublished(true)) &&
  36. $ewsNotification->getEnableTwitterNotification() === true &&
  37. $ewsNotification->getStatus() === "ended"
  38. ) {
  39. try {
  40. $parameters = [
  41. 'searchEwsIdAr' => $ewsNotification->getEwsSearchId("ar"),
  42. 'host' => API_BASE_URL,
  43. 'alertColor' => $ewsNotification->getAlertType()->getColor()
  44. ];
  45. $HashTagcontent = "#alertEnded";
  46. $html = $this->twig->render('web2print/_manned_alert_ended_notification_ar.html.twig', $parameters);
  47. $this->snappyImage->setOption('enable-local-file-access', true);
  48. $this->snappyImage->setOption('width', '787');
  49. $this->snappyImage->setOption('height', '797');
  50. $this->snappyImage->setOption('format', 'png');
  51. $image = $this->snappyImage->getOutputFromHtml($html);
  52. $asset = \App\Lib\Utility::createAsset(
  53. $image,
  54. rand(0, 10000) . '_' . time() . 'end_ews_alert.jpeg',
  55. 'END EWS Twitter Images'
  56. );
  57. $asset_path = PIMCORE_PROJECT_ROOT . '/public/var/assets' . $asset->getPath() . $asset->getFileName();
  58. $tweet = $this->twitterService->uploadMedia($asset_path, $HashTagcontent);
  59. if ($tweet) {
  60. if (isset($tweet['result']->data)) {
  61. $tweet_text = $tweet['result']?->data?->text;
  62. preg_match('/https?:\/\/\S+/', $tweet_text, $matches);
  63. $url = $matches[0] ?? null;
  64. $this->c2Service->addAsset([$asset], $url);
  65. }
  66. if (!empty($tweet['tweetId'])) {
  67. $ewsNotification->setTwitterId($tweet['tweetId']);
  68. }
  69. $ewsNotification->setTwitterLog($tweet['data']);
  70. $ewsNotification->save();
  71. $logContext = [
  72. 'ewsId' => $ewsNotification->getId(),
  73. 'tweetId' => $tweet['tweetId'] ?? null,
  74. 'httpCode' => $tweet['httpCode'],
  75. 'response' => $tweet['data']
  76. ];
  77. if (in_array($tweet['httpCode'], [200, 201, 204])) {
  78. $this->logger->info("Twitter post successful for EWS ID: {$ewsNotification->getId()}", $logContext);
  79. } elseif ($tweet['httpCode'] === 403) {
  80. $responseData = json_decode($tweet['data'], true);
  81. $errorMessage = $responseData['detail'] ?? 'Forbidden';
  82. $this->logger->error("Twitter post failed (403) for EWS ID: {$ewsNotification->getId()} - $errorMessage", $logContext);
  83. } else {
  84. $this->logger->error("Twitter post failed (HTTP {$tweet['httpCode']}) for EWS ID: {$ewsNotification->getId()}", $logContext);
  85. }
  86. } else {
  87. $this->logger->error("No tweet response for EWS ID: {$ewsNotification->getId()}");
  88. }
  89. } catch (\Throwable $e) {
  90. $this->logger->error("Exception occurred while posting to Twitter for EWS ID: {$ewsNotification->getId()} - " . $e->getMessage(), [
  91. 'exception' => $e->getTraceAsString()
  92. ]);
  93. }
  94. }
  95. }
  96. }