Set repositories for EventBundle on the new way + dependent issues

This commit is contained in:
Julien Fastré 2023-07-22 23:05:30 +02:00
parent 48cd8aaa9f
commit 4028cc8a8b
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
13 changed files with 114 additions and 161 deletions

View File

@ -16,6 +16,7 @@ use Chill\EventBundle\Entity\Status;
use Chill\EventBundle\Form\Type\PickRoleType; use Chill\EventBundle\Form\Type\PickRoleType;
use Chill\EventBundle\Form\Type\PickStatusType; use Chill\EventBundle\Form\Type\PickStatusType;
use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver; 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 * If the `event` option is defined, the role will be restricted
*/ */
class ParticipationType extends AbstractType final class ParticipationType extends AbstractType
{ {
/** public function __construct(private readonly TranslatableStringHelperInterface $translatableStringHelper)
* @var TranslatableStringHelper
*/
protected $translatableStringHelper;
public function __construct(TranslatableStringHelper $translatableStringHelper)
{ {
$this->translatableStringHelper = $translatableStringHelper;
} }
public function buildForm(FormBuilderInterface $builder, array $options) public function buildForm(FormBuilderInterface $builder, array $options)

View File

@ -14,21 +14,16 @@ namespace Chill\EventBundle\Form;
use Chill\EventBundle\Entity\EventType; use Chill\EventBundle\Entity\EventType;
use Chill\MainBundle\Form\Type\TranslatableStringFormType; use Chill\MainBundle\Form\Type\TranslatableStringFormType;
use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class RoleType extends AbstractType final class RoleType extends AbstractType
{ {
/** public function __construct(private readonly TranslatableStringHelperInterface $translatableStringHelper)
* @var TranslatableStringHelper
*/
protected $translatableStringHelper;
public function __construct(TranslatableStringHelper $translatableStringHelper)
{ {
$this->translatableStringHelper = $translatableStringHelper;
} }
public function buildForm(FormBuilderInterface $builder, array $options) public function buildForm(FormBuilderInterface $builder, array $options)

View File

@ -18,7 +18,7 @@ use Chill\EventBundle\Search\EventSearch;
use Chill\MainBundle\Entity\Center; use Chill\MainBundle\Entity\Center;
use Chill\MainBundle\Entity\GroupCenter; use Chill\MainBundle\Entity\GroupCenter;
use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Security\Authorization\AuthorizationHelper; use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface;
use RuntimeException; use RuntimeException;
use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Finder\Exception\AccessDeniedException; use Symfony\Component\Finder\Exception\AccessDeniedException;
@ -28,6 +28,7 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Role\Role; use Symfony\Component\Security\Core\Role\Role;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\Contracts\Translation\TranslatorInterface;
use function in_array; use function in_array;
@ -36,48 +37,18 @@ use function is_array;
/** /**
* Class PickEventType. * 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. * PickEventType constructor.
*/ */
public function __construct( public function __construct(
EventRepository $eventRepository, private readonly EventRepository $eventRepository,
TokenStorageInterface $tokenStorage, private readonly AuthorizationHelperInterface $authorizationHelper,
AuthorizationHelper $authorizationHelper, private readonly UrlGeneratorInterface $urlGenerator,
UrlGeneratorInterface $urlGenerator, private readonly TranslatorInterface $translator,
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) 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) protected function filterCenters(Options $options)
{ {
$user = $this->security->getUser();
if (!$user instanceof User) {
return [];
}
// option role // option role
if (null === $options['role']) { if (null === $options['role']) {
$centers = array_map( $centers = array_map(
static fn (GroupCenter $g) => $g->getCenter(), static fn (GroupCenter $g) => $g->getCenter(),
$this->user->getGroupCenters()->toArray() $user->getGroupCenters()->toArray()
); );
} else { } else {
$centers = $this->authorizationHelper->getReachableCenters( $centers = $this->authorizationHelper->getReachableCenters(
$this->user, $user,
(string) $options['role']->getRole() (string) $options['role']->getRole()
); );
} }

View File

@ -13,7 +13,9 @@ namespace Chill\EventBundle\Form\Type;
use Chill\EventBundle\Entity\EventType; use Chill\EventBundle\Entity\EventType;
use Chill\EventBundle\Entity\Role; use Chill\EventBundle\Entity\Role;
use Chill\EventBundle\Repository\RoleRepository;
use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
@ -26,31 +28,13 @@ use Symfony\Contracts\Translation\TranslatorInterface;
/** /**
* Allow to pick a choice amongst different choices. * 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( public function __construct(
TranslatableStringHelper $translatableStringHelper, private readonly TranslatableStringHelperInterface $translatableStringHelper,
TranslatorInterface $translator, private readonly TranslatorInterface $translator,
EntityRepository $roleRepository private readonly RoleRepository $roleRepository
) { ) {
$this->translatableStringHelper = $translatableStringHelper;
$this->translator = $translator;
$this->roleRepository = $roleRepository;
} }
public function buildForm(FormBuilderInterface $builder, array $options) public function buildForm(FormBuilderInterface $builder, array $options)

View File

@ -13,7 +13,9 @@ namespace Chill\EventBundle\Form\Type;
use Chill\EventBundle\Entity\EventType; use Chill\EventBundle\Entity\EventType;
use Chill\EventBundle\Entity\Status; use Chill\EventBundle\Entity\Status;
use Chill\EventBundle\Repository\StatusRepository;
use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType; 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) * - event_type : restricts to a certain event type. Default null (= all event types)
* - active_only: restricts to active type only. Default true * - active_only: restricts to active type only. Default true
*/ */
class PickStatusType extends AbstractType final class PickStatusType extends AbstractType
{ {
/** public function __construct(protected TranslatableStringHelperInterface $translatableStringHelper, protected TranslatorInterface $translator, protected StatusRepository $statusRepository)
* @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 buildForm(FormBuilderInterface $builder, array $options) public function buildForm(FormBuilderInterface $builder, array $options)

View File

@ -11,11 +11,18 @@ declare(strict_types=1);
namespace Chill\EventBundle\Repository; namespace Chill\EventBundle\Repository;
use Chill\EventBundle\Entity\Event;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/** /**
* Class EventRepository. * Class EventRepository.
*/ */
class EventRepository extends EntityRepository class EventRepository extends ServiceEntityRepository
{ {
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Event::class);
}
} }

View File

@ -11,23 +11,28 @@ declare(strict_types=1);
namespace Chill\EventBundle\Repository; 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.
*/ */
class ParticipationRepository extends EntityRepository class ParticipationRepository extends ServiceEntityRepository
{ {
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Participation::class);
}
/** /**
* Count number of participations per person. * Count number of participations per person.
* *
* @param $person_id * @param $person_id
* *
* @throws \Doctrine\ORM\NonUniqueResultException * @throws \Doctrine\ORM\NonUniqueResultException
*
* @return mixed
*/ */
public function countByPerson($person_id) public function countByPerson($person_id): int
{ {
return $this->createQueryBuilder('p') return $this->createQueryBuilder('p')
->select('COUNT (p.id)') ->select('COUNT (p.id)')

View File

@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\EventBundle\Repository;
use Chill\EventBundle\Entity\Role;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
class RoleRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Role::class);
}
}

View File

@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\EventBundle\Repository;
use Chill\EventBundle\Entity\Status;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
class StatusRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Status::class);
}
}

View File

@ -1,49 +1,33 @@
services: services:
_defaults:
autowire: true
autoconfigure: true
chill.event.form.type.pick_event_type: chill.event.form.type.pick_event_type:
class: Chill\EventBundle\Form\Type\PickEventTypeType class: Chill\EventBundle\Form\Type\PickEventTypeType
arguments:
- "@chill.main.helper.translatable_string"
tags: tags:
- { name: form.type } - { name: form.type }
chill.event.form.participation_type: chill.event.form.participation_type:
class: Chill\EventBundle\Form\ParticipationType class: Chill\EventBundle\Form\ParticipationType
arguments:
- "@chill.main.helper.translatable_string"
tags: tags:
- { name: form.type } - { name: form.type }
chill.event.form.pick_role_type: chill.event.form.pick_role_type:
class: Chill\EventBundle\Form\Type\PickRoleType class: Chill\EventBundle\Form\Type\PickRoleType
arguments:
- "@chill.main.helper.translatable_string"
- "@translator"
- "@chill_event.repository.role"
tags: tags:
- { name: form.type } - { name: form.type }
chill.event.form.pick_status_type: chill.event.form.pick_status_type:
class: Chill\EventBundle\Form\Type\PickStatusType class: Chill\EventBundle\Form\Type\PickStatusType
arguments:
- "@chill.main.helper.translatable_string"
- "@translator"
- "@chill_event.repository.status"
tags: tags:
- { name: form.type } - { name: form.type }
chill.event.form.role_type: chill.event.form.role_type:
class: Chill\EventBundle\Form\RoleType class: Chill\EventBundle\Form\RoleType
arguments:
- "@chill.main.helper.translatable_string"
tags: tags:
- { name: form.type } - { name: form.type }
Chill\EventBundle\Form\Type\PickEventType: Chill\EventBundle\Form\Type\PickEventType:
arguments:
$eventRepository: "@chill_event.repository.event"
$tokenStorage: "@security.token_storage"
$authorizationHelper: "@chill.main.security.authorization.helper"
$urlGenerator: '@Symfony\Component\Routing\Generator\UrlGeneratorInterface'
$translator: '@Symfony\Contracts\Translation\TranslatorInterface'
tags: tags:
- { name: form.type } - { name: form.type }

View File

@ -1,25 +1,5 @@
services: services:
Chill\EventBundle\Repository\:
chill_event.repository.event: resource: './../../Repository'
class: Chill\EventBundle\Repository\EventRepository autowire: true
factory: ['@doctrine.orm.entity_manager', getRepository] autoconfigure: true
arguments:
- 'Chill\EventBundle\Entity\Event'
chill_event.repository.participation:
class: Chill\EventBundle\Repository\ParticipationRepository
factory: ['@doctrine.orm.entity_manager', getRepository]
arguments:
- 'Chill\EventBundle\Entity\Participation'
chill_event.repository.role:
class: Doctrine\ORM\EntityRepository
factory: ['@doctrine.orm.entity_manager', getRepository]
arguments:
- 'Chill\EventBundle\Entity\Role'
chill_event.repository.status:
class: Doctrine\ORM\EntityRepository
factory: ['@doctrine.orm.entity_manager', getRepository]
arguments:
- 'Chill\EventBundle\Entity\Status'

View File

@ -1,11 +1,7 @@
services: services:
Chill\EventBundle\Search\EventSearch: Chill\EventBundle\Search\EventSearch:
arguments: autoconfigure: true
$security: '@Symfony\Component\Security\Core\Security' autowire: true
$eventRepository: "@chill_event.repository.event"
$authorizationHelper: "@chill.main.security.authorization.helper"
$templating: "@templating"
$paginatorFactory: "@chill_main.paginator_factory"
tags: tags:
- { name: chill.search, alias: 'event_regular' } - { name: chill.search, alias: 'event_regular' }

View File

@ -226,7 +226,7 @@ class User implements UserInterface, \Stringable
/** /**
* @return Collection<GroupCenter> * @return Collection<GroupCenter>
*/ */
public function getGroupCenters() public function getGroupCenters(): Collection
{ {
return $this->groupCenters; return $this->groupCenters;
} }