Skip to content

Commit

Permalink
feat(user): add UserFinder
Browse files Browse the repository at this point in the history
see: #30
  • Loading branch information
n3wborn committed Nov 1, 2023
1 parent a9e3f50 commit 5b52439
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/Service/User/UserFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Service\User;

use App\Entity\Project;
use App\Entity\User;
use App\Exception\NotFoundException;
use App\Helper\ApiMessages;
use App\Repository\UserRepository;

final class UserFinder
{
public function __construct(
private UserRepository $userRepository,
) {
}

public function get(User $user): ?User
{
$user->isArchived()
&& throw new NotFoundException(ApiMessages::translate(ApiMessages::USER_UNKNOWN));

return
(
null !== ($result = $this->userRepository->find($user))
)
? $result
: null;
}

public function getAll(): array
{
return $this->userRepository->findAll();
}

public function getAllNotArchived(): array
{
return $this->userRepository->findAllNotArchived();
}

/** @throws NotFoundException */
public function getUserProjects(?User $user): array
{
(!UserHelper::userExists($user))
&& throw new NotFoundException(ApiMessages::translate(ApiMessages::USER_UNKNOWN));

return array_filter(
$user->getProjects()->toArray(),
fn (Project $project) => !$project->isArchived()
);
}
}

0 comments on commit 5b52439

Please sign in to comment.