Skip to content

Commit

Permalink
feat(user): add UserHandler
Browse files Browse the repository at this point in the history
see: #30
  • Loading branch information
n3wborn committed Jan 13, 2024
1 parent 2b7f1b4 commit 141ff6c
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/Service/User/UserHandler.php
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;
}
}

0 comments on commit 141ff6c

Please sign in to comment.