Skip to content

Commit a533b75

Browse files
authored
Refonte Doctrine des évènements de la trésorerie (#2118)
Co-authored-by: vgreb <vgreb@users.noreply.github.com>
1 parent 03d871f commit a533b75

File tree

14 files changed

+124
-136
lines changed

14 files changed

+124
-136
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AppBundle\Accounting\Entity;
6+
7+
use Doctrine\ORM\Mapping as ORM;
8+
9+
#[ORM\Entity]
10+
#[ORM\Table(name: 'compta_evenement')]
11+
class Event
12+
{
13+
#[ORM\Id]
14+
#[ORM\GeneratedValue]
15+
#[ORM\Column]
16+
public ?int $id = null;
17+
18+
#[ORM\Column(name: 'evenement', length: 50, nullable: false)]
19+
public string $name;
20+
21+
#[ORM\Column(type: 'datetime_immutable', nullable: true)]
22+
public ?\DateTimeImmutable $hideInAccountingJournalAt = null;
23+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AppBundle\Accounting\Entity\Repository;
6+
7+
use AppBundle\Accounting\Entity\Event;
8+
use AppBundle\Doctrine\EntityRepository;
9+
use Doctrine\Persistence\ManagerRegistry;
10+
11+
/**
12+
* @extends EntityRepository<Event>
13+
*/
14+
final class EventRepository extends EntityRepository
15+
{
16+
public function __construct(ManagerRegistry $registry)
17+
{
18+
parent::__construct($registry, Event::class);
19+
}
20+
21+
/**
22+
* @return array<Event>
23+
*/
24+
public function getAllSortedByName(): array
25+
{
26+
return $this->createQueryBuilder('e')
27+
->orderBy('e.name', 'asc')
28+
->getQuery()
29+
->execute();
30+
}
31+
}

sources/AppBundle/Accounting/Entity/Rule.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ class Rule
3131
#[ORM\JoinColumn(nullable: true)]
3232
public ?Category $category = null;
3333

34-
#[ORM\Column(nullable: true)]
35-
public ?int $eventId = null;
34+
#[ORM\OneToOne()]
35+
#[ORM\JoinColumn(nullable: true)]
36+
public ?Event $event = null;
3637

3738
#[ORM\Column(name: 'mode_regl_id', nullable: true)]
3839
public ?int $paymentTypeId = null;

sources/AppBundle/Accounting/Form/RuleType.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
namespace AppBundle\Accounting\Form;
66

77
use AppBundle\Accounting\Entity\Category;
8-
use AppBundle\Accounting\Model\Repository\EventRepository;
8+
use AppBundle\Accounting\Entity\Event;
9+
use AppBundle\Accounting\Entity\Repository\CategoryRepository;
10+
use AppBundle\Accounting\Entity\Repository\EventRepository;
911
use AppBundle\Model\ComptaModeReglement;
1012
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
1113
use Symfony\Component\Form\AbstractType;
@@ -17,17 +19,12 @@
1719
class RuleType extends AbstractType
1820
{
1921
public function __construct(
22+
private readonly CategoryRepository $categoryRepository,
2023
private readonly EventRepository $eventRepository,
2124
) {}
2225

2326
public function buildForm(FormBuilderInterface $builder, array $options): void
2427
{
25-
$events = [];
26-
$events[''] = null;
27-
foreach ($this->eventRepository->getAllSortedByName() as $event) {
28-
$events[$event->getName()] = $event->getId();
29-
}
30-
3128
$builder->add('label', TextType::class, [
3229
'label' => 'Nom de la régle',
3330
'required' => true,
@@ -65,11 +62,14 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
6562
])->add('category', EntityType::class, [
6663
'label' => 'Catégorie',
6764
'class' => Category::class,
65+
'choices' => $this->categoryRepository->getAllSortedByName(),
6866
'choice_label' => 'name',
6967
'required' => false,
70-
])->add('eventId', ChoiceType::class, [
68+
])->add('event', EntityType::class, [
7169
'label' => 'Évènement',
72-
'choices' => $events,
70+
'class' => Event::class,
71+
'choices' => $this->eventRepository->getAllSortedByName(),
72+
'choice_label' => 'name',
7373
'required' => false,
7474
])->add('attachmentRequired', ChoiceType::class, [
7575
'label' => ' Justificatif obligatoire ? ',

sources/AppBundle/Accounting/Model/Event.php

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

sources/AppBundle/Accounting/Model/Repository/EventRepository.php

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

sources/AppBundle/Controller/Admin/Accounting/Configuration/AddEventAction.php

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

77
use AppBundle\Accounting\Form\EventType;
8-
use AppBundle\Accounting\Model\Event;
9-
use AppBundle\Accounting\Model\Repository\EventRepository;
8+
use AppBundle\Accounting\Entity\Event;
9+
use AppBundle\Accounting\Entity\Repository\EventRepository;
1010
use AppBundle\AuditLog\Audit;
1111
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1212
use Symfony\Component\HttpFoundation\Request;
@@ -26,7 +26,7 @@ public function __invoke(Request $request): Response
2626
$form->handleRequest($request);
2727
if ($form->isSubmitted() && $form->isValid()) {
2828
$this->eventRepository->save($event);
29-
$this->audit->log('Ajout de l\'évènement ' . $event->getName());
29+
$this->audit->log('Ajout de l\'évènement ' . $event->name);
3030
$this->addFlash('notice', 'L\'évènement a été ajouté');
3131
return $this->redirectToRoute('admin_accounting_events_list');
3232
}

sources/AppBundle/Controller/Admin/Accounting/Configuration/EditEventAction.php

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

77
use AppBundle\Accounting\Form\EventType;
8-
use AppBundle\Accounting\Model\Repository\EventRepository;
8+
use AppBundle\Accounting\Entity\Repository\EventRepository;
99
use AppBundle\AuditLog\Audit;
1010
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1111
use Symfony\Component\HttpFoundation\Request;
@@ -20,12 +20,12 @@ public function __construct(
2020

2121
public function __invoke(int $id,Request $request): Response
2222
{
23-
$event = $this->eventRepository->get($id);
23+
$event = $this->eventRepository->find($id);
2424
$form = $this->createForm(EventType::class, $event);
2525
$form->handleRequest($request);
2626
if ($form->isSubmitted() && $form->isValid()) {
2727
$this->eventRepository->save($event);
28-
$this->audit->log('Modification de l\'évènement ' . $event->getName());
28+
$this->audit->log('Modification de l\'évènement ' . $event->name);
2929
$this->addFlash('notice', 'L\'évènement a été modifié');
3030
return $this->redirectToRoute('admin_accounting_events_list');
3131
}

sources/AppBundle/Controller/Admin/Accounting/Configuration/ListEventAction.php

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

55
namespace AppBundle\Controller\Admin\Accounting\Configuration;
66

7-
use AppBundle\Accounting\Model\Repository\EventRepository;
7+
use AppBundle\Accounting\Entity\Repository\EventRepository;
88
use Symfony\Component\HttpFoundation\Request;
99
use Symfony\Component\HttpFoundation\Response;
1010
use Twig\Environment;

sources/AppBundle/StaticAnalysis/Rule/DoctrineRepositoryRule.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PHPStan\Rules\Rule;
1414
use PHPStan\Rules\RuleErrorBuilder;
1515
use PHPStan\Type\Type;
16+
use Symfony\Component\Form\AbstractType;
1617

1718
/**
1819
* @implements Rule<MethodCall>
@@ -28,11 +29,13 @@
2829
];
2930

3031
private ClassReflection $repositoryClassReflection;
32+
private ClassReflection $formClassReflection;
3133

3234
public function __construct(
3335
private ReflectionProvider $reflectionProvider,
3436
) {
3537
$this->repositoryClassReflection = $this->reflectionProvider->getClass(EntityRepository::class);
38+
$this->formClassReflection = $this->reflectionProvider->getClass(AbstractType::class);
3639
}
3740

3841
public function getNodeType(): string
@@ -57,6 +60,8 @@ public function processNode(Node $node, Scope $scope): array
5760

5861
// Si l'appel est fait depuis l'intérieur d'un repository, c'est autorisé
5962
|| $this->isRepositoryClass($scope->getClassReflection())
63+
// Si l'appel est fait depuis l'intérieur d'un formulaire, c'est autorisé
64+
|| $this->isFormClass($scope->getClassReflection())
6065
) {
6166
return [];
6267
}
@@ -102,6 +107,28 @@ private function isRepositoryClass(ClassReflection|Type $target): bool
102107
|| $target->isSubclassOfClass($this->repositoryClassReflection);
103108
}
104109

110+
private function isFormClass(ClassReflection|Type $target): bool
111+
{
112+
if ($target instanceof Type) {
113+
$classes = $target->getObjectClassNames();
114+
115+
foreach ($classes as $class) {
116+
$classReflection = $this->reflectionProvider->getClass($class);
117+
118+
if ($classReflection->getName() === $this->formClassReflection->getName()
119+
|| $classReflection->isSubclassOfClass($this->formClassReflection)
120+
) {
121+
return true;
122+
}
123+
}
124+
125+
return false;
126+
}
127+
128+
return $target->getName() === $this->formClassReflection->getName()
129+
|| $target->isSubclassOfClass($this->formClassReflection);
130+
}
131+
105132
private function isMethodOverridden(Type $type, string $methodName): bool
106133
{
107134
if ($type->isObject()->no()) {

0 commit comments

Comments
 (0)