Skip to content

Commit

Permalink
feat(Admin): Create an update event for each bulk entity
Browse files Browse the repository at this point in the history
  • Loading branch information
roadiz-ci committed Feb 28, 2024
1 parent fe4d56c commit f967af1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
30 changes: 11 additions & 19 deletions src/Controllers/AbstractAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Symfony\Component\Form\Exception\InvalidConfigurationException;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
Expand All @@ -24,17 +23,10 @@ abstract class AbstractAdminController extends RozierApp
{
public const ITEM_PER_PAGE = 20;

protected SerializerInterface $serializer;
protected UrlGeneratorInterface $urlGenerator;

/**
* @param SerializerInterface $serializer
* @param UrlGeneratorInterface $urlGenerator
*/
public function __construct(SerializerInterface $serializer, UrlGeneratorInterface $urlGenerator)
{
$this->serializer = $serializer;
$this->urlGenerator = $urlGenerator;
public function __construct(
protected readonly SerializerInterface $serializer,
protected readonly UrlGeneratorInterface $urlGenerator
) {
}

/**
Expand Down Expand Up @@ -101,7 +93,7 @@ protected function getRequiredExportRole(): string
* @return Response|null
* @throws \Twig\Error\RuntimeError
*/
public function defaultAction(Request $request)
public function defaultAction(Request $request): ?Response
{
$this->denyAccessUnlessGranted($this->getRequiredListingRole());
$this->additionalAssignation($request);
Expand Down Expand Up @@ -132,10 +124,10 @@ public function defaultAction(Request $request)

/**
* @param Request $request
* @return RedirectResponse|Response|null
* @return Response|null
* @throws \Twig\Error\RuntimeError
*/
public function addAction(Request $request)
public function addAction(Request $request): ?Response
{
$this->denyAccessUnlessGranted($this->getRequiredCreationRole());
$this->additionalAssignation($request);
Expand Down Expand Up @@ -188,7 +180,7 @@ public function addAction(Request $request)
* @return Response|null
* @throws \Twig\Error\RuntimeError
*/
public function editAction(Request $request, $id)
public function editAction(Request $request, $id): ?Response
{
$this->denyAccessUnlessGranted($this->getRequiredEditionRole());
$this->additionalAssignation($request);
Expand Down Expand Up @@ -271,10 +263,10 @@ public function exportAction(Request $request): JsonResponse
/**
* @param Request $request
* @param int|string $id Numeric ID or UUID
* @return RedirectResponse|Response|null
* @return Response|null
* @throws \Twig\Error\RuntimeError
*/
public function deleteAction(Request $request, $id)
public function deleteAction(Request $request, $id): ?Response
{
$this->denyAccessUnlessGranted($this->getRequiredDeletionRole());
$this->additionalAssignation($request);
Expand Down Expand Up @@ -491,7 +483,7 @@ protected function getPostDeleteResponse(PersistableInterface $item): Response
* @param T|iterable<T>|array<int, T>|null $event
* @return T|iterable<T>|array<int, T>|null
*/
protected function dispatchSingleOrMultipleEvent(mixed $event): mixed
protected function dispatchSingleOrMultipleEvent(mixed $event): object|array|null
{
if (null === $event) {
return null;
Expand Down
9 changes: 5 additions & 4 deletions src/Controllers/AbstractAdminWithBulkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@

abstract class AbstractAdminWithBulkController extends AbstractAdminController
{
protected FormFactoryInterface $formFactory;

public function __construct(
FormFactoryInterface $formFactory,
protected readonly FormFactoryInterface $formFactory,
SerializerInterface $serializer,
UrlGeneratorInterface $urlGenerator
) {
parent::__construct($serializer, $urlGenerator);
$this->formFactory = $formFactory;
}

protected function additionalAssignation(Request $request): void
Expand Down Expand Up @@ -136,6 +133,10 @@ protected function bulkAction(
foreach ($items as $item) {
if ($this->supports($item)) {
$alterItemCallable($item);
$updateEvent = $this->createUpdateEvent($item);
if (null !== $updateEvent) {
$this->dispatchSingleOrMultipleEvent($updateEvent);
}
$msg = $this->getTranslator()->trans(
$confirmMessageTemplate,
[
Expand Down

0 comments on commit f967af1

Please sign in to comment.