chill-bundles/src/Bundle/ChillCalendarBundle/Controller/RemoteCalendarProxyController.php

74 lines
2.4 KiB
PHP

<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\CalendarBundle\Controller;
use Chill\CalendarBundle\Synchro\Connector\RemoteCalendarConnectorInterface;
use Chill\MainBundle\Entity\User;
use DateTimeImmutable;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\SerializerInterface;
/**
* Contains method to get events (Calendar) from remote calendar.
*/
class RemoteCalendarProxyController
{
private RemoteCalendarConnectorInterface $remoteCalendarConnector;
private SerializerInterface $serializer;
public function __construct(RemoteCalendarConnectorInterface $remoteCalendarConnector, SerializerInterface $serializer)
{
$this->remoteCalendarConnector = $remoteCalendarConnector;
$this->serializer = $serializer;
}
/**
* @Route("api/1.0/calendar/proxy/calendar/by-user/{id}/events")
*/
public function listEventForCalendar(User $user, Request $request): Response
{
if ($request->query->has('startDate')) {
$startDate = DateTimeImmutable::createFromFormat('Y-m-d', $request->query->get('startDate'));
if (false === $startDate) {
throw new BadRequestHttpException('startDate on bad format');
}
} else {
throw new BadRequestHttpException('startDate not provided');
}
if ($request->query->has('endDate')) {
$endDate = DateTimeImmutable::createFromFormat('Y-m-d', $request->query->get('endDate'));
if (false === $endDate) {
throw new BadRequestHttpException('endDate on bad format');
}
} else {
throw new BadRequestHttpException('endDate not provided');
}
$events = $this->remoteCalendarConnector->listEventsForUser($user, $startDate, $endDate);
return new JsonResponse(
$this->serializer->serialize($events, 'json', ['groups' => ['read']]),
JsonResponse::HTTP_OK,
[],
true
);
}
}