/** * Run the queue web runner. * * @param Request $request The request. * * @return JsonResponse|mixed * * @SuppressWarnings(PHPMD.ExitExpression) */ public function run(Request $request) { $queueRepository = EntityHelper::getRepository('Avisota\\Contao:Queue'); $queueId = $request->get('id'); $queue = $queueRepository->find($queueId); /** @var \Avisota\Contao\Entity\Queue $queue */ if (!$queue) { header("HTTP/1.0 404 Not Found"); echo '<h1>404 Not Found</h1>'; exit; } $user = \BackendUser::getInstance(); $user->authenticate(); try { return $this->execute($request, $queue, $user); } catch (\Exception $exception) { // Todo i can't find where this output $response = new JsonResponse(array('error' => sprintf('%s in %s:%d', $exception->getMessage(), $exception->getFile(), $exception->getLine())), 500); $response->prepare($request); return $response; } }
/** * Execute the queue. * * @param Request $request The request. * @param Queue $queueData The queue data. * @param \BackendUser $user The user. * * @return JsonResponse * * @SuppressWarnings(PHPMD.Superglobals) * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ protected function execute(Request $request, Queue $queueData, \BackendUser $user) { global $container; if (!$queueData->getAllowManualSending()) { $response = new JsonResponse(array('error' => 'manual sending is forbidden'), 403); $response->prepare($request); return $response; } $serviceName = sprintf('avisota.queue.%s', $queueData->getId()); /** @var QueueInterface $queue */ $queue = $container[$serviceName]; $transportServiceName = sprintf('avisota.transport.%s', $queueData->getTransport()->getId()); $transport = $container[$transportServiceName]; $config = new ExecutionConfig(); if ($queueData->getMaxSendTime() > 0) { $config->setTimeLimit($queueData->getMaxSendTime()); } if ($queueData->getMaxSendCount() > 0) { $config->setMessageLimit($queueData->getMaxSendCount()); } $event = new PreQueueExecuteEvent($queue, $transport, $config); /** @var EventDispatcher $eventDispatcher */ $eventDispatcher = $GLOBALS['container']['event-dispatcher']; $eventDispatcher->dispatch(PreQueueExecuteEvent::NAME, $event); $queue = $event->getQueue(); $transport = $event->getTransport(); $config = $event->getConfig(); $status = $queue->execute($transport, $config); $jsonData = array('success' => 0, 'failed' => 0); foreach ($status as $stat) { $jsonData['success'] += $stat->getSuccessfullySend(); $jsonData['failed'] += count($stat->getFailedRecipients()); } $response = new JsonResponse($jsonData); $response->prepare($request); return $response; }
/** * Method prepare response - add Cache directives and etag * @author Krzysztof Bednarczyk * @param Request $request * @return \Symfony\Component\HttpFoundation\Response * @throws \Exception */ public function prepare(Request $request) { $this->setPrivate(); $this->setMaxAge(0); $this->setSharedMaxAge(0); $this->headers->addCacheControlDirective('no-cache', true); $this->headers->addCacheControlDirective('max-age', 0); $this->headers->addCacheControlDirective('must-revalidate', true); $this->headers->addCacheControlDirective('no-store', true); $this->headers->set("X-Powered-By", "XV-Server v1.0"); $this->setData(array("handlers" => array_values($this->handlers))); $this->setEtag(uniqid()); $this->setMaxAge(0); $this->setLastModified(new DateTime()); return parent::prepare($request); }
/** * Instantiate Symfony HTTP Foundation's JsonResponse * using Symfony's HTTP Foundation Request * * The Response Interface is used to populate RAISe Response */ public function __construct() { $this->httpResponse = new JsonResponse(); $this->httpResponse->prepare(Request::createFromGlobals()); $this->httpResponse->setEncodingOptions(JSON_PRETTY_PRINT); }