<?php
namespace App\Controller;
use App\Services\ActionLoggerService;
use App\Services\AlertService;
use App\Services\BetterService;
use App\Services\BookingCatalogService;
use App\Services\EventService;
use App\Services\EventSynchroniserService;
use App\Services\GeographicService;
use App\Services\NewslettersService;
use App\Services\NotificationAlertService;
use App\Services\OssService;
use App\Services\SplunkService;
use App\Services\WorldlinkService;
use DateTime;
use Exception;
use FOS\RestBundle\Controller\Annotations\Get;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class CronJobsController extends AbstractController
{
/** @var AlertService $alertService */
protected $alertService;
/** @var EventSynchroniserService $eventSynchroniserService */
protected $eventSynchroniserService;
/** @var OssService $ossService */
protected $ossService;
/** @var GeographicService $geographicService */
protected $geographicService;
/** @var CacheItemPoolInterface $cache */
protected $cache;
/** @var EventService $eventService */
protected $eventService;
/** @var WorldlinkService $worldlinkService */
protected $worldlinkService;
/** @var SplunkService $splunkService */
protected $splunkService;
/** @var BetterService $betterService */
protected $betterService;
/** @var NewslettersService $newslettersService */
protected $newslettersService;
/** @var NotificationAlertService $notificationAlertService */
private $notificationAlertService;
/** @var ActionLoggerService $actionLoggerService */
private $actionLoggerService;
/** @var BookingCatalogService $bookingCatalogService */
private $bookingCatalogService;
/**
* CronJobsController constructor.
*
* @param AlertService $alertService
* @param NotificationAlertService $notificationAlertService
* @param ActionLoggerService $actionLoggerService
* @param EventSynchroniserService $eventSynchroniserService
* @param OssService $ossService
* @param BookingCatalogService $bookingCatalogService
* @param GeographicService $geographicService
* @param CacheItemPoolInterface $cache
* @param EventService $eventService
* @param WorldlinkService $worldlinkService
* @param SplunkService $splunkService
* @param BetterService $betterService
* @param NewslettersService $newslettersService
*/
public function __construct(
AlertService $alertService,
NotificationAlertService $notificationAlertService,
ActionLoggerService $actionLoggerService,
EventSynchroniserService $eventSynchroniserService,
OssService $ossService,
BookingCatalogService $bookingCatalogService,
GeographicService $geographicService,
CacheItemPoolInterface $cache,
EventService $eventService,
WorldlinkService $worldlinkService,
SplunkService $splunkService,
BetterService $betterService,
NewslettersService $newslettersService
)
{
$this->alertService = $alertService;
$this->actionLoggerService = $actionLoggerService;
$this->notificationAlertService = $notificationAlertService;
$this->eventSynchroniserService = $eventSynchroniserService;
$this->ossService = $ossService;
$this->geographicService = $geographicService;
$this->bookingCatalogService = $bookingCatalogService;
$this->cache = $cache;
$this->eventService = $eventService;
$this->worldlinkService = $worldlinkService;
$this->splunkService = $splunkService;
$this->betterService = $betterService;
$this->newslettersService = $newslettersService;
}
/**
* @Route("/cron/update_all_events", name="cron_update_events", methods={"GET"})
* @return JsonResponse
*/
public function createUpdateAllEventsAction()
{
try {
$this->eventSynchroniserService->updateEvents();
//to update the cache of events
$this->bookingCatalogService->getBookableEvents(true);
$this->actionLoggerService->logAction("CRON_EVENTS_ALL_UPDATED", "EVENT");
return $this->json("Events updated");
} catch (Exception $e) {
return $this->json("Error to update events: " . $e->getMessage(), 500);
}
}
/**
* @Route("/cron/neos/update_event/{eventNo}", name="cron_update_event_by_no", methods={"GET"})
* @param string $eventNo
* @return JsonResponse
*/
public function updateEventFromNEOSAction(string $eventNo)
{
try {
$this->eventSynchroniserService->updateFromEventNo($eventNo);
//to update the cache of events
$this->bookingCatalogService->getBookableEvents(true);
$this->actionLoggerService->logAction("CRON_EVENTS_EVENT_UPDATED_FROM_NEOS", "EVENT", $eventNo);
return $this->json("Event updated: " . $eventNo);
} catch (Exception $e) {
return $this->json("Error to update events: " . $e->getMessage(), 500);
}
}
/**
* @Route("/cron/update_all_events_categories", name="cron_update_events_categories", methods={"GET"})
* @return JsonResponse
*/
public function updateEventCategoriesAction()
{
try {
$this->eventSynchroniserService->updateEventCategories();
$this->actionLoggerService->logAction("CRON_EVENTS_CATEGORIES_UPDATED", "EVENT");
return $this->json("Event categories updated");
} catch (Exception $e) {
return $this->json("Error to update events categories: " . $e->getMessage(), 500);
}
}
/**
* @Route("/cron/update_all_distances", name="cron_update_distances", methods={"GET"})
* @return JsonResponse
*/
public function updateLocationsDistancesAction()
{
try {
$this->geographicService->updateDistancesBetweenLocations();
$this->actionLoggerService->logAction("CRON_DISTANCES_UPDATED", "OSS");
return $this->json("Distance updated");
} catch (Exception $e) {
return $this->json("Error to update distance: " . $e->getMessage(), 500);
}
}
/**
* @Route("/cron/update_all_oss", name="cron_update_oss", methods={"GET"})
* @return JsonResponse
*/
public function updateAllOssAction()
{
try {
$this->ossService->updateOssUni();
//to update the cache of events
$this->bookingCatalogService->getBookableOssUni(true, true);
$this->actionLoggerService->logAction("CRON_OSS_ALL_UPDATED", "OSS");
return $this->json("All OSS updated");
} catch (Exception $e) {
return $this->json("Error to update oss: " . $e->getMessage(), 500);
}
}
/**
* @Route("/cron/update_booking_oss_origins", name="cron_update_booking_oss_origins", methods={"GET"})
* @return JsonResponse
*/
public function updateBookingOssOriginsAction()
{
try {
//to update the cache of events
$this->bookingCatalogService->getBookableOssUni(true, true);
$this->actionLoggerService->logAction("CRON_OSS_USED_FOR_BOOKING_UPDATED", "OSS");
return $this->json("OSS used for Booking origins updated in cache");
} catch (Exception $e) {
$this->actionLoggerService->logException($e, "Problem during the update of oss origins for bookings (updateBookingOssOriginsAction)");
return $this->json("Error to update oss origins for bookings: " . $e->getMessage(), 500);
}
}
/**
* @Route("/cron/update_booking_event_origins", name="cron_update_booking_event_origins", methods={"GET"})
* @return JsonResponse
*/
public function updateBookingEventOriginsAction()
{
try {
//to update the cache of events
$this->bookingCatalogService->getBookableEvents(true);
$this->actionLoggerService->logAction("CRON_EVENTS_USED_FOR_BOOKING_UPDATED", "EVENT");
return $this->json("Events used for Booking origins updated in cache");
} catch (Exception $e) {
$this->actionLoggerService->logAction("CRON_EVENTS_USED_FOR_BOOKING_UPDATED", "EVENT");
$this->actionLoggerService->logException($e, "Problem during the update of events origins for bookings (updateBookingEventOriginsAction)");
return $this->json("Error to update events origins for bookings: " . $e->getMessage(), 500);
}
}
/**
* @Get("/cron/process_mails_tweets_events", name="cron_process-mails-tweets-events", methods={"GET"})
* @return JsonResponse
*/
public function sendMailsAndTweets()
{
//We have to use this kind of lock as to send mail we need to call entra and that could be long.
$keyCache = "cronSendMailsAndTweets";
if ($this->cache->hasItem($keyCache)) {
$this->actionLoggerService->logAction("CRON_TWEETS_AND_MAILS_PROCESSED_ON_GOING_STOPPED", "MAIL");
return $this->json("Tweets and mails are not updated as process is on going", 500);
}
$item = $this->cache->getItem($keyCache);
$item->set("ONGOING");
$this->cache->save($item);
try {
$this->notificationAlertService->sendMailAndTweetForFollowers();
$this->actionLoggerService->logAction("CRON_TWEETS_AND_MAILS_PROCESSED", "MAIL");
$this->cache->deleteItem($keyCache);
return $this->json("Tweets and mails successfully sent");
} catch (Exception $e) {
$this->actionLoggerService->logException($e, "sendMailsAndTweets error");
$this->cache->deleteItem($keyCache);
return $this->json("Error to send tweets and mails: " . $e->getMessage(), 500);
}
}
/**
* @Route("/intern/events/create_update_all_events", name="create_update_all_events", options={"expose"=true}, methods={"GET"})
* @return Response
*/
public function createUpdateAllEventsFromAdminAction()
{
$this->eventSynchroniserService->updateEvents();
return new Response(null);
}
/**
* Update events and go back to event list
* @Route("/intern/events/create_update_all_events_from_admin", name="create_update_alls_events_from_admin", options={"expose"=true}, methods={"GET"})
*
* @return Response
*/
public function createUpdateAllEventsFromAdminAndRedirectAction()
{
$this->eventSynchroniserService->updateEvents();
$this->bookingCatalogService->getBookableEvents(true);
/** @noinspection PhpRouteMissingInspection */
return $this->redirectToRoute('admin_app_event_list');
}
/**
* @Route("/intern/events/create_update_events_categories", name="create_update_events_categories", options={"expose"=true}, methods={"GET"})
* @Security("is_granted('ROLE_ADMINISTRATOR')")
* @return Response
*/
public function createUpdateAllEventCategoriesAction()
{
$this->eventSynchroniserService->updateEventCategories();
/** @noinspection PhpRouteMissingInspection */
return $this->redirectToRoute('admin_app_event_list');
}
/**
* @Route("/intern/locations/update_distances", name="update_locations_distances", options={"expose"=true}, methods={"GET"})
* @return Response
*/
public function updateLocationsDistancesFromAdminAction()
{
$this->geographicService->updateDistancesBetweenLocations();
/** @noinspection PhpRouteMissingInspection */
return $this->redirectToRoute('admin_app_location_list');
}
/**
* @Route("/intern/oss-partners/create_update_all_oss_partners", name="create_update_all_oss_partners", options={"expose"=true}, methods={"GET"})
* @return Response
*/
public function createUpdateAllOssPartnersAction()
{
$this->ossService->updateOssUni();
return new Response(null);
}
/**
* @Route("/intern/oss-partners/create_update_all_oss_partners_from_admin", name="create_update_all_oss_partners_from_admin", options={"expose"=true}, methods={"GET"})
* @return Response
*/
public function createUpdateAllOssPartnersFromAdminAction()
{
$this->ossService->updateOssUni();
/** @noinspection PhpRouteMissingInspection */
return $this->redirectToRoute('admin_app_osspartner_list');
}
/**
* @Route("/intern/oss-partners/create_update_oss_partner_from_admin/{ossPartnerId}", name="create_update_oss_from_admin", options={"expose"=true}, methods={"GET"})
* @param string $ossPartnerId the OSS Partner Id in NEOS that should be updated.
* @return Response
*/
public function createUpdateOssPartnerFromAdminAction(string $ossPartnerId)
{
$ossPartner = $this->ossService->updateOssPartner($ossPartnerId);
if ($ossPartner != null) {
/** @noinspection PhpRouteMissingInspection */
return $this->redirectToRoute('admin_app_osspartner_edit', ['id' => $ossPartner->getId()]);
} else {
/** @noinspection PhpRouteMissingInspection */
return $this->redirectToRoute('admin_app_osspartner_list');
}
}
/**
* Update events and go back to event list
* @Route("/intern/event/create_update_event_from_admin/{eventNo}", name="create_update_event_from_admin", options={"expose"=true}, methods={"GET"})
* @Security("is_granted('ROLE_ADMIN_EVENTS_ADMINISTRATOR')")
* @param string $eventNo the eventNo to be refreshed
* @return Response
*/
public function createUpdateEventFromAdminAction(string $eventNo)
{
$event = $this->eventSynchroniserService->updateFromEventNo($eventNo);
if ($event != null) {
/** @noinspection PhpRouteMissingInspection */
return $this->redirectToRoute('admin_app_event_edit', ['id' => $event->getId()]);
} else {
/** @noinspection PhpRouteMissingInspection */
return $this->redirectToRoute('admin_app_event_list');
}
}
/**
* @Get("/cron/process_worldfeed_reporting_mails", name="process_worldfeed_reporting_mails", methods={"GET"})
* @return JsonResponse
*/
public function sendWorldfeedReports()
{
// Get all worldfeed where endDate = now
$worldfeeds = $this->eventService->getAllWorldfeedByEndDate(new DateTime('-1 day'));
foreach ($worldfeeds as $worldfeed) {
$this->eventService->searchSpecificStatsWorldFeedInLocalDb($worldfeed->transmissionNo);
}
return $this->json("Worldfeed reports has been successfully sent");
}
/**
* @Get("/cron/process_worldlink_reporting_mails", name="process_worldlink_reporting_mails", methods={"GET"})
* @return JsonResponse
*/
public function sendWorldlinkReports()
{
// Get all worldlink where endDate = now
$worldlinks = $this->worldlinkService->getAllWorldlinkByEndDate(new DateTime('-1 day'));
foreach ($worldlinks as $worldlink) {
$this->splunkService->searchSpecificStatsWorldLinkSplunk($worldlink->getBetterId());
}
return $this->json("Worldlink reports has been successfully sent");
}
/**
* @Route("/cron/newsletter/campaign/update-last-campaign-status", name="cron_newsletter_update_last_campaign_status", options={"expose" = true })
* @return JsonResponse
*/
public function updateLastCampaignStatusInDBAction()
{
try {
$newsletters = $this->newslettersService->getLastCreatedNewsletters();
$objectIdDone = [];
foreach ($newsletters as $newsletter) {
if (!in_array($newsletter->getObjectId(), $objectIdDone)) {
$this->newslettersService->updateNewslettersStatus('events', $newsletter->getObjectId());
array_push($objectIdDone, $newsletter->getObjectId());
}
}
return $this->json('ok');
} catch (Exception $e) {
return $this->json('ko');
}
}
/**
* @Route("/cron/newsletter/campaign/update-sending-campaign-status", name="cron_newsletter_update_sending_campaign_status", options={"expose" = true })
* @return JsonResponse
*/
public function updateSendingCampaignStatusInDBAction()
{
$res = $this->newslettersService->updateSendingCampaignStatusInDB();
return new JsonResponse(array(
'message' => $res->getMessage(),
'status' => $res->getStatus(),
'data' => $res->getData(),
));
}
/**
* @Get("/cron/process_worldlink_cleaning", name="process_worldlink_cleaning", methods={"GET"})
* @return JsonResponse
*/
public function worldlinkCleaning()
{
$pastWorldlinks = $this->worldlinkService->getPublishedPastWorldlink();
$pastWorldlinkRemovedSuccessfully = [];
foreach ($pastWorldlinks as $pastWorldlink) {
$betterMedia = $this->betterService->getItemBetterMedia($pastWorldlink->getBetterId());
if (!empty($betterMedia)) {
$this->betterService->deleteMediaFromBetter($pastWorldlink->getBetterId());
$pastWorldlinkRemovedSuccessfully[] = $pastWorldlink;
}
$this->worldlinkService->updateBetterStatusWorldLink($pastWorldlink);
}
return $this->json("Past Worldlinks have been removed from Better");
}
}