<?php
namespace App\Admin\Controller;
use App\Application\Controller\AbstractController;
use App\Application\Manager\GenericManager;
use App\Application\Messenger\ErrorResponseBuilder;
use App\Application\Service\BubbleNotificationService;
use App\Domain\Entity\PointOfInterest;
use App\Domain\Entity\PointOfInterestGps;
use App\Infrastructure\Repository\AwardRepository;
use App\Infrastructure\Repository\PointOfInterestGpsRepository;
use App\Infrastructure\Repository\PointOfInterestRepository;
use App\Query\PointOfInterest\GetCommentsOverviewQuery;
use App\Query\PointOfInterest\GetJSONRevisionsLogQuery;
use App\Query\PointOfInterest\GetRevisionsQuery;
use Exception;
use JsonException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Routing\Annotation\Route;
/**
* Class LocaleController
*
* @package App\Admin\Controller
*/
class RevisionController extends AbstractController
{
public function __construct(
ErrorResponseBuilder $errorResponseBuilder,
MessageBusInterface $commandBus,
MessageBusInterface $queryBus,
private readonly BubbleNotificationService $bubbleNotificationService,
private readonly AwardRepository $awardsRepository,
private readonly PointOfInterestGpsRepository $pointOfInterestGpsRepository,
private readonly PointOfInterestRepository $pointOfInterestRepository,
private readonly GenericManager $genericManager,
) {
parent::__construct($errorResponseBuilder, $commandBus, $queryBus);
}
#[Route('/revisions/add', name: 'revision_add')]
public function add(): Response
{
return $this->render('admin/revision/add.html.twig', [
'notifications' => $this->bubbleNotificationService->getNotifications(),
'location' => 'revision_add',
]);
}
/**
* @throws JsonException
*/
#[Route('/point_of_interest/{uuid}/logs', name: 'poi_logs')]
public function logs(PointOfInterest $pointOfInterest): Response
{
$revisionLogsQuery = new GetJSONRevisionsLogQuery($pointOfInterest);
$revisionsLogs = $this->executeQuery($revisionLogsQuery);
return $this->render('admin/revision/logs.html.twig', [
'revisionsLogs' => json_encode($revisionsLogs, JSON_THROW_ON_ERROR),
'notifications' => $this->bubbleNotificationService->getNotifications(),
'location' => 'revision',
'pointOfInterest' => $pointOfInterest,
]);
}
/**
* @throws JsonException
*/
#[Route('/revisions/debug-gps', name: 'debug_gps')]
public function poiGps(): Response
{
/** @var PointOfInterestGps[] $pointOfInterestGPSs */
$pointOfInterestGPSs = $this->pointOfInterestGpsRepository->findBy(['isSolved' => false]);
$toDelete = array_filter(
$pointOfInterestGPSs,
static fn (PointOfInterestGps $gps) => $gps->revision1->isDeleted() || $gps->revision2->isDeleted(),
);
$this->genericManager->removeAll($toDelete);
if (count($toDelete)) {
$pointOfInterestGPSs = $this->pointOfInterestGpsRepository->findBy(['isSolved' => false]);
}
/** @var PointOfInterestGps|null $pointOfInterestGps */
$pointOfInterestGps = empty($pointOfInterestGPSs) ? null : $pointOfInterestGPSs[array_rand(
$pointOfInterestGPSs,
)];
return $this->render('admin/revision/debug-gps.html.twig', [
'revisions' => json_encode([
$pointOfInterestGps?->revision1->uuid,
$pointOfInterestGps?->revision2->uuid,
], JSON_THROW_ON_ERROR),
'notifications' => $this->bubbleNotificationService->getNotifications(),
'location' => 'revision_gps',
]);
}
/**
* @throws JsonException
* @throws Exception
*/
#[Route('/point_of_interest/certification', name: 'poi_certification_edit')]
public function certificationDetail(): Response
{
$pointOfInterest = $this->pointOfInterestRepository->findOldestPoiWithLastRevisionNotMain();
if (!$pointOfInterest) return $this->redirectToRoute('revision_index');
return $this->handleEdit($pointOfInterest, true);
}
/**
* @throws JsonException
*/
#[Route('/point_of_interest/{uuid}', name: 'poi_edit')]
public function edit(PointOfInterest $pointOfInterest): Response
{
return $this->handleEdit($pointOfInterest, false);
}
#[Route('/point_of_interest', name: 'revision_index')]
public function index(): Response
{
return $this->render('admin/revision/index.html.twig', [
'notifications' => $this->bubbleNotificationService->getNotifications(),
'location' => 'revision',
]);
}
/**
* @throws JsonException
*/
private function handleEdit(PointOfInterest $pointOfInterest, bool $certificationMode): Response
{
$revisionQuery = new GetRevisionsQuery($pointOfInterest);
$revisions = $this->executeQuery($revisionQuery);
$commentQuery = new GetCommentsOverviewQuery($pointOfInterest);
$commentsOverview = $this->executeQuery($commentQuery);
$pointOfInterestAwards = $this->awardsRepository->findByPointOfInterest($pointOfInterest);
$pointOfInterestAwards = array_map(static fn ($award) => "/api/awards/$award->uuid", $pointOfInterestAwards);
return $this->render('admin/revision/edit.html.twig', [
'totalComments' => $commentsOverview['total'],
'currentMainNumber' => $revisions['currentMainNumber'],
'newComments' => $commentsOverview['new'],
'pointOfInterest' => $pointOfInterest->uuid,
'revisions' => json_encode($revisions['revisions'], JSON_THROW_ON_ERROR),
'revisionUuid' => $revisions['revisions'][0]['uuid'],
'revisionId' => $revisions['revisions'][0]['id'],
'notifications' => $this->bubbleNotificationService->getNotifications(),
'location' => $certificationMode ? 'revision_certification' : 'revision',
'pointOfInterestAwards' => json_encode($pointOfInterestAwards, JSON_THROW_ON_ERROR),
'certificationMode' => $certificationMode ? 1 : 0,
]);
}
}