Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/Controller/SurfSession/NewSurfSessionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
use App\Entity\SurfSession;
use App\Entity\User;
use App\Enum\User\UserRole;
use App\Form\Model\SurfSession\SurfSessionWriteModel;
use App\Form\SurfSession\SurfSessionFormType;
use App\Repository\SurfSessionRepository;
use App\Security\Voter\TripVoter;
use App\Service\SurfSession\SurfSessionWriteModelFactory;
use App\Service\Trip\TripReadModelProvider;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\ObjectMapper\ObjectMapperInterface;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Routing\Requirement\Requirement;
use Symfony\Component\Security\Http\Attribute\CurrentUser;
use Symfony\Component\Security\Http\Attribute\IsGranted;

Expand All @@ -25,17 +28,33 @@ public function __construct(
private readonly ObjectMapperInterface $objectMapper,
private readonly SurfSessionRepository $surfSessionRepository,
private readonly SurfSessionCacheInvalidator $surfSessionCacheInvalidator,
private readonly TripReadModelProvider $tripReadModelProvider,
private readonly SurfSessionWriteModelFactory $surfSessionWriteModelFactory,
) {}

#[Route(
path: '/sessions/new',
name: 'app.surf_session.new',
methods: [Request::METHOD_GET, Request::METHOD_POST],
)]
#[Route(
path: '/trip/{tripId}/sessions/new',
name: 'app.trip.surf_session.new',
requirements: ['tripId' => Requirement::POSITIVE_INT],
methods: [Request::METHOD_GET, Request::METHOD_POST],
)]
#[IsGranted(UserRole::USER)]
public function __invoke(Request $request, #[CurrentUser()] User $currentUser): Response
public function __invoke(Request $request, #[CurrentUser()] User $currentUser, ?int $tripId = null): Response
{
$surfSessionWriteModel = new SurfSessionWriteModel();
$trip = null;

if (null !== $tripId) {
$trip = $this->tripReadModelProvider->getById($tripId);

$this->denyAccessUnlessGranted(TripVoter::EDIT, $trip);
}

$surfSessionWriteModel = $this->surfSessionWriteModelFactory->create($trip);

$form = $this->createForm(SurfSessionFormType::class, $surfSessionWriteModel);
$form->handleRequest($request);
Expand Down
5 changes: 0 additions & 5 deletions src/Controller/Trip/EditTripController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace App\Controller\Trip;

use App\Exception\TripNotFoundHttpException;
use App\Form\Model\Trip\TripWriteModel;
use App\Form\Trip\TripFormType;
use App\Security\Voter\TripVoter;
Expand Down Expand Up @@ -40,10 +39,6 @@ public function __invoke(Request $request, int $id, string $slug): Response
{
$trip = $this->tripReadModelProvider->getById($id);

if (null === $trip) {
throw new TripNotFoundHttpException($id);
}

$this->denyAccessUnlessGranted(TripVoter::EDIT, $trip);

if ($trip->slug->value !== $slug) {
Expand Down
5 changes: 0 additions & 5 deletions src/Controller/Trip/ShowTripController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace App\Controller\Trip;

use App\Exception\TripNotFoundHttpException;
use App\Service\Trip\TripReadModelProvider;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -33,10 +32,6 @@ public function __invoke(int $id, string $slug): Response
{
$trip = $this->tripReadModelProvider->getById($id);

if (null === $trip) {
throw new TripNotFoundHttpException($id);
}

if ($trip->slug->value !== $slug) {
return $this->redirectToRoute(self::ROUTE, [
'id' => $trip->id,
Expand Down
50 changes: 50 additions & 0 deletions src/Service/SurfSession/SurfSessionWriteModelFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace App\Service\SurfSession;

use App\Form\Model\SurfSession\SurfSessionWriteModel;
use App\ReadModel\Trip\AbstractTripReadModel;
use App\ReadModel\Trip\TripSelectReadModel;

final class SurfSessionWriteModelFactory
{
private const int DEFAULT_SESSION_DURATION_HOURS = 2;

public function create(?AbstractTripReadModel $trip = null): SurfSessionWriteModel
{
$surfSessionWriteModel = new SurfSessionWriteModel();
$startAt = $this->resolveStartAt($trip);
$tripSelect = null;

if (null !== $trip) {
$tripSelect = new TripSelectReadModel(
id: $trip->id,
title: $trip->title->value,
location: $trip->location->value,
);
}

$surfSessionWriteModel->trip = $tripSelect;
$surfSessionWriteModel->spot = $tripSelect?->location;
$surfSessionWriteModel->startAt = $startAt;
$surfSessionWriteModel->endAt = $startAt->modify(sprintf('+%d hours', self::DEFAULT_SESSION_DURATION_HOURS));
Comment thread
RomMad marked this conversation as resolved.

return $surfSessionWriteModel;
}

private function resolveStartAt(?AbstractTripReadModel $trip = null): \DateTimeImmutable
{
$now = new \DateTimeImmutable();

$startAt = match (true) {
null === $trip => $now,
$now < $trip->startAt => $trip->startAt,
$now > $trip->endAt => $trip->endAt,
default => $now,
};

return $startAt->setTime(10, 0);
}
}
13 changes: 11 additions & 2 deletions src/Service/Trip/TripReadModelProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Service\Trip;

use App\Cache\Trip\TripCacheKeys;
use App\Exception\TripNotFoundHttpException;
use App\ReadModel\Trip\TripShowReadModel;
use App\Repository\TripRepository;
use Symfony\Contracts\Cache\ItemInterface;
Expand All @@ -19,15 +20,23 @@ public function __construct(
private TagAwareCacheInterface $cache,
) {}

public function getById(int $id): ?TripShowReadModel
public function getById(int $id): TripShowReadModel
{
return $this->cache->get(
$trip = $this->cache->get(
TripCacheKeys::readModel($id),
function (ItemInterface $item) use ($id): ?TripShowReadModel {
$item->expiresAfter(new \DateInterval(self::CACHE_TTL));

return $this->tripRepository->findShowReadModelById($id);
},
);

if (null === $trip) {
$this->cache->delete(TripCacheKeys::readModel($id));

throw new TripNotFoundHttpException($id);
}

return $trip;
}
}
11 changes: 10 additions & 1 deletion templates/trip/edit.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@
{% set title = 'trip.edit.label'|trans %}

{% block body %}
<twig:Heading1 :title="title" />
<div class="app-page-header">
<twig:Heading1 :title="title" class="mb-0" />
{% if is_granted('EDIT', trip) %}
<twig:Button:New
href="{{ path('app.trip.surf_session.new', {tripId: trip.id}) }}"
label="surf_session.add.label"
/>
{% endif %}
</div>

<twig:Trip:Form :form="form" button_label="update.label" />
<twig:Trip:Form:Delete :trip="trip" />
<twig:Trip:BackToIndexButton />
Expand Down
16 changes: 13 additions & 3 deletions templates/trip/show.html.twig
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
{% extends 'base.html.twig' %}

{% set title = 'trip.label'|trans %}
{% set is_granted_edit = is_granted('EDIT', trip) %}

{% block body %}
<twig:Heading1 :title="title" />
<div class="app-page-header">
<twig:Heading1 :title="title" class="mb-0" />
{% if is_granted_edit %}
<twig:Button:New
href="{{ path('app.trip.surf_session.new', {tripId: trip.id}) }}"
label="surf_session.add.label"
/>
{% endif %}
</div>

<twig:Trip:ShowTable :trip="trip" />
{% if is_granted('EDIT', trip) %}
{% if is_granted_edit %}
<twig:Calendar:LinksGroup :event="trip" />
{% endif %}

<div class="flex flex-wrap items-center gap-2">
{% if is_granted('EDIT', trip) %}
{% if is_granted_edit %}
<twig:Button:Edit href="{{ path('app.trip.edit', {id: trip.id, slug: trip.slug}) }}" class="mb-4" />
<twig:Trip:Form:Delete :trip="trip" />
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ final class NewSurfSessionControllerTest extends CustomWebTestCase
// Paths
private const string PATH_INDEX = '/en/sessions';
private const string PATH_NEW = '/en/sessions/new';
private const string PATH_NEW_FROM_TRIP = '/en/trip/%d/sessions/new';
// Selectors
private const string FORM = 'form[name="surf_session"]';
private const string FIRST_CARD = '.app-card';
Expand Down Expand Up @@ -89,4 +90,33 @@ public function testCreateSurfSessionIsSuccessful(): void
$this->assertSelectorTextContains(self::FIRST_CARD, self::SESSION_SPOT);
$this->assertSelectorTextContains(self::FIRST_CARD, self::SESSION_BOARD);
}

public function testNewSurfSessionWithTrip(): void
{
$trip = TripFactory::find(['title' => Title::from(TripStory::CURRENT_TRIP_TITLE)]);
$now = new \DateTimeImmutable()->setTime(10, 0);

$this->client->request(Request::METHOD_GET, sprintf(self::PATH_NEW_FROM_TRIP, $trip->id));

$startAt = $this->parseDateTimeFromInput('#surf_session_startAt');
$endAt = $this->parseDateTimeFromInput('#surf_session_endAt');

$this->assertResponseIsSuccessful();
$this->assertSame($trip->location->value, $this->getInputValue('#surf_session_spot'));
$this->assertSame((string) $trip->id, $this->getInputValue('#surf_session_trip option[selected]'));
$this->assertSame($now->format('Y-m-d\TH'), $startAt->format('Y-m-d\TH'));
$this->assertSame($startAt->modify('+2 hours')->format('Y-m-d\TH'), $endAt->format('Y-m-d\TH'));
}
Comment thread
RomMad marked this conversation as resolved.

private function parseDateTimeFromInput(string $selector): \DateTimeImmutable
{
$value = $this->getInputValue($selector);

return \DateTimeImmutable::createFromFormat('Y-m-d\TH:i', $value);
}

private function getInputValue(string $selector): string
{
return (string) $this->client->getCrawler()->filter($selector)->attr('value');
}
}
2 changes: 2 additions & 0 deletions tests/Functional/Controller/Trip/ShowTripControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ final class ShowTripControllerTest extends CustomWebTestCase

private const string PATH = '/en/trip/%d/%s';
private const string TITLE = 'Trip';
private const string ADD_SESSION_LABEL = 'Add session';

private ?Trip $trip = null;

Expand All @@ -45,6 +46,7 @@ public function testShowTripPageIsDisplayed(): void
$this->assertSelectorTextSame(self::TITLE_H1, self::TITLE);
$this->assertSelectorExists(self::TABLE);
$this->assertSelectorTextContains(self::TABLE, $this->trip->title->value);
$this->assertCount(1, $this->client->getCrawler()->selectLink(self::ADD_SESSION_LABEL));
}

public function testShowTripPageRedirectsWhenSlugIsInvalid(): void
Expand Down
1 change: 1 addition & 0 deletions translations/messages+intl-icu.en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ surf_session:
created_successfully: The session has been created.
updated_successfully: The session has been updated.
deleted_successfully: The session has been deleted.
add.label: Add session
new.label: New session
edit.label: Edit session
delete.confirm: Are you sure you want to delete this session?
Expand Down
1 change: 1 addition & 0 deletions translations/messages+intl-icu.fr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ surf_session:
created_successfully: La session a été créée.
updated_successfully: La session a été mise à jour.
deleted_successfully: La session a été supprimée.
add.label: Ajouter une session
new.label: Nouvelle session
edit.label: Édition de la session
delete.confirm: Es-tu sûr de vouloir supprimer cette session ?
Expand Down
Loading