-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
see: #30
- Loading branch information
Showing
1 changed file
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace App\Service\User; | ||
|
||
use App\Entity\Project; | ||
use App\Entity\User; | ||
use App\Exception\NotFoundException; | ||
use App\Helper\ApiMessages; | ||
use App\Helper\ApiResponse; | ||
use App\Helper\ExceptionLogger; | ||
use App\Service\Project\ProjectMapper; | ||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
final class UserHandler | ||
{ | ||
public function __construct( | ||
private UserFinder $finder, | ||
private ExceptionLogger $logger, | ||
private UserArchiver $archiver, | ||
private UserPersister $persister, | ||
) { | ||
} | ||
|
||
public function handleGetUserInfos(?User $user): JsonResponse | ||
{ | ||
try { | ||
null === $user | ||
&& throw new NotFoundException(ApiMessages::translate(ApiMessages::USER_NOT_FOUND)); | ||
|
||
$userInfos = $this->finder->get($user); | ||
$result = UserMapper::fromEntityToJson($userInfos); | ||
$response = new ApiResponse($result); | ||
} catch (NotFoundException $exception) { | ||
$this->logger->logNotice($exception); | ||
$response = ApiResponse::createWarningMessage($exception->getMessage()); | ||
} catch (\Throwable $exception) { | ||
$this->logger->logCriticalAndTrace($exception); | ||
$response = ApiResponse::createErrorMessage(ApiMessages::DEFAULT_ERROR_MESSAGE, exception: $exception); | ||
} | ||
|
||
return $response; | ||
} | ||
|
||
public function handleGetAllUsers(): JsonResponse | ||
{ | ||
$users = $this->finder->getAllNotArchived(); | ||
$result = array_map(static fn (User $user) => UserMapper::fromEntityToJson($user), $users); | ||
|
||
return new ApiResponse($result); | ||
} | ||
|
||
public function handlePersistUser(?User $user, Request $request, UserDTO $dto): JsonResponse | ||
{ | ||
return $this->persister->processRequest($user ?? new User(), $dto, $request); | ||
} | ||
|
||
public function handleArchiveUser(?User $user): JsonResponse | ||
{ | ||
return $this->archiver->process($user); | ||
} | ||
|
||
public function handleGetUserProjects(?User $user): JsonResponse | ||
{ | ||
try { | ||
$projects = $this->finder->getUserProjects($user); | ||
$result = array_map(static fn (Project $project) => ProjectMapper::fromEntityToJson($project), $projects); | ||
$response = new ApiResponse($result); | ||
} catch (NotFoundException $exception) { | ||
$this->logger->logNotice($exception); | ||
$response = ApiResponse::createWarningMessage($exception->getMessage()); | ||
} catch (\Throwable $exception) { | ||
$this->logger->logCriticalAndTrace($exception); | ||
$response = ApiResponse::createErrorMessage(ApiMessages::DEFAULT_ERROR_MESSAGE, exception: $exception); | ||
} | ||
|
||
return $response; | ||
} | ||
} |