2024-07-18 13:40:49 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
|
2024-08-01 14:37:23 +02:00
|
|
|
use App\Entity\DomainEntity;
|
|
|
|
|
use App\Entity\DomainEvent;
|
2024-07-18 13:40:49 +02:00
|
|
|
use App\Entity\User;
|
|
|
|
|
use App\Entity\WatchList;
|
2024-08-01 14:37:23 +02:00
|
|
|
use App\Repository\WatchListRepository;
|
2024-07-18 13:40:49 +02:00
|
|
|
use Doctrine\Common\Collections\Collection;
|
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2024-08-01 14:37:23 +02:00
|
|
|
use Eluceo\iCal\Domain\Entity\Attendee;
|
|
|
|
|
use Eluceo\iCal\Domain\Entity\Calendar;
|
|
|
|
|
use Eluceo\iCal\Domain\Entity\Event;
|
|
|
|
|
use Eluceo\iCal\Domain\ValueObject\Category;
|
|
|
|
|
use Eluceo\iCal\Domain\ValueObject\Date;
|
|
|
|
|
use Eluceo\iCal\Domain\ValueObject\EmailAddress;
|
|
|
|
|
use Eluceo\iCal\Domain\ValueObject\SingleDay;
|
|
|
|
|
use Eluceo\iCal\Presentation\Factory\CalendarFactory;
|
|
|
|
|
use Sabre\VObject\EofException;
|
|
|
|
|
use Sabre\VObject\InvalidDataException;
|
|
|
|
|
use Sabre\VObject\ParseException;
|
|
|
|
|
use Sabre\VObject\Reader;
|
2024-07-18 13:40:49 +02:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2024-08-01 14:37:23 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2024-07-18 13:40:49 +02:00
|
|
|
use Symfony\Component\Routing\Attribute\Route;
|
|
|
|
|
use Symfony\Component\Serializer\SerializerInterface;
|
|
|
|
|
|
|
|
|
|
class WatchListController extends AbstractController
|
|
|
|
|
{
|
|
|
|
|
public function __construct(
|
2024-08-02 23:24:52 +02:00
|
|
|
private readonly SerializerInterface $serializer,
|
2024-08-01 14:37:23 +02:00
|
|
|
private readonly EntityManagerInterface $em,
|
2024-08-02 23:24:52 +02:00
|
|
|
private readonly WatchListRepository $watchListRepository
|
|
|
|
|
) {
|
2024-07-18 13:40:49 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-29 16:01:32 +02:00
|
|
|
/**
|
2024-08-02 23:24:52 +02:00
|
|
|
* @throws \Exception
|
2024-07-29 16:01:32 +02:00
|
|
|
*/
|
2024-07-18 13:40:49 +02:00
|
|
|
#[Route(
|
|
|
|
|
path: '/api/watchlists',
|
|
|
|
|
name: 'watchlist_create',
|
|
|
|
|
defaults: [
|
|
|
|
|
'_api_resource_class' => WatchList::class,
|
|
|
|
|
'_api_operation_name' => 'create',
|
|
|
|
|
],
|
|
|
|
|
methods: ['POST']
|
|
|
|
|
)]
|
|
|
|
|
public function createWatchList(Request $request): WatchList
|
|
|
|
|
{
|
|
|
|
|
$watchList = $this->serializer->deserialize($request->getContent(), WatchList::class, 'json', ['groups' => 'watchlist:create']);
|
2024-08-03 00:06:38 +02:00
|
|
|
/** @var User $user */
|
|
|
|
|
$user = $this->getUser();
|
|
|
|
|
$watchList->setUser($user);
|
2024-07-29 16:01:32 +02:00
|
|
|
|
2024-07-18 13:40:49 +02:00
|
|
|
$this->em->persist($watchList);
|
|
|
|
|
$this->em->flush();
|
|
|
|
|
|
|
|
|
|
return $watchList;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-01 14:37:23 +02:00
|
|
|
/**
|
|
|
|
|
* @throws ParseException
|
|
|
|
|
* @throws EofException
|
|
|
|
|
* @throws InvalidDataException
|
2024-08-02 23:24:52 +02:00
|
|
|
* @throws \Exception
|
2024-08-01 14:37:23 +02:00
|
|
|
*/
|
|
|
|
|
#[Route(
|
|
|
|
|
path: '/api/watchlists/{token}/calendar',
|
|
|
|
|
name: 'watchlist_calendar',
|
|
|
|
|
defaults: [
|
|
|
|
|
'_api_resource_class' => WatchList::class,
|
|
|
|
|
'_api_operation_name' => 'calendar',
|
|
|
|
|
]
|
|
|
|
|
)]
|
|
|
|
|
public function getWatchlistCalendar(string $token): Response
|
|
|
|
|
{
|
2024-08-02 01:06:49 +02:00
|
|
|
/** @var WatchList $watchList */
|
2024-08-02 23:24:52 +02:00
|
|
|
$watchList = $this->watchListRepository->findOneBy(['token' => $token]);
|
2024-08-01 14:37:23 +02:00
|
|
|
|
|
|
|
|
$calendar = new Calendar();
|
|
|
|
|
|
|
|
|
|
foreach ($watchList->getDomains()->getIterator() as $domain) {
|
|
|
|
|
$attendees = [];
|
|
|
|
|
|
|
|
|
|
/** @var DomainEntity $entity */
|
|
|
|
|
foreach ($domain->getDomainEntities()->toArray() as $entity) {
|
|
|
|
|
$vCard = Reader::readJson($entity->getEntity()->getJCard());
|
2024-08-03 00:06:38 +02:00
|
|
|
if (isset($vCard->EMAIL) && isset($vCard->FN)) {
|
|
|
|
|
$email = (string) $vCard->EMAIL;
|
|
|
|
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$attendees[] = (new Attendee(new EmailAddress($email)))->setDisplayName((string) $vCard->FN);
|
2024-08-02 23:24:52 +02:00
|
|
|
}
|
2024-08-01 14:37:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @var DomainEvent $event */
|
|
|
|
|
foreach ($domain->getEvents()->toArray() as $event) {
|
|
|
|
|
$calendar->addEvent((new Event())
|
2024-08-02 23:24:52 +02:00
|
|
|
->setSummary($domain->getLdhName().' ('.$event->getAction().')')
|
2024-08-01 14:37:23 +02:00
|
|
|
->addCategory(new Category($event->getAction()))
|
|
|
|
|
->setAttendees($attendees)
|
|
|
|
|
->setOccurrence(new SingleDay(new Date($event->getDate())))
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new Response((new CalendarFactory())->createCalendar($calendar), Response::HTTP_OK, [
|
2024-08-02 23:24:52 +02:00
|
|
|
'Content-Type' => 'text/calendar; charset=utf-8',
|
2024-08-01 14:37:23 +02:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[Route(
|
|
|
|
|
path: '/api/watchlists',
|
|
|
|
|
name: 'watchlist_get_all_mine',
|
|
|
|
|
defaults: [
|
|
|
|
|
'_api_resource_class' => WatchList::class,
|
|
|
|
|
'_api_operation_name' => 'get_all_mine',
|
|
|
|
|
],
|
|
|
|
|
methods: ['GET']
|
|
|
|
|
)]
|
|
|
|
|
public function getWatchLists(): Collection
|
|
|
|
|
{
|
|
|
|
|
/** @var User $user */
|
|
|
|
|
$user = $this->getUser();
|
2024-08-02 23:24:52 +02:00
|
|
|
|
2024-08-01 14:37:23 +02:00
|
|
|
return $user->getWatchLists();
|
|
|
|
|
}
|
2024-08-02 23:24:52 +02:00
|
|
|
}
|