[Chapter 7] Clean up unnecessary level

This commit is contained in:
Carlos Buenosvinos
2022-03-12 13:05:26 +01:00
parent 043c6be68b
commit 97a12fa542
2 changed files with 26 additions and 7 deletions

View File

@@ -10,6 +10,8 @@ use Redis;
//snippet create-followers-counter-projection-projector
final class CreateFollowersCounterProjectionHandler implements CreateFollowersCounterProjectionHandlerInterface
{
public const REDIS_KEY_TEMPLATE = 'author_followers_counter_projection:%s';
public function __construct(
private Redis $redis
) {
@@ -26,7 +28,10 @@ final class CreateFollowersCounterProjectionHandler implements CreateFollowersCo
];
$this->redis->set(
'author_followers_counter_projection:'.$authorId->toString(),
sprintf(
self::REDIS_KEY_TEMPLATE,
$authorId->toString()
),
json_encode($result)
);
}

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Cheeper\Chapter7\Application\Cheep\Projection;
use Cheeper\Chapter7\Application\Author\Projection\CreateFollowersCounterProjectionHandler;
use Redis;
//snippet add-cheep-to-timeline-projector
@@ -17,13 +18,26 @@ final class AddCheepToTimelineProjectionHandler
public function __invoke(AddCheepToTimelineProjection $message): void
{
$this->redis->lPush(
sprintf("timelines_%s", $message->authorId),
json_encode([
'cheep_id' => $message->cheepId,
'cheep_message' => $message->cheepMessage,
'cheep_date' => $message->cheepDate,
], JSON_THROW_ON_ERROR)
$this->getRedisKey($message),
$this->getRedisContent($message)
);
}
private function getRedisKey(AddCheepToTimelineProjection $message): string
{
return sprintf(
CreateFollowersCounterProjectionHandler::REDIS_KEY_TEMPLATE,
$message->authorId
);
}
private function getRedisContent(AddCheepToTimelineProjection $message): string
{
return json_encode([
'cheep_id' => $message->cheepId,
'cheep_message' => $message->cheepMessage,
'cheep_date' => $message->cheepDate,
], JSON_THROW_ON_ERROR);
}
}
//end-snippet