<?php
namespace App\EventSubscriber;
use Carbon\Carbon;
use Pimcore\Mail;
use Pimcore\Model\DataObject;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Model\DataObject\EwsNotification;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use App\Service\TwitterService;
use App\C2IntegrationBundle\Service\C2Service;
use Symfony\Component\Templating\EngineInterface;
use Knp\Snappy\Pdf;
use Knp\Snappy\Image;
use Pimcore\Log\ApplicationLogger;
class TwitterEwsEndEventListner
{
private TwitterService $twitterService;
private C2Service $c2Service;
private ApplicationLogger $logger;
public function __construct(
private Pdf $snappy,
private Image $snappyImage,
private EngineInterface $twig,
ApplicationLogger $logger
) {
$this->twitterService = new TwitterService();
$this->c2Service = new C2Service();
$this->logger = $logger;
}
public function onObjectUpdate(DataObjectEvent $ewsNotificationObject)
{
$ewsNotification = $ewsNotificationObject->getObject();
if (
($ewsNotification instanceof EwsNotification) &&
($ewsNotification->isPublished(true)) &&
$ewsNotification->getEnableTwitterNotification() === true &&
$ewsNotification->getStatus() === "ended"
) {
try {
$parameters = [
'searchEwsIdAr' => $ewsNotification->getEwsSearchId("ar"),
'host' => API_BASE_URL,
'alertColor' => $ewsNotification->getAlertType()->getColor()
];
$HashTagcontent = "#alertEnded";
$html = $this->twig->render('web2print/_manned_alert_ended_notification_ar.html.twig', $parameters);
$this->snappyImage->setOption('enable-local-file-access', true);
$this->snappyImage->setOption('width', '787');
$this->snappyImage->setOption('height', '797');
$this->snappyImage->setOption('format', 'png');
$image = $this->snappyImage->getOutputFromHtml($html);
$asset = \App\Lib\Utility::createAsset(
$image,
rand(0, 10000) . '_' . time() . 'end_ews_alert.jpeg',
'END EWS Twitter Images'
);
$asset_path = PIMCORE_PROJECT_ROOT . '/public/var/assets' . $asset->getPath() . $asset->getFileName();
$tweet = $this->twitterService->uploadMedia($asset_path, $HashTagcontent);
if ($tweet) {
if (isset($tweet['result']->data)) {
$tweet_text = $tweet['result']?->data?->text;
preg_match('/https?:\/\/\S+/', $tweet_text, $matches);
$url = $matches[0] ?? null;
$this->c2Service->addAsset([$asset], $url);
}
if (!empty($tweet['tweetId'])) {
$ewsNotification->setTwitterId($tweet['tweetId']);
}
$ewsNotification->setTwitterLog($tweet['data']);
$ewsNotification->save();
$logContext = [
'ewsId' => $ewsNotification->getId(),
'tweetId' => $tweet['tweetId'] ?? null,
'httpCode' => $tweet['httpCode'],
'response' => $tweet['data']
];
if (in_array($tweet['httpCode'], [200, 201, 204])) {
$this->logger->info("Twitter post successful for EWS ID: {$ewsNotification->getId()}", $logContext);
} elseif ($tweet['httpCode'] === 403) {
$responseData = json_decode($tweet['data'], true);
$errorMessage = $responseData['detail'] ?? 'Forbidden';
$this->logger->error("Twitter post failed (403) for EWS ID: {$ewsNotification->getId()} - $errorMessage", $logContext);
} else {
$this->logger->error("Twitter post failed (HTTP {$tweet['httpCode']}) for EWS ID: {$ewsNotification->getId()}", $logContext);
}
} else {
$this->logger->error("No tweet response for EWS ID: {$ewsNotification->getId()}");
}
} catch (\Throwable $e) {
$this->logger->error("Exception occurred while posting to Twitter for EWS ID: {$ewsNotification->getId()} - " . $e->getMessage(), [
'exception' => $e->getTraceAsString()
]);
}
}
}
}