src/EventSubscriber/TwitterEventListner.php line 200

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. class TwitterEventListner
  15. {
  16.     private $twitterService;
  17.     private $c2Service;
  18.     public function __construct(private Pdf $snappy, private Image $snappyImage, private EngineInterface $twig)
  19.     {
  20.         $this->twitterService = new TwitterService();
  21.         $this->c2Service = new C2Service();
  22.     }
  23.     public function onObjectCreate(DataObjectEvent $ewsNotificationObject)
  24.     {
  25.         $ewsNotification $ewsNotificationObject->getObject();
  26.         
  27.         if (
  28.             ($ewsNotification instanceof EwsNotification) &&
  29.             ($ewsNotification->isPublished(true)) &&
  30.             empty($ewsNotification->getTwitterId())  &&
  31.             empty($ewsNotification->getTwitterLog()) &&
  32.             $ewsNotification->getEnableTwitterNotification() == true
  33.         ) {
  34.             $governates $ewsNotification->getGovernorate();
  35.             $startDate $ewsNotification->getStartDate()->format('Y-m-d');
  36.             $currentDate Carbon::now();
  37.             $targetDate Carbon::parse($startDate);
  38.             $content '';
  39.             $parameters = [];
  40.             if (!empty($ewsNotification->getRegion())) {
  41.                 // $content .= 'Region : '. $ewsNotification->getRegion()->getName('en')  ." \n";
  42.                 $parameters['region'] = $ewsNotification->getRegion()->getName('ar');
  43.             }
  44.             if (!empty($ewsNotification->getAlertAction())) {
  45.                 $alertActions = [];
  46.                 //$content .= 'Alert Action: ';
  47.                 foreach ($ewsNotification->getAlertAction() as $alertAction) {
  48.                     // $content .= $alertAction->getName() .' , ';
  49.                     $alertActions[] = $alertAction->getName('ar');
  50.                 }
  51.                 // $content .= " \n";
  52.                 $parameters['alertAction'] = $alertActions;
  53.             }
  54.             if (!empty($ewsNotification->getAlertType())) {
  55.                 $parameters['alertType'] = $ewsNotification->getAlertType()->getName('en');
  56.                 
  57.                 // $content .= 'Alert Type: ' . $ewsNotification->getAlertType()->getName() ." \n";
  58.             }
  59.             if (!empty($ewsNotification->getWeatherPhenomenon())) {
  60.                 $parameters['weatherPhenomenon'] = $ewsNotification->getWeatherPhenomenon()->getTitle('en');
  61.                 // $parameters['alertStatusAr'] = $ewsNotification->getWeatherPhenomenon()->getTitle('ar');
  62.                 $parameters['alertStatusAr'] = '';
  63.                 //$content .= 'Weather Phenomenon: ' . $ewsNotification->getWeatherPhenomenon()->getTitle() ." \n";
  64.             }
  65.             
  66.             if (!empty($ewsNotification->getAlertHazard())) {
  67.                 $alertHazards = [];
  68.                 //$content .= 'Alert Hazards: ';
  69.                 foreach ($ewsNotification->getAlertHazard() as $hazard) {
  70.                     //$content .= $hazard->getName() .' , ';
  71.                     $alertHazards[] = $hazard->getName('ar');
  72.                 }
  73.                 $parameters['alertHazards'] = $alertHazards;
  74.                 //$content .= " \n";
  75.             }
  76.             if ($ewsNotification->getGovernorate() && !empty($ewsNotification->getGovernorate())) {
  77.                 $governorates = [];
  78.                 //$content .= 'Governorate(s) Affected: ';
  79.                 foreach ($ewsNotification->getGovernorate() as $governorate) {
  80.                     $governorates[] = $governorate->getName('ar');
  81.                     //$content .= $governorate->getName() .' , ';
  82.                 }
  83.                 $parameters['governorateAffected'] = $governorates;
  84.                 //$content .= " \n";
  85.             }
  86.             //$content .= 'Alert Start Date: ' . (!empty($ewsNotification->getStartDate()) ? $ewsNotification->getStartDate()->format('Y-m-d h:i:sa') : 'Unknown') ." \n";
  87.             //$content .= 'Alert End Date: ' . (!empty($ewsNotification->getEndDate()) ? $ewsNotification->getEndDate()->format('Y-m-d h:i:sa') : 'Unknown') ." \n";
  88.             $parameters['alertStartDate'] = !empty($ewsNotification->getStartDate()) ? $ewsNotification->getStartDate()->format('Y-m-d h:i:sa') : 'Unknown';
  89.             $parameters['alertEndDate'] = !empty($ewsNotification->getEndDate()) ? $ewsNotification->getEndDate()->format('Y-m-d h:i:sa') : 'Unknown';
  90.             // $content .= 'Posted Today Date: ' . date('Y-m-d h:i:sa') ." \n";
  91.             // if (!empty($ewsNotification->getAttachment())) {
  92.             //     $attachmentFile = API_BASE_URL.'/'.$ewsNotification->getAttachment()->getFullPath();
  93.             //     $content .= 'Attachment : '. $attachmentFile ." \n";
  94.             // }
  95.             $HashTagcontent "";
  96.             if (!empty($ewsNotification->getRegion())) {
  97.                 $regionName $ewsNotification->getRegion()->getName('ar');
  98.                 $regionName str_replace(' ''_'$regionName);
  99.                 // $HashTagcontent .= "اسم المنطقة: #$regionName\n";
  100.             }
  101.             if ($ewsNotification->getGovernorate() && !empty($ewsNotification->getGovernorate())) {
  102.                 // $HashTagcontent .= "المحافظات المتأثرة: ";
  103.                 foreach ($ewsNotification->getGovernorate() as $governorate) {
  104.                     $govName $governorate->getName('ar');
  105.                     // $HashTagcontent .= "#$govName, ";
  106.                 }
  107.                 // $HashTagcontent = rtrim($HashTagcontent, ', ');
  108.                 // $HashTagcontent .= "\n";
  109.             }
  110.             // $HashTagcontent .= "#الإنذار_المبكر #طقس_السعودية #المركز_الوطني_للأرصاد";
  111.             // $HashTagcontent .= "";
  112.             
  113.             $HashTagcontent $ewsNotification->getXPost();
  114.             
  115.             $parameters['furtherInformation'] = "https://ncm.gov.sa/Ar/alert/Pages/default.aspx";
  116.             // $content .= "For further information visit: https://ncm.gov.sa/Ar/alert/Pages/default.aspx \n";
  117.             
  118.             // $html = $this->twig->render('image_templates/x-post-template.html.twig', $parameters);
  119.             // $this->snappyImage->setOption('enable-local-file-access', true);
  120.             // $this->snappyImage->setOption('width', '787');
  121.             // $this->snappyImage->setOption('height', '797');
  122.             // $this->snappyImage->setOption('format', 'png');
  123.             // $image = $this->snappyImage->getOutputFromHtml($html);
  124.             // $asset = \App\Lib\Utility::createAsset($image, rand(0, 10000) . '_' . time() . '_custom_weather_report.jpeg', 'EWS Twitter Images');
  125.             // $asset_path = PIMCORE_PROJECT_ROOT . '/public/var/assets' . $asset->getPath() . $asset->getFileName();
  126.             // $tweet = $this->twitterService->uploadMedia($asset_path,$HashTagcontent);
  127.             $xPost PIMCORE_PROJECT_ROOT '/public/var/assets' .$ewsNotification->getXAttachment()->getPath() . $ewsNotification->getXAttachment()->getFileName();
  128.             $xPostAssets $ewsNotification->getXAttachment();
  129.             $tweet $this->twitterService->uploadMedia($xPost,$HashTagcontent);
  130.             if ($tweet) {
  131.                 // $tweet = $this->twitterService->postTweet('', $media_id);
  132.                 // $twitterResponse = $this->c2Service->sendTwitterTweetResponse($_ENV['TWITTER_POST_TEMPLATE'], $ewsNotification->getId(), $html);
  133.                 if(isset($tweet['result']->data)){
  134.                     $tweet_text $tweet['result']?->data?->text;
  135.                     preg_match('/https?:\/\/\S+/'$tweet_text$matches);
  136.                     $url $matches[0] ?? null;
  137.                     $twitterResponse $this->c2Service->addAsset([$xPostAssets],$url);
  138.                 }
  139.               
  140.                 // $tweet = $this->twitterService->postTweet('', $media_id);
  141.                 // $twitterResponse = $this->c2Service->sendTwitterTweetResponse($_ENV['TWITTER_POST_TEMPLATE'], $ewsNotification->getId(), $html, $url);
  142.                 // $asset->delete();
  143.                 if (!empty($tweet['tweetId'])) {
  144.                     $ewsNotification->setTwitterId($tweet['tweetId']);
  145.                 }
  146.     
  147.                 $ewsNotification->setTwitterLog($tweet['data']);
  148.                 $ewsNotification->save();
  149.     
  150.                 
  151.                 if ($tweet['httpCode'] === 200 || $tweet['httpCode'] === 201 || $tweet['httpCode'] === 204) {
  152.                     
  153.                 }
  154.                 elseif ($tweet['httpCode'] === 403) {
  155.                     // Forbidden - Duplicate content error
  156.                     $responseData json_decode($tweet['data'], true);
  157.                     $errorMessage $responseData['detail'];
  158.                     // echo $errorMessage;
  159.                 } else {
  160.                     // echo 'Unexpected HTTP status code: ' . $tweet['httpCode'];
  161.                 }
  162.             }
  163.             // $tweet = $this->twitterService->postTweet($content);
  164.             
  165.             
  166.             // echo 'POST_ID : '.$tweet['tweetId']. ' EWS_ID : '.$ewsNotification->getId(). ' , ';        
  167.         }
  168.     }
  169.     public function onObjectUpdate(DataObjectEvent $ewsNotificationObject)
  170.     {
  171.         $ewsNotification $ewsNotificationObject->getObject();
  172.         if (
  173.             ($ewsNotification instanceof EwsNotification) &&
  174.             ($ewsNotification->isPublished(true)) &&
  175.             $ewsNotification->getEnableTwitterNotification() == true
  176.         ) {
  177.             $governates $ewsNotification->getGovernorate();
  178.             $startDate $ewsNotification->getStartDate()->format('Y-m-d');
  179.             $currentDate Carbon::now();
  180.             $targetDate Carbon::parse($startDate);
  181.             $content '';
  182.             $parameters = [];
  183.             if (!empty($ewsNotification->getRegion())) {
  184.                // $content .= 'Region : '. $ewsNotification->getRegion()->getName('en')  ." \n";
  185.                 $parameters['region'] = $ewsNotification->getRegion()->getName('ar');
  186.             }
  187.             if (!empty($ewsNotification->getAlertAction())) {
  188.                // $content .= 'Alert Action: ';
  189.                 $alertActions = [];
  190.                 foreach ($ewsNotification->getAlertAction() as $alertAction) {
  191.                     $alertActions[] = $alertAction->getName('ar');
  192.                     //$content .= $alertAction->getName() .' , ';
  193.                 }
  194.                 $parameters['alertAction'] = $alertActions;
  195.                 //$content .= " \n";
  196.             }
  197.             if (!empty($ewsNotification->getAlertType())) {
  198.                 $parameters['alertType'] = $ewsNotification->getAlertType()->getName('en');
  199.                 //$content .= 'Alert Type: ' . $ewsNotification->getAlertType()->getColor() ." \n";
  200.             }
  201.             if (!empty($ewsNotification->getWeatherPhenomenon())) {
  202.                 $parameters['weatherPhenomenon'] = $ewsNotification->getWeatherPhenomenon()->getTitle('en');
  203.                 $parameters['alertStatusAr'] = $ewsNotification->getWeatherPhenomenon()->getTitle('ar');
  204.                 //$content .= 'Weather Phenomenon: ' . $ewsNotification->getWeatherPhenomenon()->getTitle() ." \n";
  205.             }
  206.             
  207.             if (!empty($ewsNotification->getAlertHazard())) {
  208.                 $alertHazards = [];
  209.                 //$content .= 'Alert Hazards: ';
  210.                 foreach ($ewsNotification->getAlertHazard() as $hazard) {
  211.                     $alertHazards[] = $hazard->getName('ar');
  212.                     //$content .= $hazard->getName() .' , ';
  213.                 }
  214.                 $parameters['alertHazards'] = $alertHazards;
  215.                 //$content .= " \n";
  216.             }
  217.             if ($ewsNotification->getGovernorate() && !empty($ewsNotification->getGovernorate())) {
  218.                 //$content .= 'Governorate(s) Affected: ';
  219.                 $governorates = [];
  220.                 foreach ($ewsNotification->getGovernorate() as $governorate) {
  221.                     $governorates[] = $governorate->getName('ar');
  222.                     //$content .= $governorate->getName() .' , ';
  223.                 }
  224.                 $parameters['governorateAffected'] = $governorates;
  225.                 //$content .= " \n";
  226.             }
  227.             //$content .= 'Alert Start  Date: ' . (!empty($ewsNotification->getStartDate()) ? $ewsNotification->getStartDate()->format('Y-m-d h:i:sa') : 'Unknown') ." \n";
  228.             //$content .= 'Alert End Date: ' . (!empty($ewsNotification->getEndDate()) ? $ewsNotification->getEndDate()->format('Y-m-d h:i:sa') : 'Unknown') ." \n";
  229.             $parameters['alertStartDate'] = !empty($ewsNotification->getStartDate()) ? $ewsNotification->getStartDate()->format('Y-m-d h:i:sa') : 'Unknown';
  230.             $parameters['alertEndDate'] = !empty($ewsNotification->getEndDate()) ? $ewsNotification->getEndDate()->format('Y-m-d h:i:sa') : 'Unknown';
  231.             // $content .= 'Posted Today Date: ' . date('Y-m-d h:i:sa') ." \n";
  232.             // if (!empty($ewsNotification->getAttachment())) {
  233.             //     $attachmentFile = API_BASE_URL.'/'.$ewsNotification->getAttachment()->getFullPath();
  234.             //     $content .= 'Attachment : '. $attachmentFile ." \n";
  235.             // }
  236.             //$content .= "For further information visit: https://ncm.gov.sa/Ar/alert/Pages/default.aspx \n";
  237.             $HashTagcontent "";
  238.             if (!empty($ewsNotification->getRegion())) {
  239.                 $regionName $ewsNotification->getRegion()->getName('ar');
  240.                 $regionName str_replace(' ''_'$regionName);
  241.                 // $HashTagcontent .= "اسم المنطقة: #$regionName\n";
  242.             }
  243.             if ($ewsNotification->getGovernorate() && !empty($ewsNotification->getGovernorate())) {
  244.                 // $HashTagcontent .= "المحافظات المتأثرة: ";
  245.                 foreach ($ewsNotification->getGovernorate() as $governorate) {
  246.                     $govName $governorate->getName('ar');
  247.                     // $HashTagcontent .= "#$govName, ";
  248.                 }
  249.                 // $HashTagcontent = rtrim($HashTagcontent, ', ');
  250.                 // $HashTagcontent .= "\n";
  251.             }
  252.             // $HashTagcontent .= "#الإنذار_المبكر #طقس_السعودية #المركز_الوطني_للأرصاد";
  253.             // $HashTagcontent .= "";
  254.             $HashTagcontent $ewsNotification->getXPost();
  255.             $parameters['furtherInformation'] = "https://ncm.gov.sa/Ar/alert/Pages/default.aspx";
  256.             // $html = $this->twig->render('image_templates/x-post-template.html.twig', $parameters);
  257.             
  258.             // $this->snappyImage->setOption('enable-local-file-access', true);
  259.             // $this->snappyImage->setOption('width', '787');
  260.             // $this->snappyImage->setOption('height', '797');
  261.             // $this->snappyImage->setOption('format', 'png');
  262.             // $image = $this->snappyImage->getOutputFromHtml($html);
  263.             // $asset = \App\Lib\Utility::createAsset($image, rand(0, 10000) . '_' . time() . '_custom_weather_report.jpeg', 'EWS Twitter Images');
  264.             // $asset_path = PIMCORE_PROJECT_ROOT . '/public/var/assets' . $asset->getPath() . $asset->getFileName();
  265.             // $tweet = $this->twitterService->uploadMedia($asset_path, $HashTagcontent);
  266.             $xPost PIMCORE_PROJECT_ROOT '/public/var/assets' .$ewsNotification->getXAttachment()->getPath() . $ewsNotification->getXAttachment()->getFileName();
  267.             $xPostAssets $ewsNotification->getXAttachment();
  268.             $tweet $this->twitterService->uploadMedia($xPost$HashTagcontent);
  269.             if ($tweet) {
  270.                 if(isset($tweet['result']->data)){
  271.                     $tweet_text $tweet['result']?->data?->text;
  272.                     preg_match('/https?:\/\/\S+/'$tweet_text$matches);
  273.                     $url $matches[0] ?? null;
  274.                   
  275.                     // $tweet = $this->twitterService->postTweet('', $media_id);
  276.                     // $twitterResponse = $this->c2Service->sendTwitterTweetResponse($_ENV['TWITTER_POST_TEMPLATE'], $ewsNotification->getId(), $html, $url);
  277.                     // $twitterResponse = $this->c2Service->addAsset([$asset],$url);
  278.                     // $asset->delete();
  279.               
  280.                     $twitterResponse $this->c2Service->addAsset([$xPostAssets],$url);
  281.                 }
  282.                 if (!empty($tweet['tweetId'])) {
  283.                     $ewsNotification->setTwitterId($tweet['tweetId']);
  284.                 }
  285.     
  286.                 $ewsNotification->setTwitterLog($tweet['data']);
  287.                 $ewsNotification->save();
  288.     
  289.                 
  290.                 if ($tweet['httpCode'] === 200 || $tweet['httpCode'] === 201 || $tweet['httpCode'] === 204) {
  291.                     
  292.                 }
  293.                 elseif ($tweet['httpCode'] === 403) {
  294.                     // Forbidden - Duplicate content error
  295.                     $responseData json_decode($tweet['data'], true);
  296.                     $errorMessage $responseData['detail'];
  297.                     // echo $errorMessage;
  298.                 } else {
  299.                     // echo 'Unexpected HTTP status code: ' . $tweet['httpCode'];
  300.                 }
  301.             }
  302.             //$tweet = $this->twitterService->postTweet($content);
  303.             
  304.             // echo 'POST_ID : '.$tweet['tweetId']. ' EWS_ID : '.$ewsNotification->getId(). ' , ';
  305.         }
  306.     }
  307. }