-
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
52 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,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() | ||
); | ||
} | ||
} |