feat: api endpoints
This commit is contained in:
@@ -2,13 +2,28 @@
|
||||
|
||||
namespace App\BusProNet\Model;
|
||||
|
||||
use Symfony\Component\Serializer\Attribute\Groups;
|
||||
|
||||
class Hotel
|
||||
{
|
||||
#[Groups(['api:list', 'api:single'])]
|
||||
public ?int $id = null;
|
||||
|
||||
#[Groups(['api:list', 'api:single'])]
|
||||
public ?string $code = null;
|
||||
|
||||
#[Groups(['api:list', 'api:single'])]
|
||||
public ?string $name = null;
|
||||
|
||||
#[Groups(['api:list', 'api:single'])]
|
||||
public ?string $city = null;
|
||||
|
||||
#[Groups(['api:list', 'api:single'])]
|
||||
public ?string $street = null;
|
||||
|
||||
#[Groups(['api:list', 'api:single'])]
|
||||
public ?string $phone = null;
|
||||
|
||||
#[Groups(['api:list', 'api:single'])]
|
||||
public ?string $country = null;
|
||||
}
|
||||
@@ -2,14 +2,34 @@
|
||||
|
||||
namespace App\BusProNet\Model;
|
||||
|
||||
use Symfony\Component\Serializer\Attribute\Context;
|
||||
use Symfony\Component\Serializer\Attribute\Groups;
|
||||
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
|
||||
|
||||
class Pickup
|
||||
{
|
||||
#[Groups(['api:list'])]
|
||||
public ?int $id = null;
|
||||
|
||||
#[Groups(['api:list'])]
|
||||
public ?string $code = null;
|
||||
|
||||
#[Groups(['api:list'])]
|
||||
public ?string $city = null;
|
||||
|
||||
#[Groups(['api:list'])]
|
||||
public ?string $postalCode = null;
|
||||
|
||||
#[Groups(['api:list'])]
|
||||
public ?string $street = null;
|
||||
|
||||
#[Groups(['api:single'])]
|
||||
#[Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d H:i'])]
|
||||
public ?\DateTimeImmutable $time = null;
|
||||
|
||||
#[Groups(['api:single'])]
|
||||
public ?float $price = null;
|
||||
|
||||
#[Groups(['api:booking'])]
|
||||
public array $mapping = [];
|
||||
}
|
||||
@@ -2,24 +2,65 @@
|
||||
|
||||
namespace App\BusProNet\Model;
|
||||
|
||||
use Symfony\Component\Serializer\Attribute\Context;
|
||||
use Symfony\Component\Serializer\Attribute\Groups;
|
||||
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
|
||||
|
||||
class Room
|
||||
{
|
||||
#[Groups(['api:list', 'api:single'])]
|
||||
public ?int $id = null;
|
||||
|
||||
#[Groups(['api:list', 'api:single'])]
|
||||
public ?string $category = null;
|
||||
|
||||
#[Groups(['api:list', 'api:single'])]
|
||||
public ?int $boardId = null;
|
||||
|
||||
#[Groups(['api:list', 'api:single'])]
|
||||
public ?string $code = null;
|
||||
|
||||
#[Groups(['api:list', 'api:single'])]
|
||||
public ?string $label = null;
|
||||
|
||||
#[Groups(['booking'])]
|
||||
#[Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
|
||||
public ?\DateTimeImmutable $dateFrom = null;
|
||||
|
||||
#[Groups(['booking'])]
|
||||
#[Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
|
||||
public ?\DateTimeImmutable $dateTo = null;
|
||||
|
||||
#[Groups(['booking'])]
|
||||
public ?int $totalCount = null;
|
||||
|
||||
#[Groups(['api:list', 'api:single'])]
|
||||
public ?int $minPax = null;
|
||||
|
||||
#[Groups(['api:list', 'api:single'])]
|
||||
public ?int $maxPax = null;
|
||||
|
||||
#[Groups(['api:list', 'api:single'])]
|
||||
public ?int $nights = null;
|
||||
|
||||
#[Groups(['api:list', 'api:single'])]
|
||||
public ?string $status = null;
|
||||
|
||||
#[Groups(['api:list', 'api:single'])]
|
||||
public ?int $available = null;
|
||||
|
||||
#[Groups(['api:list', 'api:single'])]
|
||||
public ?string $board = null;
|
||||
|
||||
#[Groups(['booking'])]
|
||||
public array $mapping = [];
|
||||
|
||||
#[Groups(['api:list', 'api:single'])]
|
||||
public ?float $price = null;
|
||||
|
||||
#[Groups(['booking'])]
|
||||
public ?float $totalPrice = null;
|
||||
|
||||
#[Groups(['booking'])]
|
||||
public array $individualPrice = [];
|
||||
}
|
||||
@@ -2,6 +2,10 @@
|
||||
|
||||
namespace App\BusProNet\Model;
|
||||
|
||||
use Symfony\Component\Serializer\Attribute\Context;
|
||||
use Symfony\Component\Serializer\Attribute\Groups;
|
||||
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
|
||||
|
||||
class Service
|
||||
{
|
||||
public const SOURCE_TRAVEL = 'travel';
|
||||
@@ -16,20 +20,50 @@ class Service
|
||||
public const TOKEN_BOARD = 'VPF';
|
||||
public const TOKEN_RENTALS = ['VER', 'VE2', 'VE3', 'VE4', 'VE5', 'VE6', 'VE7', 'VE8'];
|
||||
|
||||
#[Groups(['api:single', 'api:list'])]
|
||||
public ?int $id = null;
|
||||
public ?string $source = null;
|
||||
|
||||
#[Groups(['api:single', 'api:list'])]
|
||||
public ?string $category = null;
|
||||
|
||||
#[Groups(['api:single', 'api:list'])]
|
||||
public ?string $status = null;
|
||||
|
||||
#[Groups(['api:single', 'api:list'])]
|
||||
public bool $mandatory = false;
|
||||
|
||||
#[Groups(['api:single', 'api:list'])]
|
||||
public ?string $label = null;
|
||||
|
||||
#[Groups(['api:single', 'api:list'])]
|
||||
#[Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
|
||||
public ?\DateTimeImmutable $dateFrom = null;
|
||||
|
||||
#[Groups(['api:single', 'api:list'])]
|
||||
#[Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
|
||||
public ?\DateTimeImmutable $dateTo = null;
|
||||
|
||||
#[Groups(['api:single', 'api:list'])]
|
||||
public ?string $subType = null;
|
||||
|
||||
#[Groups(['booking'])]
|
||||
public ?int $totalCount = null;
|
||||
|
||||
#[Groups(['api:single'])]
|
||||
public array $mapping = [];
|
||||
|
||||
#[Groups(['api:single', 'api:list'])]
|
||||
public ?float $price = null;
|
||||
|
||||
#[Groups(['api:single'])]
|
||||
public ?float $totalPrice = null;
|
||||
|
||||
#[Groups(['api:single'])]
|
||||
public array $individualPrice = [];
|
||||
public ?string $direction = null;
|
||||
|
||||
#[Groups(['api:single', 'api:list'])]
|
||||
public ?int $available = null;
|
||||
|
||||
#[Groups(['api:single', 'api:list'])]
|
||||
public ?string $source = null;
|
||||
}
|
||||
@@ -2,25 +2,72 @@
|
||||
|
||||
namespace App\BusProNet\Model;
|
||||
|
||||
use Symfony\Component\Serializer\Attribute\Context;
|
||||
use Symfony\Component\Serializer\Attribute\Groups;
|
||||
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
|
||||
|
||||
class Travel
|
||||
{
|
||||
#[Groups(['api:list', 'api:single'])]
|
||||
public ?int $id = null;
|
||||
|
||||
#[Groups(['api:list', 'api:single'])]
|
||||
public ?string $code = null;
|
||||
|
||||
#[Groups(['api:list', 'api:single'])]
|
||||
public ?string $label = null;
|
||||
|
||||
#[Groups(['api:list', 'api:single'])]
|
||||
public ?int $hotelId = null;
|
||||
|
||||
#[Groups(['api:single'])]
|
||||
public ?Hotel $hotel = null;
|
||||
|
||||
#[Groups(['api:list', 'api:single'])]
|
||||
#[Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
|
||||
public ?\DateTimeImmutable $dateFrom = null;
|
||||
|
||||
#[Groups(['api:list', 'api:single'])]
|
||||
#[Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
|
||||
public ?\DateTimeImmutable $dateTo = null;
|
||||
|
||||
#[Groups(['api:list', 'api:single'])]
|
||||
public ?string $type = null;
|
||||
|
||||
#[Groups(['api:list', 'api:single'])]
|
||||
public ?float $priceFrom = null;
|
||||
|
||||
#[Groups(['api:single'])]
|
||||
public array $selectionGroups = [];
|
||||
|
||||
#[Groups(['api:single'])]
|
||||
public array $additionalServices = [];
|
||||
|
||||
#[Groups(['api:single'])]
|
||||
public bool $additionalServicesMutable = true;
|
||||
|
||||
#[Groups(['api:single'])]
|
||||
public array $transportationServices = [];
|
||||
|
||||
#[Groups(['api:single'])]
|
||||
public bool $transportationServicesMutable = true;
|
||||
|
||||
#[Groups(['api:single'])]
|
||||
public array $pickups = [];
|
||||
|
||||
#[Groups(['api:single'])]
|
||||
public bool $pickupsMutable = true;
|
||||
|
||||
#[Groups(['api:single'])]
|
||||
public array $rooms = [];
|
||||
|
||||
#[Groups(['api:single'])]
|
||||
public bool $roomsMutable = true;
|
||||
|
||||
#[Groups(['api:single'])]
|
||||
public bool $participantDataMutable = true;
|
||||
|
||||
#[Groups(['api:single'])]
|
||||
public bool $participantCountMutable = true;
|
||||
|
||||
public function getAdditionalServicesByGroup(mixed $group, bool $availableOnly = true): array
|
||||
|
||||
+2
-2
@@ -1,11 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\BusProNet\DataLoader;
|
||||
namespace App\BusProNet\XmlLoader;
|
||||
|
||||
use App\BusProNet\TypeConversionTrait;
|
||||
use Symfony\Contracts\Cache\CacheInterface;
|
||||
|
||||
abstract class AbstractDataLoader
|
||||
abstract class AbstractXmlLoader
|
||||
{
|
||||
use TypeConversionTrait;
|
||||
|
||||
+10
-2
@@ -1,12 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\BusProNet\DataLoader;
|
||||
namespace App\BusProNet\XmlLoader;
|
||||
|
||||
use App\BusProNet\Model\Hotel;
|
||||
use App\BusProNet\Model\Travel;
|
||||
use Psr\Cache\InvalidArgumentException;
|
||||
use Symfony\Contracts\Cache\ItemInterface;
|
||||
|
||||
class HotelDataLoader extends AbstractDataLoader
|
||||
class HotelXmlLoader extends AbstractXmlLoader
|
||||
{
|
||||
public function loadAll(?string $filename = 'hotel.xml'): array
|
||||
{
|
||||
@@ -72,4 +73,11 @@ class HotelDataLoader extends AbstractDataLoader
|
||||
|
||||
return $hotel;
|
||||
}
|
||||
|
||||
public function patchHotelDetails(Travel $travel): void
|
||||
{
|
||||
$hotelId = $travel->hotelId;
|
||||
$hotel = $this->loadById($hotelId);
|
||||
$travel->hotel = $hotel;
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -1,13 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\BusProNet\DataLoader;
|
||||
namespace App\BusProNet\XmlLoader;
|
||||
|
||||
use App\BusProNet\Model\Pickup;
|
||||
use App\BusProNet\Model\Travel;
|
||||
use Psr\Cache\InvalidArgumentException;
|
||||
use Symfony\Contracts\Cache\ItemInterface;
|
||||
|
||||
class PickupDataLoader extends AbstractDataLoader
|
||||
class PickupXmlLoader extends AbstractXmlLoader
|
||||
{
|
||||
public function loadAll(?string $filename = 'zustiege.xml'): array
|
||||
{
|
||||
+30
-15
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace App\BusProNet\DataLoader;
|
||||
namespace App\BusProNet\XmlLoader;
|
||||
|
||||
use App\BusProNet\Model\BaseData;
|
||||
use App\BusProNet\Model\CrmSelection;
|
||||
@@ -14,12 +14,12 @@ use Symfony\Component\Finder\Finder;
|
||||
use Symfony\Contracts\Cache\CacheInterface;
|
||||
use Symfony\Contracts\Cache\ItemInterface;
|
||||
|
||||
class TravelDataLoader extends AbstractDataLoader
|
||||
class TravelXmlLoader extends AbstractXmlLoader
|
||||
{
|
||||
public function __construct(
|
||||
private readonly HotelDataLoader $hotelDataLoader,
|
||||
CacheInterface $cache,
|
||||
?string $xmlPath = null
|
||||
private readonly HotelXmlLoader $hotelDataLoader,
|
||||
CacheInterface $cache,
|
||||
?string $xmlPath = null
|
||||
) {
|
||||
parent::__construct($cache, $xmlPath);
|
||||
}
|
||||
@@ -67,24 +67,24 @@ class TravelDataLoader extends AbstractDataLoader
|
||||
});
|
||||
}
|
||||
|
||||
public function loadById(int $id, ?string $filename = null): ?Travel
|
||||
public function loadById(int $travelId, ?int $hotelId = null, ?string $filename = null): ?Travel
|
||||
{
|
||||
if (null !== $filename) {
|
||||
return $this->loadXml($id, $filename);
|
||||
return $this->loadXml($travelId, $hotelId, $filename);
|
||||
}
|
||||
|
||||
$mapping = $this->generateMapping();
|
||||
|
||||
if (false === isset($mapping[$id])) {
|
||||
if (false === isset($mapping[$travelId])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$file = $mapping[$id]['file'];
|
||||
$file = $mapping[$travelId]['file'];
|
||||
|
||||
return $this->loadXml($id, $file);
|
||||
return $this->loadXml($travelId, $hotelId, $file);
|
||||
}
|
||||
|
||||
private function loadXml(int $id, string $filename): ?Travel
|
||||
private function loadXml(int $id, ?int $hotelId, string $filename): ?Travel
|
||||
{
|
||||
$xml = simplexml_load_file($filename);
|
||||
|
||||
@@ -94,16 +94,19 @@ class TravelDataLoader extends AbstractDataLoader
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->parseXml($travelXml[0]);
|
||||
return $this->parseXml($travelXml[0], $hotelId);
|
||||
}
|
||||
|
||||
public function parseXml(\SimpleXMLElement $xml): Travel
|
||||
public function parseXml(\SimpleXMLElement $xml, ?int $hotelId): Travel
|
||||
{
|
||||
$hotelXml = $this->getHotelXml($xml, $hotelId);
|
||||
|
||||
$attributes = $xml->attributes();
|
||||
|
||||
$travel = new Travel();
|
||||
$travel->id = (int) $attributes['idbuspro'];
|
||||
$travel->label = (string) $xml->text;
|
||||
$travel->hotelId = $hotelId;
|
||||
$travel->dateFrom = $this->stringToDate((string) $attributes['termin']);
|
||||
$travel->dateTo = $this->stringToDate((string) $attributes['bis']);
|
||||
$travel->code = (string) $attributes['code'];
|
||||
@@ -112,7 +115,7 @@ class TravelDataLoader extends AbstractDataLoader
|
||||
$travel->selectionGroups = $this->getSelectionGroups($xml);
|
||||
$travel->additionalServices = $this->getAdditionalServices($xml);
|
||||
$travel->transportationServices = $this->getTransportationServices($xml);
|
||||
$travel->rooms = $this->getRooms($xml);
|
||||
$travel->rooms = $this->getRooms($hotelXml);
|
||||
$travel->pickups = $this->getPickups($xml);
|
||||
|
||||
return $travel;
|
||||
@@ -213,11 +216,23 @@ class TravelDataLoader extends AbstractDataLoader
|
||||
return $pickups;
|
||||
}
|
||||
|
||||
public function getHotelXml(\SimpleXMLElement $xml, ?int $hotelId): ?\SimpleXMLElement
|
||||
{
|
||||
// In case not hotel id is provided, take the first hotel node (which is most probably the only one)
|
||||
if (null === $hotelId) {
|
||||
return $xml->hotel;
|
||||
}
|
||||
|
||||
$hotelXml = $xml->xpath(sprintf('//hotel[@idbuspro="%d"]', $hotelId));
|
||||
|
||||
return $hotelXml[0] ?? null;
|
||||
}
|
||||
|
||||
public function getRooms(\SimpleXMLElement $xml): array
|
||||
{
|
||||
$rooms = [];
|
||||
|
||||
foreach ($xml->hotel->zimmer->preis as $item) {
|
||||
foreach ($xml->zimmer->preis as $item) {
|
||||
$attributes = $item->attributes();
|
||||
$roomId = (int) $attributes['idbuspro_zimmer'];
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller\Api;
|
||||
|
||||
use App\BusProNet\DataProvider\CountryDataProvider;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
#[Route('/api')]
|
||||
class CountryController extends AbstractController
|
||||
{
|
||||
public function __construct(private readonly CountryDataProvider $dataProvider)
|
||||
{
|
||||
}
|
||||
|
||||
#[Route(path: '/countries', name: 'api_country_all')]
|
||||
public function index(): JsonResponse
|
||||
{
|
||||
$countries = $this->dataProvider->getAll();
|
||||
|
||||
return $this->json($countries);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller\Api;
|
||||
|
||||
use App\BusProNet\ApiClient;
|
||||
use App\BusProNet\Exception\ApiClientException;
|
||||
use App\BusProNet\Model\Notification;
|
||||
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;
|
||||
|
||||
#[Route('/api')]
|
||||
class CrmAttributesController extends AbstractController
|
||||
{
|
||||
public function __construct(private readonly APiClient $apiClient)
|
||||
{
|
||||
}
|
||||
|
||||
#[Route('/crm-attributes', name: 'api_crm_attributes', methods: ['GET'])]
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$email = $request->headers->get('x-bpn-email');
|
||||
$password = $request->headers->get('x-bpn-password');
|
||||
|
||||
if (null === $email || null === $password) {
|
||||
return new JsonResponse(['message' => 'Invalid credentials'], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
try {
|
||||
$data = $this->apiClient->getCrmAttributes($email, $password);
|
||||
|
||||
if ($data instanceof Notification) {
|
||||
return new JsonResponse(['message' => $data->message, 'code' => $data->code], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
return $this->json($data);
|
||||
} catch (ApiClientException $e) {
|
||||
return new JsonResponse(['message' => $e->getMessage()], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller\Api;
|
||||
|
||||
use App\BusProNet\XmlLoader\HotelXmlLoader;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
#[Route('/api')]
|
||||
class HotelController extends AbstractController
|
||||
{
|
||||
public function __construct(private readonly HotelXmlLoader $xmlLoader)
|
||||
{
|
||||
}
|
||||
|
||||
#[Route('/hotels', name: 'api_hotel_all')]
|
||||
public function index(): JsonResponse
|
||||
{
|
||||
$hotels = $this->xmlLoader->loadAll();
|
||||
|
||||
return $this->json($hotels, Response::HTTP_OK, [], ['groups' => 'api:list']);
|
||||
}
|
||||
|
||||
#[Route('/hotels/{id}', name: 'api_hotels_single')]
|
||||
public function single(int $id): JsonResponse
|
||||
{
|
||||
$hotel = $this->xmlLoader->loadById($id);
|
||||
|
||||
if (null === $hotel) {
|
||||
return new JsonResponse(['message' => 'Not found'], Response::HTTP_NOT_FOUND);
|
||||
}
|
||||
|
||||
return $this->json($hotel, Response::HTTP_OK, [], ['groups' => 'api:single']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller\Api;
|
||||
|
||||
use App\BusProNet\ApiClient;
|
||||
use App\BusProNet\Exception\ApiClientException;
|
||||
use App\BusProNet\Model\Notification;
|
||||
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;
|
||||
|
||||
#[Route('/api')]
|
||||
class PersonalDataController extends AbstractController
|
||||
{
|
||||
public function __construct(private readonly APiClient $apiClient)
|
||||
{
|
||||
}
|
||||
|
||||
#[Route('/personal-data', name: 'api_personal_data', methods: ['GET'])]
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$email = $request->headers->get('x-bpn-email');
|
||||
$password = $request->headers->get('x-bpn-password');
|
||||
|
||||
if (null === $email || null === $password) {
|
||||
return new JsonResponse(['message' => 'Invalid credentials'], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
try {
|
||||
$data = $this->apiClient->getPersonalData($email, $password);
|
||||
|
||||
if ($data instanceof Notification) {
|
||||
return new JsonResponse(['message' => $data->message, 'code' => $data->code], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
return $this->json($data);
|
||||
} catch (ApiClientException $e) {
|
||||
return new JsonResponse(['message' => $e->getMessage()], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller\Api;
|
||||
|
||||
use App\BusProNet\XmlLoader\PickupXmlLoader;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
#[Route('/api')]
|
||||
class PickupController extends AbstractController
|
||||
{
|
||||
public function __construct(private readonly PickupXmlLoader $xmlLoader)
|
||||
{
|
||||
}
|
||||
|
||||
#[Route('/pickups', name: 'api_pickup_all')]
|
||||
public function index(): JsonResponse
|
||||
{
|
||||
$pickups = $this->xmlLoader->loadAll();
|
||||
|
||||
return $this->json($pickups, Response::HTTP_OK, [], ['groups' => 'api:list']);
|
||||
}
|
||||
|
||||
#[Route('/pickups/{id}', name: 'api_pickup_single')]
|
||||
public function single(int $id): JsonResponse
|
||||
{
|
||||
$pickup = $this->xmlLoader->loadById($id);
|
||||
|
||||
return $this->json($pickup, Response::HTTP_OK, [], ['groups' => 'api:list']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller\Api;
|
||||
|
||||
use App\BusProNet\ApiClient;
|
||||
use App\BusProNet\Exception\ApiClientException;
|
||||
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\Validator\Constraints\Email;
|
||||
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||
|
||||
#[Route('/api')]
|
||||
class ResetPasswordController extends AbstractController
|
||||
{
|
||||
public function __construct(private readonly ApiClient $apiClient, private readonly ValidatorInterface $validator)
|
||||
{
|
||||
}
|
||||
|
||||
#[Route('/reset-password', name: 'api_reset_password', methods: ['POST'])]
|
||||
public function index(Request $request): Response
|
||||
{
|
||||
$payload = json_decode($request->getContent(), true);
|
||||
$email = $payload['email'] ?? null;
|
||||
|
||||
if (null === $email) {
|
||||
return new JsonResponse(['message' => 'Invalid email'], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
$violationList = $this->validator->validate($email, [new Email([
|
||||
'mode' => 'strict',
|
||||
])]);
|
||||
|
||||
if (0 < count($violationList)) {
|
||||
return new JsonResponse(['message' => 'Invalid email'], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
try {
|
||||
$this->apiClient->resetPassword($email);
|
||||
} catch (ApiClientException $e) {
|
||||
}
|
||||
|
||||
return $this->json(['message' => 'Password reset invoked']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller\Api;
|
||||
|
||||
use App\BusProNet\XmlLoader\HotelXmlLoader;
|
||||
use App\BusProNet\XmlLoader\PickupXmlLoader;
|
||||
use App\BusProNet\XmlLoader\TravelXmlLoader;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Contracts\Cache\CacheInterface;
|
||||
use Symfony\Contracts\Cache\ItemInterface;
|
||||
|
||||
#[Route('/api')]
|
||||
class TravelController extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly TravelXmlLoader $travelXmlLoader,
|
||||
private readonly HotelXmlLoader $hotelXmlLoader,
|
||||
private readonly PickupXmlLoader $pickupXmlLoader,
|
||||
private readonly CacheInterface $cache,
|
||||
) {
|
||||
}
|
||||
|
||||
#[Route(path: '/travels', name: 'api_travel_mapping')]
|
||||
public function mapping(): JsonResponse
|
||||
{
|
||||
$mapping = $this->travelXmlLoader->generateMapping();
|
||||
|
||||
return $this->json($mapping);
|
||||
}
|
||||
|
||||
#[Route(
|
||||
path: '/travels/{travelId}/{hotelId}',
|
||||
name: 'api_travel_single',
|
||||
requirements: ['travelId' => '\d+', 'hotelId' => '\d+']
|
||||
)]
|
||||
public function single(int $travelId, ?int $hotelId = null): JsonResponse
|
||||
{
|
||||
$cacheKey = sprintf('bpn_travel_%d_%d', $travelId, $hotelId ?? 0);
|
||||
$travel = $this->cache->get($cacheKey, function (ItemInterface $item) use ($travelId, $hotelId) {
|
||||
$item->expiresAfter(60);
|
||||
|
||||
$travel = $this->travelXmlLoader->loadById($travelId, $hotelId);
|
||||
|
||||
if (null === $travel) {
|
||||
return new JsonResponse(['message' => 'Not found'], Response::HTTP_NOT_FOUND);
|
||||
}
|
||||
|
||||
$this->pickupXmlLoader->patchPickupsDetails($travel);
|
||||
$this->hotelXmlLoader->patchHotelDetails($travel);
|
||||
|
||||
return $travel;
|
||||
});
|
||||
|
||||
return $this->json($travel, Response::HTTP_OK, [], ['groups' => ['api:list', 'api:single']]);
|
||||
}
|
||||
}
|
||||
@@ -3,8 +3,8 @@
|
||||
namespace App\Controller\Booking;
|
||||
|
||||
use App\BusProNet\ApiClient;
|
||||
use App\BusProNet\DataLoader\PickupDataLoader;
|
||||
use App\BusProNet\DataLoader\TravelDataLoader;
|
||||
use App\BusProNet\XmlLoader\PickupXmlLoader;
|
||||
use App\BusProNet\XmlLoader\TravelXmlLoader;
|
||||
use App\Form\BookingType;
|
||||
use App\Form\Model\BookingData;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
@@ -19,11 +19,11 @@ use Symfony\Contracts\Cache\ItemInterface;
|
||||
class EditController extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ApiClient $apiClient,
|
||||
private readonly TravelDataLoader $travelDataLoader,
|
||||
private readonly PickupDataLoader $pickupDataLoader,
|
||||
private readonly CacheInterface $cache,
|
||||
private readonly Security $security,
|
||||
private readonly ApiClient $apiClient,
|
||||
private readonly TravelXmlLoader $travelDataLoader,
|
||||
private readonly PickupXmlLoader $pickupDataLoader,
|
||||
private readonly CacheInterface $cache,
|
||||
private readonly Security $security,
|
||||
) {
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
namespace App\Controller\Booking;
|
||||
|
||||
use App\BusProNet\ApiClient;
|
||||
use App\BusProNet\DataLoader\TravelDataLoader;
|
||||
use App\BusProNet\XmlLoader\TravelXmlLoader;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
@@ -16,10 +16,10 @@ use Symfony\Contracts\Cache\ItemInterface;
|
||||
class IndexController extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ApiClient $apiClient,
|
||||
private readonly TravelDataLoader $travelDataLoader,
|
||||
private readonly CacheInterface $cache,
|
||||
private readonly Security $security,
|
||||
private readonly ApiClient $apiClient,
|
||||
private readonly TravelXmlLoader $travelDataLoader,
|
||||
private readonly CacheInterface $cache,
|
||||
private readonly Security $security,
|
||||
) {
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Security;
|
||||
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
||||
use Symfony\Component\Security\Core\Exception\AuthenticationException;
|
||||
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
|
||||
use Symfony\Component\Security\Core\User\UserInterface;
|
||||
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
|
||||
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
|
||||
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
|
||||
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
|
||||
|
||||
class ApiKeyAuthenticator extends AbstractAuthenticator
|
||||
{
|
||||
public function __construct(private readonly array $apiKeys)
|
||||
{
|
||||
}
|
||||
|
||||
public function supports(Request $request): ?bool
|
||||
{
|
||||
return $request->headers->has('X-BPN-API-KEY');
|
||||
}
|
||||
|
||||
public function authenticate(Request $request): Passport
|
||||
{
|
||||
$apiKey = $request->headers->get('X-BPN-API-KEY');
|
||||
|
||||
if (null === $apiKey) {
|
||||
throw new CustomUserMessageAuthenticationException('No API key provided');
|
||||
}
|
||||
|
||||
if (false === in_array($apiKey, $this->apiKeys)) {
|
||||
throw new CustomUserMessageAuthenticationException('Invalid API key');
|
||||
}
|
||||
|
||||
return new SelfValidatingPassport(
|
||||
new UserBadge($apiKey, function (string $userIdentifier) use ($apiKey): ?UserInterface {
|
||||
return new ApiUser($apiKey);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
|
||||
{
|
||||
$data = [
|
||||
'message' => 'Authentication missing or failed',
|
||||
];
|
||||
|
||||
return new JsonResponse($data, Response::HTTP_UNAUTHORIZED);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Security;
|
||||
|
||||
use Symfony\Component\Security\Core\User\UserInterface;
|
||||
|
||||
class ApiUser implements UserInterface
|
||||
{
|
||||
public function __construct(private readonly string $apiKey)
|
||||
{
|
||||
}
|
||||
|
||||
public function getRoles(): array
|
||||
{
|
||||
return ['ROLE_USER', 'ROLE_API'];
|
||||
}
|
||||
|
||||
public function eraseCredentials(): void
|
||||
{
|
||||
}
|
||||
|
||||
public function getUserIdentifier(): string
|
||||
{
|
||||
return $this->apiKey;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user