Skip to content

Commit

Permalink
fix(Helper): edit/create check
Browse files Browse the repository at this point in the history
  • Loading branch information
n3wborn committed Sep 11, 2023
1 parent 61d7e20 commit ae6cce8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
20 changes: 17 additions & 3 deletions src/Service/Project/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,40 @@
use App\Entity\Project;
use App\Exception\NotFoundException;
use App\Helper\ApiMessages;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;

final class Helper
{
public function __construct(
private EntityManagerInterface $em,
) {
}

public static function isEditRoute(Request $request): bool
{
return ProjectController::ROUTE_EDIT === $request->get('_route');
}

/** @throws NotFoundException */
public function editSlugParamExists(Request $request): ?Project
{
return isset($request->get('_route_params')['slug'])
? $this->em->getRepository(Project::class)->findOneBy(['slug' => $request->get('_route_params')['slug']])
: null;
}

public static function generateEditSuccessMessage(Request $request): string
{
return ProjectController::ROUTE_EDIT === $request->get('_route')
return self::isEditRoute($request)
? ApiMessages::PROJECT_UPDATE_SUCCESS_MESSAGE
: ApiMessages::PROJECT_CREATE_SUCCESS_MESSAGE;
}

/** @throws NotFoundException */
public static function validateRequestResource(Request $request, Project $project): void
public function validateRequestResource(Request $request): void
{
(self::isEditRoute($request) && (null === $project))
(self::isEditRoute($request) && (null === $this->editSlugParamExists($request)))
&& throw new NotFoundException(ApiMessages::translate(ApiMessages::PROJECT_UNKNOWN));
}
}
3 changes: 2 additions & 1 deletion src/Service/Project/Persister.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ final class Persister
public function __construct(
private Validator $validator,
private EntityManagerInterface $em,
private Helper $helper,
private LoggerInterface $logger,
private SerializerInterface $serializer,
) {
Expand All @@ -26,7 +27,7 @@ public function __construct(
public function processRequest(Project $project, DTO $dto, Request $request): JsonResponse
{
try {
Helper::validateRequestResource($request, $project);
$this->helper->validateRequestResource($request, $project);
$this->validator->validate($dto);
$this->persist($project, $dto);

Expand Down
4 changes: 0 additions & 4 deletions src/Service/Project/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ final class Validator
public const NAME_SHOULD_NOT_BE_EMPTY = 'Le champ Nom ne peut être vide';
public const DESCRIPTION_SHOULD_NOT_BE_EMPTY = 'Le champ Description ne peut être vide';

public function __construct()
{
}

/** @throws BadDataException*/
public function validate(DTO $dto): void
{
Expand Down

0 comments on commit ae6cce8

Please sign in to comment.