Skip to content

Commit

Permalink
dev: Extract a ConstraintErrorsFormatter from BaseController
Browse files Browse the repository at this point in the history
This formatter is now reusable outside of controllers, making it more
useful!
  • Loading branch information
marien-probesys committed Aug 3, 2023
1 parent 8f5ce4a commit cc94181
Show file tree
Hide file tree
Showing 15 changed files with 70 additions and 40 deletions.
3 changes: 2 additions & 1 deletion docs/developers/notifications.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace App\Controller;
use App\Controller\BaseController;
use App\Entity\Ticket;
use App\Repository\TicketRepository;
use App\Utils\ConstraintErrorsFormatter;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
Expand Down Expand Up @@ -44,7 +45,7 @@ class TicketsController extends BaseController

$errors = $validator->validate($ticket);
if (count($errors) > 0) {
$error = implode(' ', $this->formatErrors($errors));
$error = implode(' ', ConstraintErrorsFormatter::format($errors));
$this->addFlash('error', $error);
return $this->redirectToRoute('ticket', [
'uid' => $ticket->getUid(),
Expand Down
21 changes: 0 additions & 21 deletions src/Controller/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Validator\ConstraintViolationListInterface;

class BaseController extends AbstractController
{
Expand All @@ -27,26 +26,6 @@ protected function renderBadRequest(string $view, array $parameters = [], Respon
return $this->render($view, $parameters, $response);
}

/**
* @return array<string, string>
*/
protected function formatErrors(ConstraintViolationListInterface $errors): array
{
$formattedErrors = [];
foreach ($errors as $error) {
$property = $error->getPropertyPath();
if (isset($formattedErrors[$property])) {
$formattedErrors[$property] = implode(
' ',
[$formattedErrors[$property], $error->getMessage()],
);
} else {
$formattedErrors[$property] = $error->getMessage();
}
}
return $formattedErrors;
}

protected function isPathRedirectable(string $path): bool
{
$router = $this->container->get('router');
Expand Down
5 changes: 3 additions & 2 deletions src/Controller/MailboxesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use App\Repository\MailboxEmailRepository;
use App\Security\Encryptor;
use App\Service\Sorter\MailboxSorter;
use App\Utils\ConstraintErrorsFormatter;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Messenger\MessageBusInterface;
Expand Down Expand Up @@ -135,7 +136,7 @@ public function create(
'username' => $username,
'password' => $password,
'folder' => $folder,
'errors' => $this->formatErrors($errors),
'errors' => ConstraintErrorsFormatter::format($errors),
]);
}

Expand Down Expand Up @@ -236,7 +237,7 @@ public function update(
'encryption' => $encryption,
'username' => $username,
'folder' => $folder,
'errors' => $this->formatErrors($errors),
'errors' => ConstraintErrorsFormatter::format($errors),
]);
}

Expand Down
5 changes: 3 additions & 2 deletions src/Controller/Organizations/TicketsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use App\SearchEngine\TicketFilter;
use App\SearchEngine\TicketSearcher;
use App\Service\ActorsLister;
use App\Utils\ConstraintErrorsFormatter;
use App\Utils\Time;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HtmlSanitizer\HtmlSanitizerInterface;
Expand Down Expand Up @@ -296,7 +297,7 @@ public function create(
'priority' => $priority,
'allUsers' => $allUsers,
'techUsers' => $techUsers,
'errors' => $this->formatErrors($errors),
'errors' => ConstraintErrorsFormatter::format($errors),
]);
}

Expand All @@ -321,7 +322,7 @@ public function create(
'priority' => $priority,
'allUsers' => $allUsers,
'techUsers' => $techUsers,
'errors' => $this->formatErrors($errors),
'errors' => ConstraintErrorsFormatter::format($errors),
]);
}

Expand Down
5 changes: 3 additions & 2 deletions src/Controller/OrganizationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Entity\Organization;
use App\Repository\OrganizationRepository;
use App\Service\Sorter\OrganizationSorter;
use App\Utils\ConstraintErrorsFormatter;
use App\Utils\Time;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -138,7 +139,7 @@ public function create(
'organizations' => $organizations,
'name' => $name,
'selectedParentUid' => $selectedParentUid,
'errors' => $this->formatErrors($errors),
'errors' => ConstraintErrorsFormatter::format($errors),
]);
}

Expand Down Expand Up @@ -199,7 +200,7 @@ public function update(
return $this->renderBadRequest('organizations/edit.html.twig', [
'organization' => $organization,
'name' => $name,
'errors' => $this->formatErrors($errors),
'errors' => ConstraintErrorsFormatter::format($errors),
]);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Controller/PreferencesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace App\Controller;

use App\Repository\UserRepository;
use App\Utils\ConstraintErrorsFormatter;
use App\Utils\Locales;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -71,7 +72,7 @@ public function update(
'colorScheme' => $colorScheme,
'locale' => $locale,
'availableLanguages' => Locales::getSupportedLanguages(),
'errors' => $this->formatErrors($errors),
'errors' => ConstraintErrorsFormatter::format($errors),
]);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Controller/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace App\Controller;

use App\Repository\UserRepository;
use App\Utils\ConstraintErrorsFormatter;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
Expand Down Expand Up @@ -91,7 +92,7 @@ public function update(
return $this->renderBadRequest('profile/edit.html.twig', [
'name' => $name,
'email' => $email,
'errors' => $this->formatErrors($errors),
'errors' => ConstraintErrorsFormatter::format($errors),
]);
}

