Skip to content

Commit 725cec0

Browse files
authored
refactor: migre l'entité Room de Ting vers Doctrine (#2383) (#2412)
1 parent b4b9c9e commit 725cec0

8 files changed

Lines changed: 140 additions & 53 deletions

File tree

‎phpstan-baseline.php‎

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2989,58 +2989,34 @@
29892989
'count' => 1,
29902990
'path' => __DIR__ . '/sources/AppBundle/Controller/Admin/Event/RemoveEventAction.php',
29912991
];
2992-
$ignoreErrors[] = [
2993-
'message' => '#^Binary operation "\\." between \'edit_room_\' and mixed results in an error\\.$#',
2994-
'identifier' => 'binaryOp.invalid',
2995-
'count' => 1,
2996-
'path' => __DIR__ . '/sources/AppBundle/Controller/Admin/Event/RoomAction.php',
2997-
];
2998-
$ignoreErrors[] = [
2999-
'message' => '#^Cannot call method getId\\(\\) on mixed\\.$#',
3000-
'identifier' => 'method.nonObject',
3001-
'count' => 1,
3002-
'path' => __DIR__ . '/sources/AppBundle/Controller/Admin/Event/RoomAction.php',
3003-
];
3004-
$ignoreErrors[] = [
3005-
'message' => '#^Cannot call method getName\\(\\) on mixed\\.$#',
3006-
'identifier' => 'method.nonObject',
3007-
'count' => 3,
3008-
'path' => __DIR__ . '/sources/AppBundle/Controller/Admin/Event/RoomAction.php',
3009-
];
3010-
$ignoreErrors[] = [
3011-
'message' => '#^Method AppBundle\\\\Controller\\\\Admin\\\\Event\\\\RoomAction\\:\\:getFormsForRooms\\(\\) has parameter \\$rooms with generic interface CCMBenchmark\\\\Ting\\\\Repository\\\\CollectionInterface but does not specify its types\\: T$#',
3012-
'identifier' => 'missingType.generics',
3013-
'count' => 1,
3014-
'path' => __DIR__ . '/sources/AppBundle/Controller/Admin/Event/RoomAction.php',
3015-
];
30162992
$ignoreErrors[] = [
30172993
'message' => '#^Method AppBundle\\\\Controller\\\\Admin\\\\Event\\\\RoomAction\\:\\:getFormsForRooms\\(\\) return type with generic interface Symfony\\\\Component\\\\Form\\\\FormInterface does not specify its types\\: TData$#',
30182994
'identifier' => 'missingType.generics',
30192995
'count' => 1,
30202996
'path' => __DIR__ . '/sources/AppBundle/Controller/Admin/Event/RoomAction.php',
30212997
];
30222998
$ignoreErrors[] = [
3023-
'message' => '#^Parameter \\#1 \\$entity of method CCMBenchmark\\\\Ting\\\\Repository\\\\Repository\\<AppBundle\\\\Event\\\\Model\\\\Room\\>\\:\\:delete\\(\\) expects AppBundle\\\\Event\\\\Model\\\\Room, mixed given\\.$#',
3024-
'identifier' => 'argument.type',
3025-
'count' => 1,
2999+
'message' => '#^Cannot access property \\$name on mixed\\.$#',
3000+
'identifier' => 'property.nonObject',
3001+
'count' => 2,
30263002
'path' => __DIR__ . '/sources/AppBundle/Controller/Admin/Event/RoomAction.php',
30273003
];
30283004
$ignoreErrors[] = [
3029-
'message' => '#^Parameter \\#1 \\$entity of method CCMBenchmark\\\\Ting\\\\Repository\\\\Repository\\<AppBundle\\\\Event\\\\Model\\\\Room\\>\\:\\:save\\(\\) expects AppBundle\\\\Event\\\\Model\\\\Room, mixed given\\.$#',
3005+
'message' => '#^Parameter \\#1 \\$entity of method AppBundle\\\\Doctrine\\\\EntityRepository\\<AppBundle\\\\Event\\\\Entity\\\\Room\\>\\:\\:delete\\(\\) expects AppBundle\\\\Event\\\\Entity\\\\Room, mixed given\\.$#',
30303006
'identifier' => 'argument.type',
3031-
'count' => 2,
3007+
'count' => 1,
30323008
'path' => __DIR__ . '/sources/AppBundle/Controller/Admin/Event/RoomAction.php',
30333009
];
30343010
$ignoreErrors[] = [
3035-
'message' => '#^Parameter \\#1 \\$eventId of method AppBundle\\\\Event\\\\Model\\\\Room\\:\\:setEventId\\(\\) expects int, int\\|null given\\.$#',
3011+
'message' => '#^Parameter \\#1 \\$entity of method AppBundle\\\\Doctrine\\\\EntityRepository\\<AppBundle\\\\Event\\\\Entity\\\\Room\\>\\:\\:save\\(\\) expects AppBundle\\\\Event\\\\Entity\\\\Room, mixed given\\.$#',
30363012
'identifier' => 'argument.type',
30373013
'count' => 1,
30383014
'path' => __DIR__ . '/sources/AppBundle/Controller/Admin/Event/RoomAction.php',
30393015
];
30403016
$ignoreErrors[] = [
30413017
'message' => '#^Parameter \\#2 \\.\\.\\.\\$values of function sprintf expects bool\\|float\\|int\\|string\\|null, mixed given\\.$#',
30423018
'identifier' => 'argument.type',
3043-
'count' => 3,
3019+
'count' => 2,
30443020
'path' => __DIR__ . '/sources/AppBundle/Controller/Admin/Event/RoomAction.php',
30453021
];
30463022
$ignoreErrors[] = [
@@ -3104,7 +3080,7 @@
31043080
'path' => __DIR__ . '/sources/AppBundle/Controller/Admin/Event/Session/EditAction.php',
31053081
];
31063082
$ignoreErrors[] = [
3107-
'message' => '#^Method AppBundle\\\\Controller\\\\Admin\\\\Event\\\\Session\\\\EditAction\\:\\:roomChoices\\(\\) should return array\\<string, int\\> but returns array\\<int\\|null\\>\\.$#',
3083+
'message' => '#^Method AppBundle\\\\Controller\\\\Admin\\\\Event\\\\Session\\\\EditAction\\:\\:roomChoices\\(\\) should return array\\<string, int\\> but returns array\\<string, int\\|null\\>\\.$#',
31083084
'identifier' => 'return.type',
31093085
'count' => 1,
31103086
'path' => __DIR__ . '/sources/AppBundle/Controller/Admin/Event/Session/EditAction.php',
@@ -3134,7 +3110,7 @@
31343110
'path' => __DIR__ . '/sources/AppBundle/Controller/Admin/Event/Session/EditAction.php',
31353111
];
31363112
$ignoreErrors[] = [
3137-
'message' => '#^Possibly invalid array key type mixed\\.$#',
3113+
'message' => '#^Possibly invalid array key type string\\|null\\.$#',
31383114
'identifier' => 'offsetAccess.invalidOffset',
31393115
'count' => 1,
31403116
'path' => __DIR__ . '/sources/AppBundle/Controller/Admin/Event/Session/EditAction.php',
@@ -3152,7 +3128,7 @@
31523128
'path' => __DIR__ . '/sources/AppBundle/Controller/Admin/Event/Session/IndexAction.php',
31533129
];
31543130
$ignoreErrors[] = [
3155-
'message' => '#^Parameter \\#2 \\$title of class AppBundle\\\\Event\\\\Model\\\\Session\\\\CalendarResource constructor expects string, mixed given\\.$#',
3131+
'message' => '#^Parameter \\#2 \\$title of class AppBundle\\\\Event\\\\Model\\\\Session\\\\CalendarResource constructor expects string, string\\|null given\\.$#',
31563132
'identifier' => 'argument.type',
31573133
'count' => 1,
31583134
'path' => __DIR__ . '/sources/AppBundle/Controller/Admin/Event/Session/IndexAction.php',

‎sources/AppBundle/Controller/Admin/Event/RoomAction.php‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66

77
use AppBundle\Event\AdminEventSelection;
88
use Symfony\Component\Form\FormView;
9+
use AppBundle\Event\Entity\Repository\RoomRepository;
10+
use AppBundle\Event\Entity\Room;
911
use AppBundle\Event\Form\RoomType;
10-
use AppBundle\Event\Model\Repository\RoomRepository;
11-
use AppBundle\Event\Model\Room;
12-
use CCMBenchmark\Ting\Repository\CollectionInterface;
1312
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1413
use Symfony\Component\Form\FormFactoryInterface;
1514
use Symfony\Component\Form\FormInterface;
@@ -29,16 +28,16 @@ public function __invoke(Request $request, AdminEventSelection $eventSelection):
2928
$rooms = $this->roomRepository->getByEvent($event);
3029
$editForms = $this->getFormsForRooms($rooms);
3130

32-
foreach ($editForms as $form) {
31+
foreach ($editForms as $i => $form) {
3332
$form->handleRequest($request);
3433
if ($form->isSubmitted() && $form->isValid()) {
3534
$room = $form->getData();
3635
if ($request->request->has('delete')) {
3736
$this->roomRepository->delete($room);
38-
$this->addFlash('notice', sprintf('La salle "%s" a été supprimée.', $room->getName()));
37+
$this->addFlash('notice', sprintf('La salle "%s" a été supprimée.', $room->name));
3938
} else {
4039
$this->roomRepository->save($room);
41-
$this->addFlash('notice', sprintf('La salle "%s" a été sauvegardée.', $room->getName()));
40+
$this->addFlash('notice', sprintf('La salle "%s" a été sauvegardée.', $room->name));
4241
}
4342

4443
return $this->redirectToRoute('admin_event_room', [
@@ -48,15 +47,14 @@ public function __invoke(Request $request, AdminEventSelection $eventSelection):
4847
}
4948

5049
$newRoom = new Room();
51-
$newRoom->setEventId($event->getId());
50+
$newRoom->eventId = $event->getId();
5251

5352
$addForm = $this->createForm(RoomType::class, $newRoom);
5453
$addForm->handleRequest($request);
5554

5655
if ($addForm->isSubmitted() && $addForm->isValid()) {
57-
$newRoom = $addForm->getData();
5856
$this->roomRepository->save($newRoom);
59-
$this->addFlash('notice', sprintf('La salle "%s" a été ajoutée.', $newRoom->getName()));
57+
$this->addFlash('notice', sprintf('La salle "%s" a été ajoutée.', $newRoom->name));
6058

6159
return $this->redirectToRoute('admin_event_room', [
6260
'id' => $event->getId(),
@@ -74,13 +72,15 @@ public function __invoke(Request $request, AdminEventSelection $eventSelection):
7472
}
7573

7674
/**
75+
* @param array<Room> $rooms
76+
*
7777
* @return FormInterface[]
7878
*/
79-
private function getFormsForRooms(CollectionInterface $rooms): array
79+
private function getFormsForRooms(array $rooms): array
8080
{
8181
$forms = [];
8282
foreach ($rooms as $room) {
83-
$forms[] = $this->formFactory->createNamedBuilder('edit_room_' . $room->getId(), RoomType::class,
83+
$forms[] = $this->formFactory->createNamedBuilder('edit_room_' . $room->id, RoomType::class,
8484
$room)->getForm();
8585
}
8686

‎sources/AppBundle/Controller/Admin/Event/Session/EditAction.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
namespace AppBundle\Controller\Admin\Event\Session;
66

77
use AppBundle\AuditLog\Audit;
8+
use AppBundle\Event\Entity\Repository\RoomRepository;
9+
use AppBundle\Event\Entity\Room;
810
use AppBundle\Event\Model\Event;
911
use AppBundle\Event\Model\Planning;
1012
use AppBundle\Event\Model\Repository\EventRepository;
1113
use AppBundle\Event\Model\Repository\PlanningRepository;
12-
use AppBundle\Event\Model\Repository\RoomRepository;
1314
use AppBundle\Event\Model\Repository\TalkRepository;
14-
use AppBundle\Event\Model\Room;
1515
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1616
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
1717
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
@@ -134,7 +134,7 @@ private function roomChoices(Event $event): array
134134
$rooms = $this->roomRepository->getByEvent($event);
135135
/** @var Room $room */
136136
foreach ($rooms as $room) {
137-
$roomChoices[$room->getName()] = $room->getId();
137+
$roomChoices[$room->name] = $room->id;
138138
}
139139

140140
return $roomChoices;

‎sources/AppBundle/Controller/Admin/Event/Session/IndexAction.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
namespace AppBundle\Controller\Admin\Event\Session;
66

77
use AppBundle\Event\AdminEventSelection;
8+
use AppBundle\Event\Entity\Repository\RoomRepository;
9+
use AppBundle\Event\Entity\Room;
810
use AppBundle\Event\Model\Event;
911
use AppBundle\Event\Model\Planning;
10-
use AppBundle\Event\Model\Repository\RoomRepository;
1112
use AppBundle\Event\Model\Repository\TalkRepository;
12-
use AppBundle\Event\Model\Room;
1313
use AppBundle\Event\Model\Session\CalendarEvent;
1414
use AppBundle\Event\Model\Session\CalendarResource;
1515
use AppBundle\Event\Model\TalkAggregate;
@@ -54,8 +54,8 @@ private function calendarResources(Event $event): array
5454
/** @var Room $room */
5555
foreach ($rooms as $i => $room) {
5656
$resources[] = new CalendarResource(
57-
$room->getId(),
58-
$room->getName(),
57+
$room->id,
58+
$room->name,
5959
$colors[$i % $m],
6060
);
6161
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AppBundle\Event\Entity\Repository;
6+
7+
use AppBundle\Doctrine\EntityRepository;
8+
use AppBundle\Event\Entity\Room;
9+
use AppBundle\Event\Model\Event;
10+
use Doctrine\Persistence\ManagerRegistry;
11+
12+
/**
13+
* @extends EntityRepository<Room>
14+
*/
15+
final class RoomRepository extends EntityRepository
16+
{
17+
public function __construct(ManagerRegistry $registry)
18+
{
19+
parent::__construct($registry, Room::class);
20+
}
21+
22+
/**
23+
* @return list<Room>
24+
*/
25+
public function getByEvent(Event $event): array
26+
{
27+
return array_values($this->findBy(['eventId' => $event->getId()]));
28+
}
29+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AppBundle\Event\Entity;
6+
7+
use AppBundle\Event\Entity\Repository\RoomRepository;
8+
use Doctrine\ORM\Mapping as ORM;
9+
use Symfony\Component\Validator\Constraints as Assert;
10+
11+
#[ORM\Entity(repositoryClass: RoomRepository::class)]
12+
#[ORM\Table(name: 'afup_forum_salle')]
13+
class Room
14+
{
15+
#[ORM\Id]
16+
#[ORM\GeneratedValue]
17+
#[ORM\Column(type: 'smallint')]
18+
public ?int $id = null;
19+
20+
#[ORM\Column(name: 'nom', length: 255, nullable: true)]
21+
public ?string $name = null;
22+
23+
#[Assert\NotBlank]
24+
#[Assert\GreaterThan(0)]
25+
#[ORM\Column(name: 'id_forum', nullable: true)]
26+
public ?int $eventId = null;
27+
}

‎sources/AppBundle/Event/Form/RoomType.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace AppBundle\Event\Form;
66

7-
use AppBundle\Event\Model\Room;
7+
use AppBundle\Event\Entity\Room;
88
use Symfony\Component\Form\AbstractType;
99
use Symfony\Component\Form\Extension\Core\Type\TextType;
1010
use Symfony\Component\Form\FormBuilderInterface;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AppBundle\IntegrationTests\Event\Entity\Repository;
6+
7+
use Afup\Tests\Support\IntegrationTestCase;
8+
use AppBundle\Event\Entity\Repository\RoomRepository;
9+
use AppBundle\Event\Entity\Room;
10+
use AppBundle\Event\Model\Event;
11+
12+
final class RoomRepositoryTest extends IntegrationTestCase
13+
{
14+
public function testSaveGetByEventAndDelete(): void
15+
{
16+
$roomRepository = self::getContainer()->get(RoomRepository::class);
17+
$event = new Event();
18+
$event->setId(4242);
19+
20+
// Deux salles : une pour l'événement testé, l'autre pour un événement voisin
21+
$room = new Room();
22+
$room->name = 'La salle de test';
23+
$room->eventId = 4242;
24+
$roomRepository->save($room);
25+
26+
$autre = new Room();
27+
$autre->name = 'La salle ailleurs';
28+
$autre->eventId = 99;
29+
$roomRepository->save($autre);
30+
31+
// L'auto-incrément remplit l'identifiant à la sauvegarde
32+
self::assertNotNull($room->id);
33+
self::assertGreaterThan(0, $room->id);
34+
35+
$rooms = $roomRepository->getByEvent($event);
36+
self::assertCount(1, $rooms);
37+
self::assertSame('La salle de test', $rooms[0]->name);
38+
self::assertSame($room->id, $rooms[0]->id);
39+
40+
// Mise à jour puis vidage de l'EntityManager pour vérifier la persistance en base
41+
$room->name = 'La salle renommée';
42+
$roomRepository->save($room);
43+
$roomRepository->getEntityManager()->clear();
44+
45+
$reloaded = $roomRepository->find($room->id);
46+
self::assertInstanceOf(Room::class, $reloaded);
47+
self::assertSame('La salle renommée', $reloaded->name);
48+
self::assertSame(4242, $reloaded->eventId);
49+
50+
$roomRepository->delete($reloaded);
51+
52+
self::assertNull($roomRepository->find($room->id));
53+
self::assertCount(0, $roomRepository->getByEvent($event));
54+
}
55+
}

0 commit comments

Comments
 (0)