Set repositories for EventBundle on the new way + dependent issues

This commit is contained in:
2023-07-22 23:05:30 +02:00
parent 48cd8aaa9f
commit 4028cc8a8b
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\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)

View File

@@ -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)

View File

@@ -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()
);
}

View File

@@ -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)

View File

@@ -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)