85 lines
3.1 KiB
PHP
85 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Controller\Api;
|
|
|
|
use App\BusProNet\DataProcessor\PickupPlanningTransformer;
|
|
use App\BusProNet\XmlLoader\PickupLoader;
|
|
use League\Flysystem\FilesystemException;
|
|
use League\Flysystem\FilesystemOperator;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\Routing\Attribute\Route;
|
|
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
|
|
|
#[Route('/api')]
|
|
#[IsGranted('ROLE_OAUTH2_API')]
|
|
class PickupController extends AbstractController
|
|
{
|
|
private const PLANNING_FILE = 'pickup_planning.json';
|
|
|
|
public function __construct(
|
|
private readonly PickupLoader $xmlLoader,
|
|
private readonly PickupPlanningTransformer $planningTransformer,
|
|
private readonly FilesystemOperator $jsonExport,
|
|
) {
|
|
}
|
|
|
|
#[Route('/pickups', name: 'api_pickups_all')]
|
|
public function index(): JsonResponse
|
|
{
|
|
$pickups = $this->xmlLoader->loadAll();
|
|
|
|
return $this->json($pickups, Response::HTTP_OK, [], ['groups' => 'api:list']);
|
|
}
|
|
|
|
#[Route('/pickups/{id}', name: 'api_pickups_single')]
|
|
public function single(int $id): JsonResponse
|
|
{
|
|
$pickup = $this->xmlLoader->loadById($id);
|
|
|
|
return $this->json($pickup, Response::HTTP_OK, [], ['groups' => 'api:list']);
|
|
}
|
|
|
|
#[Route('/pickups-planning/{travelCode}', name: 'api_pickups_planning_single', methods: ['GET'])]
|
|
public function planningByTravelCode(string $travelCode): JsonResponse
|
|
{
|
|
try {
|
|
if (false === $this->jsonExport->fileExists(self::PLANNING_FILE)) {
|
|
return $this->json(['error' => 'Planning data not available'], Response::HTTP_NOT_FOUND);
|
|
}
|
|
|
|
$planning = json_decode($this->jsonExport->read(self::PLANNING_FILE), true, 512, \JSON_THROW_ON_ERROR);
|
|
} catch (FilesystemException|\JsonException) {
|
|
return $this->json(['error' => 'Failed to read planning data'], Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
}
|
|
|
|
if (false === \array_key_exists($travelCode, $planning)) {
|
|
return $this->json(['error' => 'Travel code not found'], Response::HTTP_NOT_FOUND);
|
|
}
|
|
|
|
return $this->json($planning[$travelCode]);
|
|
}
|
|
|
|
#[Route('/pickups-planning', name: 'api_pickups_planning', methods: ['POST'])]
|
|
public function planning(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$payload = $request->toArray();
|
|
} catch (\JsonException) {
|
|
return $this->json(['error' => 'Invalid JSON payload'], Response::HTTP_BAD_REQUEST);
|
|
}
|
|
|
|
$transformed = $this->planningTransformer->transform($payload);
|
|
|
|
try {
|
|
$this->jsonExport->write(self::PLANNING_FILE, json_encode($transformed, \JSON_THROW_ON_ERROR));
|
|
} catch (FilesystemException|\JsonException) {
|
|
return $this->json(['error' => 'Failed to write planning data'], Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
}
|
|
|
|
return $this->json(['status' => 'ok']);
|
|
}
|
|
}
|