From 4028cc8a8b28a4f79742249fd3f3ee48e5172866 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Sat, 22 Jul 2023 23:05:30 +0200 Subject: [PATCH] Set repositories for EventBundle on the new way + dependent issues --- .../Form/ParticipationType.php | 11 +--- src/Bundle/ChillEventBundle/Form/RoleType.php | 11 +--- .../Form/Type/PickEventType.php | 55 ++++++------------- .../Form/Type/PickRoleType.php | 28 ++-------- .../Form/Type/PickStatusType.php | 29 ++-------- .../Repository/EventRepository.php | 9 ++- .../Repository/ParticipationRepository.php | 15 +++-- .../Repository/RoleRepository.php | 25 +++++++++ .../Repository/StatusRepository.php | 24 ++++++++ .../config/services/forms.yaml | 30 +++------- .../config/services/repositories.yaml | 28 ++-------- .../config/services/search.yaml | 8 +-- src/Bundle/ChillMainBundle/Entity/User.php | 2 +- 13 files changed, 114 insertions(+), 161 deletions(-) create mode 100644 src/Bundle/ChillEventBundle/Repository/RoleRepository.php create mode 100644 src/Bundle/ChillEventBundle/Repository/StatusRepository.php diff --git a/src/Bundle/ChillEventBundle/Form/ParticipationType.php b/src/Bundle/ChillEventBundle/Form/ParticipationType.php index 998dc91a3..d53566cc5 100644 --- a/src/Bundle/ChillEventBundle/Form/ParticipationType.php +++ b/src/Bundle/ChillEventBundle/Form/ParticipationType.php @@ -16,6 +16,7 @@ use Chill\EventBundle\Entity\Status; use Chill\EventBundle\Form\Type\PickRoleType; use Chill\EventBundle\Form\Type\PickStatusType; use Chill\MainBundle\Templating\TranslatableStringHelper; +use Chill\MainBundle\Templating\TranslatableStringHelperInterface; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -25,16 +26,10 @@ use Symfony\Component\OptionsResolver\OptionsResolver; * * If the `event` option is defined, the role will be restricted */ -class ParticipationType extends AbstractType +final class ParticipationType extends AbstractType { - /** - * @var TranslatableStringHelper - */ - protected $translatableStringHelper; - - public function __construct(TranslatableStringHelper $translatableStringHelper) + public function __construct(private readonly TranslatableStringHelperInterface $translatableStringHelper) { - $this->translatableStringHelper = $translatableStringHelper; } public function buildForm(FormBuilderInterface $builder, array $options) diff --git a/src/Bundle/ChillEventBundle/Form/RoleType.php b/src/Bundle/ChillEventBundle/Form/RoleType.php index 519e814cf..b867aec63 100644 --- a/src/Bundle/ChillEventBundle/Form/RoleType.php +++ b/src/Bundle/ChillEventBundle/Form/RoleType.php @@ -14,21 +14,16 @@ namespace Chill\EventBundle\Form; use Chill\EventBundle\Entity\EventType; use Chill\MainBundle\Form\Type\TranslatableStringFormType; use Chill\MainBundle\Templating\TranslatableStringHelper; +use Chill\MainBundle\Templating\TranslatableStringHelperInterface; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; -class RoleType extends AbstractType +final class RoleType extends AbstractType { - /** - * @var TranslatableStringHelper - */ - protected $translatableStringHelper; - - public function __construct(TranslatableStringHelper $translatableStringHelper) + public function __construct(private readonly TranslatableStringHelperInterface $translatableStringHelper) { - $this->translatableStringHelper = $translatableStringHelper; } public function buildForm(FormBuilderInterface $builder, array $options) diff --git a/src/Bundle/ChillEventBundle/Form/Type/PickEventType.php b/src/Bundle/ChillEventBundle/Form/Type/PickEventType.php index 2bad90153..f844124ec 100644 --- a/src/Bundle/ChillEventBundle/Form/Type/PickEventType.php +++ b/src/Bundle/ChillEventBundle/Form/Type/PickEventType.php @@ -18,7 +18,7 @@ use Chill\EventBundle\Search\EventSearch; use Chill\MainBundle\Entity\Center; use Chill\MainBundle\Entity\GroupCenter; use Chill\MainBundle\Entity\User; -use Chill\MainBundle\Security\Authorization\AuthorizationHelper; +use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface; use RuntimeException; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Finder\Exception\AccessDeniedException; @@ -28,6 +28,7 @@ use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Symfony\Component\Security\Core\Role\Role; +use Symfony\Component\Security\Core\Security; use Symfony\Contracts\Translation\TranslatorInterface; use function in_array; @@ -36,48 +37,18 @@ use function is_array; /** * Class PickEventType. */ -class PickEventType extends AbstractType +final class PickEventType extends AbstractType { - /** - * @var AuthorizationHelper - */ - protected $authorizationHelper; - - /** - * @var EventRepository - */ - protected $eventRepository; - - /** - * @var TranslatorInterface - */ - protected $translator; - - /** - * @var UrlGeneratorInterface - */ - protected $urlGenerator; - - /** - * @var User - */ - protected $user; - /** * PickEventType constructor. */ public function __construct( - EventRepository $eventRepository, - TokenStorageInterface $tokenStorage, - AuthorizationHelper $authorizationHelper, - UrlGeneratorInterface $urlGenerator, - TranslatorInterface $translator + private readonly EventRepository $eventRepository, + private readonly AuthorizationHelperInterface $authorizationHelper, + private readonly UrlGeneratorInterface $urlGenerator, + private readonly TranslatorInterface $translator, + private readonly Security $security ) { - $this->eventRepository = $eventRepository; - $this->user = $tokenStorage->getToken()->getUser(); - $this->authorizationHelper = $authorizationHelper; - $this->urlGenerator = $urlGenerator; - $this->translator = $translator; } public function buildView(\Symfony\Component\Form\FormView $view, \Symfony\Component\Form\FormInterface $form, array $options) @@ -133,15 +104,21 @@ class PickEventType extends AbstractType */ protected function filterCenters(Options $options) { + $user = $this->security->getUser(); + + if (!$user instanceof User) { + return []; + } + // option role if (null === $options['role']) { $centers = array_map( static fn (GroupCenter $g) => $g->getCenter(), - $this->user->getGroupCenters()->toArray() + $user->getGroupCenters()->toArray() ); } else { $centers = $this->authorizationHelper->getReachableCenters( - $this->user, + $user, (string) $options['role']->getRole() ); } diff --git a/src/Bundle/ChillEventBundle/Form/Type/PickRoleType.php b/src/Bundle/ChillEventBundle/Form/Type/PickRoleType.php index 8fd8ff7d7..b49fc733d 100644 --- a/src/Bundle/ChillEventBundle/Form/Type/PickRoleType.php +++ b/src/Bundle/ChillEventBundle/Form/Type/PickRoleType.php @@ -13,7 +13,9 @@ namespace Chill\EventBundle\Form\Type; use Chill\EventBundle\Entity\EventType; use Chill\EventBundle\Entity\Role; +use Chill\EventBundle\Repository\RoleRepository; use Chill\MainBundle\Templating\TranslatableStringHelper; +use Chill\MainBundle\Templating\TranslatableStringHelperInterface; use Doctrine\ORM\EntityRepository; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; @@ -26,31 +28,13 @@ use Symfony\Contracts\Translation\TranslatorInterface; /** * Allow to pick a choice amongst different choices. */ -class PickRoleType extends AbstractType +final class PickRoleType extends AbstractType { - /** - * @var EntityRepository - */ - protected $roleRepository; - - /** - * @var TranslatableStringHelper - */ - protected $translatableStringHelper; - - /** - * @var TranslatorInterface - */ - protected $translator; - public function __construct( - TranslatableStringHelper $translatableStringHelper, - TranslatorInterface $translator, - EntityRepository $roleRepository + private readonly TranslatableStringHelperInterface $translatableStringHelper, + private readonly TranslatorInterface $translator, + private readonly RoleRepository $roleRepository ) { - $this->translatableStringHelper = $translatableStringHelper; - $this->translator = $translator; - $this->roleRepository = $roleRepository; } public function buildForm(FormBuilderInterface $builder, array $options) diff --git a/src/Bundle/ChillEventBundle/Form/Type/PickStatusType.php b/src/Bundle/ChillEventBundle/Form/Type/PickStatusType.php index dcc3ba205..588543c1e 100644 --- a/src/Bundle/ChillEventBundle/Form/Type/PickStatusType.php +++ b/src/Bundle/ChillEventBundle/Form/Type/PickStatusType.php @@ -13,7 +13,9 @@ namespace Chill\EventBundle\Form\Type; use Chill\EventBundle\Entity\EventType; use Chill\EventBundle\Entity\Status; +use Chill\EventBundle\Repository\StatusRepository; use Chill\MainBundle\Templating\TranslatableStringHelper; +use Chill\MainBundle\Templating\TranslatableStringHelperInterface; use Doctrine\ORM\EntityRepository; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; @@ -31,31 +33,10 @@ use Symfony\Contracts\Translation\TranslatorInterface; * - event_type : restricts to a certain event type. Default null (= all event types) * - active_only: restricts to active type only. Default true */ -class PickStatusType extends AbstractType +final class PickStatusType extends AbstractType { - /** - * @var EntityRepository - */ - protected $statusRepository; - - /** - * @var TranslatableStringHelper - */ - protected $translatableStringHelper; - - /** - * @var TranslatorInterface - */ - protected $translator; - - public function __construct( - TranslatableStringHelper $translatableStringHelper, - TranslatorInterface $translator, - EntityRepository $statusRepository - ) { - $this->translatableStringHelper = $translatableStringHelper; - $this->translator = $translator; - $this->statusRepository = $statusRepository; + public function __construct(protected TranslatableStringHelperInterface $translatableStringHelper, protected TranslatorInterface $translator, protected StatusRepository $statusRepository) + { } public function buildForm(FormBuilderInterface $builder, array $options) diff --git a/src/Bundle/ChillEventBundle/Repository/EventRepository.php b/src/Bundle/ChillEventBundle/Repository/EventRepository.php index d24841d5c..85ec84959 100644 --- a/src/Bundle/ChillEventBundle/Repository/EventRepository.php +++ b/src/Bundle/ChillEventBundle/Repository/EventRepository.php @@ -11,11 +11,18 @@ declare(strict_types=1); namespace Chill\EventBundle\Repository; +use Chill\EventBundle\Entity\Event; +use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\ORM\EntityRepository; +use Doctrine\Persistence\ManagerRegistry; /** * Class EventRepository. */ -class EventRepository extends EntityRepository +class EventRepository extends ServiceEntityRepository { + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, Event::class); + } } diff --git a/src/Bundle/ChillEventBundle/Repository/ParticipationRepository.php b/src/Bundle/ChillEventBundle/Repository/ParticipationRepository.php index cc4bd6f51..e6330ce3d 100644 --- a/src/Bundle/ChillEventBundle/Repository/ParticipationRepository.php +++ b/src/Bundle/ChillEventBundle/Repository/ParticipationRepository.php @@ -11,23 +11,28 @@ declare(strict_types=1); namespace Chill\EventBundle\Repository; -use Doctrine\ORM\EntityRepository; +use Chill\EventBundle\Entity\Participation; +use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; +use Doctrine\Persistence\ManagerRegistry; /** * Class ParticipationRepository. */ -class ParticipationRepository extends EntityRepository +class ParticipationRepository extends ServiceEntityRepository { + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, Participation::class); + } + /** * Count number of participations per person. * * @param $person_id * * @throws \Doctrine\ORM\NonUniqueResultException - * - * @return mixed */ - public function countByPerson($person_id) + public function countByPerson($person_id): int { return $this->createQueryBuilder('p') ->select('COUNT (p.id)') diff --git a/src/Bundle/ChillEventBundle/Repository/RoleRepository.php b/src/Bundle/ChillEventBundle/Repository/RoleRepository.php new file mode 100644 index 000000000..d38ac1354 --- /dev/null +++ b/src/Bundle/ChillEventBundle/Repository/RoleRepository.php @@ -0,0 +1,25 @@ + */ - public function getGroupCenters() + public function getGroupCenters(): Collection { return $this->groupCenters; }