Expand Down
5 changes: 3 additions & 2 deletions src/Controller/RolesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Entity\Role;
use App\Repository\RoleRepository;
use App\Service\Sorter\RoleSorter;
use App\Utils\ConstraintErrorsFormatter;
use App\Utils\Time;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -122,7 +123,7 @@ public function create(
'name' => $name,
'description' => $description,
'permissions' => $permissions,
'errors' => $this->formatErrors($errors),
'errors' => ConstraintErrorsFormatter::format($errors),
]);
}

Expand Down Expand Up @@ -217,7 +218,7 @@ public function update(
'name' => $name,
'description' => $description,
'permissions' => $permissions,
'errors' => $this->formatErrors($errors),
'errors' => ConstraintErrorsFormatter::format($errors),
]);
}

Expand Down
5 changes: 3 additions & 2 deletions src/Controller/Tickets/MessagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use App\Repository\OrganizationRepository;
use App\Repository\TicketRepository;
use App\Service\TicketTimeline;
use App\Utils\ConstraintErrorsFormatter;
use App\Utils\Time;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HtmlSanitizer\HtmlSanitizerInterface;
Expand Down Expand Up @@ -131,7 +132,7 @@ public function create(
'statuses' => $statuses,
'isSolution' => $isSolution,
'isConfidential' => $isConfidential,
'errors' => $this->formatErrors($errors),
'errors' => ConstraintErrorsFormatter::format($errors),
]);
}

Expand Down Expand Up @@ -165,7 +166,7 @@ public function create(
'statuses' => $statuses,
'isSolution' => $isSolution,
'isConfidential' => $isConfidential,
'errors' => $this->formatErrors($errors),
'errors' => ConstraintErrorsFormatter::format($errors),
]);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Controller/Tickets/PriorityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Controller\BaseController;
use App\Entity\Ticket;
use App\Repository\TicketRepository;
use App\Utils\ConstraintErrorsFormatter;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
Expand Down Expand Up @@ -89,7 +90,7 @@ public function update(
'urgency' => $urgency,
'impact' => $impact,
'priority' => $priority,
'errors' => $this->formatErrors($errors),
'errors' => ConstraintErrorsFormatter::format($errors),
]);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Controller/Tickets/StatusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Controller\BaseController;
use App\Entity\Ticket;
use App\Repository\TicketRepository;
use App\Utils\ConstraintErrorsFormatter;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
Expand Down Expand Up @@ -82,7 +83,7 @@ public function update(
'ticket' => $ticket,
'status' => $status,
'statuses' => $statuses,
'errors' => $this->formatErrors($errors),
'errors' => ConstraintErrorsFormatter::format($errors),
]);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Controller/Tickets/TitleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Controller\BaseController;
use App\Entity\Ticket;
use App\Repository\TicketRepository;
use App\Utils\ConstraintErrorsFormatter;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
Expand Down Expand Up @@ -78,7 +79,7 @@ public function update(
return $this->renderBadRequest('tickets/title/edit.html.twig', [
'ticket' => $ticket,
'title' => $title,
'errors' => $this->formatErrors($errors),
'errors' => ConstraintErrorsFormatter::format($errors),
]);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Controller/Tickets/TypeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Controller\BaseController;
use App\Entity\Ticket;
use App\Repository\TicketRepository;
use App\Utils\ConstraintErrorsFormatter;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
Expand Down Expand Up @@ -54,7 +55,7 @@ public function update(

$errors = $validator->validate($ticket);
if (count($errors) > 0) {
$error = implode(' ', $this->formatErrors($errors));
$error = implode(' ', ConstraintErrorsFormatter::format($errors));
$this->addFlash('error', $error);
return $this->redirectToRoute('ticket', [
'uid' => $ticket->getUid(),
Expand Down
5 changes: 3 additions & 2 deletions src/Controller/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use App\Repository\UserRepository;
use App\Service\Sorter\OrganizationSorter;
use App\Service\Sorter\UserSorter;
use App\Utils\ConstraintErrorsFormatter;
use App\Utils\Time;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -116,7 +117,7 @@ public function create(
'email' => $email,
'name' => $name,
'organizationUid' => $organizationUid,
'errors' => $this->formatErrors($errors),
'errors' => ConstraintErrorsFormatter::format($errors),
]);
}

Expand Down Expand Up @@ -210,7 +211,7 @@ public function update(
'email' => $email,
'name' => $name,
'organizationUid' => $organizationUid,
'errors' => $this->formatErrors($errors),
'errors' => ConstraintErrorsFormatter::format($errors),
]);
}

Expand Down
38 changes: 38 additions & 0 deletions src/Utils/ConstraintErrorsFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

// This file is part of Bileto.
// Copyright 2022-2023 Probesys
// SPDX-License-Identifier: AGPL-3.0-or-later

namespace App\Utils;

use Symfony\Component\Validator\ConstraintViolationListInterface;

class ConstraintErrorsFormatter
{
/**
* Format a list of errors returned by a Validator into a list of errors
* displayable in the interface.
*
* The keys are the properties of the concerned Entity, and the values are
* a concatenation of the corresponding error messages.
*
* @return array<string, string>
*/
public static function format(ConstraintViolationListInterface $errors): array
{
$formattedErrors = [];
foreach ($errors as $error) {
$property = $error->getPropertyPath();
if (isset($formattedErrors[$property])) {
$formattedErrors[$property] = implode(
' ',
[$formattedErrors[$property], $error->getMessage()],
);
} else {
$formattedErrors[$property] = $error->getMessage();
}
}
return $formattedErrors;
}
}

0 comments on commit cc94181

Please sign in to comment.