Skip to content

Commit 4661f97

Browse files
authored
Migration des coupons d'évènement de Ting vers Doctrine (#2386)
1 parent f84e559 commit 4661f97

7 files changed

Lines changed: 173 additions & 202 deletions

File tree

‎phpstan-baseline.php‎

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6097,54 +6097,6 @@
60976097
'count' => 1,
60986098
'path' => __DIR__ . '/sources/AppBundle/Event/Model/Repository/BadgeRepository.php',
60996099
];
6100-
$ignoreErrors[] = [
6101-
'message' => '#^Cannot cast mixed to string\\.$#',
6102-
'identifier' => 'cast.string',
6103-
'count' => 1,
6104-
'path' => __DIR__ . '/sources/AppBundle/Event/Model/Repository/EventCouponRepository.php',
6105-
];
6106-
$ignoreErrors[] = [
6107-
'message' => '#^Method AppBundle\\\\Event\\\\Model\\\\Repository\\\\EventCouponRepository\\:\\:changeCouponForEvent\\(\\) has parameter \\$coupons with no value type specified in iterable type array\\.$#',
6108-
'identifier' => 'missingType.iterableValue',
6109-
'count' => 1,
6110-
'path' => __DIR__ . '/sources/AppBundle/Event/Model/Repository/EventCouponRepository.php',
6111-
];
6112-
$ignoreErrors[] = [
6113-
'message' => '#^Method AppBundle\\\\Event\\\\Model\\\\Repository\\\\EventCouponRepository\\:\\:couponsListForEvent\\(\\) has no return type specified\\.$#',
6114-
'identifier' => 'missingType.return',
6115-
'count' => 1,
6116-
'path' => __DIR__ . '/sources/AppBundle/Event/Model/Repository/EventCouponRepository.php',
6117-
];
6118-
$ignoreErrors[] = [
6119-
'message' => '#^Method AppBundle\\\\Event\\\\Model\\\\Repository\\\\EventCouponRepository\\:\\:initMetadata\\(\\) has parameter \\$options with no value type specified in iterable type array\\.$#',
6120-
'identifier' => 'missingType.iterableValue',
6121-
'count' => 1,
6122-
'path' => __DIR__ . '/sources/AppBundle/Event/Model/Repository/EventCouponRepository.php',
6123-
];
6124-
$ignoreErrors[] = [
6125-
'message' => '#^Method AppBundle\\\\Event\\\\Model\\\\Repository\\\\EventCouponRepository\\:\\:initMetadata\\(\\) should return M of CCMBenchmark\\\\Ting\\\\Repository\\\\Metadata but returns CCMBenchmark\\\\Ting\\\\Repository\\\\Metadata\\<object\\>\\.$#',
6126-
'identifier' => 'return.type',
6127-
'count' => 1,
6128-
'path' => __DIR__ . '/sources/AppBundle/Event/Model/Repository/EventCouponRepository.php',
6129-
];
6130-
$ignoreErrors[] = [
6131-
'message' => '#^Parameter \\#1 \\$callback of function array_map expects \\(callable\\(mixed\\)\\: mixed\\)\\|null, Closure\\(AppBundle\\\\Event\\\\Model\\\\EventCoupon\\)\\: string given\\.$#',
6132-
'identifier' => 'argument.type',
6133-
'count' => 1,
6134-
'path' => __DIR__ . '/sources/AppBundle/Event/Model/Repository/EventCouponRepository.php',
6135-
];
6136-
$ignoreErrors[] = [
6137-
'message' => '#^Parameter \\#1 \\$databaseName of method CCMBenchmark\\\\Ting\\\\Repository\\\\Metadata\\<object\\>\\:\\:setDatabase\\(\\) expects string, mixed given\\.$#',
6138-
'identifier' => 'argument.type',
6139-
'count' => 1,
6140-
'path' => __DIR__ . '/sources/AppBundle/Event/Model/Repository/EventCouponRepository.php',
6141-
];
6142-
$ignoreErrors[] = [
6143-
'message' => '#^Parameter \\#1 \\$iterator of function iterator_to_array expects iterable, mixed given\\.$#',
6144-
'identifier' => 'argument.type',
6145-
'count' => 1,
6146-
'path' => __DIR__ . '/sources/AppBundle/Event/Model/Repository/EventCouponRepository.php',
6147-
];
61486100
$ignoreErrors[] = [
61496101
'message' => '#^Argument of an invalid type CCMBenchmark\\\\Ting\\\\Repository\\\\CollectionInterface\\<AppBundle\\\\Event\\\\Model\\\\Event\\>\\|null supplied for foreach, only iterables are supported\\.$#',
61506102
'identifier' => 'foreach.nonIterable',

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
namespace AppBundle\Controller\Admin\Event;
66

7+
use AppBundle\Event\Entity\Repository\EventCouponRepository;
78
use AppBundle\Event\Form\EventType;
89
use AppBundle\Event\Model\Event;
9-
use AppBundle\Event\Model\Repository\EventCouponRepository;
1010
use AppBundle\Event\Model\Repository\EventRepository;
1111
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1212
use Symfony\Component\Form\FormInterface;
@@ -35,7 +35,7 @@ public function __invoke(Request $request, $id): Response
3535

3636
$form = $this->createForm(EventType::class, $event);
3737

38-
if ($event->getId() && $couponsImploded = $this->couponRepository->couponsListForEventImploded($event)) {
38+
if ($event->getId() && $couponsImploded = $this->couponRepository->couponsListForEventImploded((int) $event->getId())) {
3939
$form->get('coupons')->setData($couponsImploded);
4040
}
4141

@@ -49,7 +49,7 @@ public function __invoke(Request $request, $id): Response
4949

5050
if ($form->get('coupons')->getData()) {
5151
$eventCoupons = explode(',', (string) $form->get('coupons')->getData());
52-
$this->couponRepository->changeCouponForEvent($event, $eventCoupons);
52+
$this->couponRepository->changeCouponForEvent((int) $event->getId(), $eventCoupons);
5353
}
5454

5555
$this->addFlash('notice', 'Évènement ' . ($id ? 'modifié' : 'ajouté'));
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AppBundle\Event\Entity;
6+
7+
use AppBundle\Event\Entity\Repository\EventCouponRepository;
8+
use Doctrine\ORM\Mapping as ORM;
9+
10+
#[ORM\Entity(repositoryClass: EventCouponRepository::class)]
11+
#[ORM\Table(name: 'afup_forum_coupon')]
12+
class EventCoupon
13+
{
14+
#[ORM\Id]
15+
#[ORM\GeneratedValue]
16+
#[ORM\Column]
17+
public int $id;
18+
19+
#[ORM\Column(name: 'id_forum', nullable: false)]
20+
public int $eventId;
21+
22+
#[ORM\Column(name: 'texte', length: 45, nullable: false)]
23+
public string $texte;
24+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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\EventCoupon;
9+
use Doctrine\Persistence\ManagerRegistry;
10+
11+
/**
12+
* @extends EntityRepository<EventCoupon>
13+
*/
14+
final class EventCouponRepository extends EntityRepository
15+
{
16+
public function __construct(ManagerRegistry $registry)
17+
{
18+
parent::__construct($registry, EventCoupon::class);
19+
}
20+
21+
/**
22+
* @param list<string> $coupons
23+
*/
24+
public function changeCouponForEvent(int $eventId, array $coupons): void
25+
{
26+
$this->getEntityManager()->wrapInTransaction(function () use ($eventId, $coupons): void {
27+
($qb = $this->createQueryBuilder('c'))
28+
->delete()
29+
->where($qb->expr()->eq('c.eventId', ':eventId'))
30+
->setParameter('eventId', $eventId)
31+
->getQuery()
32+
->execute();
33+
34+
foreach ($coupons as $coupon) {
35+
$coupon = trim($coupon);
36+
if ($coupon === '') {
37+
continue;
38+
}
39+
40+
$eventCoupon = new EventCoupon();
41+
$eventCoupon->eventId = $eventId;
42+
$eventCoupon->texte = $coupon;
43+
$this->getEntityManager()->persist($eventCoupon);
44+
}
45+
46+
$this->getEntityManager()->flush();
47+
});
48+
}
49+
50+
/**
51+
* @return list<EventCoupon>
52+
*/
53+
public function couponsListForEvent(int $eventId): array
54+
{
55+
return ($qb = $this->createQueryBuilder('c'))
56+
->where($qb->expr()->eq('c.eventId', ':eventId'))
57+
->setParameter('eventId', $eventId)
58+
->orderBy('c.id', 'ASC')
59+
->getQuery()
60+
->getResult();
61+
}
62+
63+
public function couponsListForEventImploded(int $eventId, string $separator = ', '): string
64+
{
65+
$texts = array_map(
66+
static fn(EventCoupon $coupon): string => $coupon->texte,
67+
$this->couponsListForEvent($eventId),
68+
);
69+
70+
return implode($separator, $texts);
71+
}
72+
}

‎sources/AppBundle/Event/Model/EventCoupon.php‎

Lines changed: 0 additions & 68 deletions
This file was deleted.

‎sources/AppBundle/Event/Model/Repository/EventCouponRepository.php‎

Lines changed: 0 additions & 83 deletions
This file was deleted.

0 commit comments

Comments
 (0)