src/EventSubscriber/TodayWeatherReportTwitterEventListner.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Pimcore\Event\Model\DataObjectEvent;
  4. use Pimcore\Model\DataObject\Report;
  5. use App\Service\TwitterService;
  6. use App\C2IntegrationBundle\Service\C2Service;
  7. use Symfony\Contracts\Translation\TranslatorInterface;
  8. class TodayWeatherReportTwitterEventListner
  9. {
  10.     private TwitterService $twitterService;
  11.     private C2Service $c2Service;
  12.     public function __construct(private TranslatorInterface $translator)
  13.     {
  14.         $this->twitterService = new TwitterService();
  15.         $this->c2Service = new C2Service();
  16.     }
  17.     public function onObjectCreate(DataObjectEvent $reportObject): void
  18.     {
  19.         $report $reportObject->getObject();
  20.         
  21.         if ($this->shouldHandleReport($report)) {
  22.             $this->handleTodayWeatherReport($report);
  23.         }
  24.     }
  25.     public function onObjectUpdate(DataObjectEvent $reportObject): void
  26.     {
  27.         $report $reportObject->getObject();
  28.         
  29.         if ($this->shouldHandleReport($report)) {
  30.             $this->handleTodayWeatherReport($report);
  31.         }
  32.     }
  33.     private function shouldHandleReport($report): bool
  34.     {
  35.         return ($report instanceof Report) &&
  36.             $report->isPublished(true) &&
  37.             $report->getReportType()?->getReportKey() === 'today-weather-report' &&
  38.             $report->getForTwitterChannel() == true;
  39.     }
  40.     private function handleTodayWeatherReport(Report $report): void
  41.     {
  42.         
  43.         $assetsToPost $this->collectAssets($report);
  44.         
  45.         foreach ($assetsToPost as $assetData) {
  46.             $assetObj $assetData['asset'];
  47.             $assetPath PIMCORE_PROJECT_ROOT '/public/var/assets' $assetObj->getPath() . $assetObj->getFileName();
  48.             
  49.             if (!file_exists($assetPath)) {
  50.                 continue;
  51.             }
  52.             if ($report->getIsMarine() === true) {
  53.                 $tweetText = (string) $report->getTwitterDescription();
  54.             } elseif ($assetData['lang'] == 'ar') {
  55.                 $tweetText $this->translator->trans('Today\'s Weather Report', [], null'ar');
  56.             } else {
  57.                 $tweetText $this->translator->trans('Today\'s Weather Report', [], null'en');
  58.             }
  59.             $tweet $this->twitterService->uploadMedia($assetPath$tweetText);
  60.             
  61.             if ($tweet && !isset($tweet['error'])) {
  62.                 $this->processTweetResponse($tweet$assetObj$report);
  63.             } elseif ($tweet && isset($tweet['error'])) {
  64.                 // Log the error but continue processing other assets
  65.                 $report->setTwitterLog($tweet['data']);
  66.             }
  67.         }
  68.         
  69.         $report->save();
  70.     }
  71.     private function collectAssets(Report $report): array
  72.     {
  73.         $assetAr $report->getAssetAr();
  74.         if (!$assetAr) {
  75.             return [];
  76.         }
  77.         return [['asset' => $assetAr'lang' => 'ar']];
  78.     }
  79.     private function processTweetResponse(array $tweet$assetObjReport $report): void
  80.     {
  81.         if (isset($tweet['result']->data)) {
  82.             $tweetText $tweet['result']?->data?->text;
  83.             preg_match('/https?:\/\/\S+/'$tweetText$matches);
  84.             $url $matches[0] ?? null;
  85.             
  86.             if ($url) {
  87.                 $this->c2Service->addAsset([$assetObj], $url);
  88.             }
  89.         }
  90.         
  91.         if (!empty($tweet['tweetId'])) {
  92.             $report->setTwitterId($tweet['tweetId']);
  93.         }
  94.         
  95.         $report->setTwitterLog($tweet['data']);
  96.     }
  97. }