Files
cheeper-ddd-cqrs-example/src/Cheeper/Chapter5/Application/Query/CountFollowersHandlerWithRepositoriesAccess/CountFollowersHandler.php
2022-02-02 16:06:28 +01:00

46 lines
1.4 KiB
PHP

<?php
declare(strict_types=1);
namespace Cheeper\Chapter5\Application\Query\CountFollowersHandlerWithRepositoriesAccess;
use Cheeper\AllChapters\DomainModel\Author\AuthorDoesNotExist;
use Cheeper\AllChapters\DomainModel\Author\AuthorId;
use Cheeper\AllChapters\DomainModel\Author\AuthorRepository;
use Cheeper\Chapter5\Application\Query\CountFollowers;
use Cheeper\Chapter5\Application\Query\CountFollowersResponse;
use Cheeper\Chapter5\DomainModel\Follow\Followers;
//snippet count-followers-handler
final class CountFollowersHandler
{
public function __construct(
private Followers $followers,
private AuthorRepository $authors
) {
}
public function __invoke(CountFollowers $query): CountFollowersResponse
{
$authorId = AuthorId::fromString($query->authorId());
$author = $this->authors->ofId($authorId);
if (null === $author) {
throw AuthorDoesNotExist::withAuthorIdOf($authorId);
}
$followersCount = $this->followers->ofAuthorId($authorId)?->followers() ?? 0;
// Other option would be with a counter method in the Repository
// $followersCount = $this->followers->countOfAuthorId($authorId));
return new CountFollowersResponse(
authorId: $authorId->toString(),
authorUsername: $author->userName()->userName(),
numberOfFollowers: $followersCount
);
}
}
//end-snippet