Making progress on working CODE for all the application

This commit is contained in:
Carlos Buenosvinos
2022-01-10 02:19:36 +01:00
parent 9b167cc49a
commit e185cc73a8
36 changed files with 1090 additions and 102 deletions

View File

@@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
namespace Cheeper\Chapter7\Infrastructure\Persistence;
use Cheeper\Chapter7\DomainModel\Follow\Follow;
use Cheeper\Chapter7\DomainModel\Follow\Follows;
use Cheeper\DomainModel\Author\AuthorId;
use Cheeper\DomainModel\Follow\FollowId;
use function Functional\head;
use function Functional\reduce_left;
use function Functional\select;
final class InMemoryFollows implements Follows
{
/** @var array<string, Follow> */
public array $collection = [];
public function ofId(FollowId $followId): ?Follow
{
$candidate = head(
select($this->collection, fn (Follow $u): bool => $u->followId()->equals($followId))
);
if (null === $candidate) {
return null;
}
return $candidate;
}
public function add(Follow $follow): void
{
$candidate = head(
select($this->collection, fn (Follow $u): bool => $u->fromAuthorId()->equals($follow->fromAuthorId()) && $u->toAuthorId()->equals($follow->toAuthorId()))
);
if ((null !== $candidate && $candidate != $follow) || null === $candidate) {
$this->collection[$follow->followId()->toString()] = $follow;
}
}
public function numberOfFollowersFor(AuthorId $authorId): int
{
return reduce_left(
$this->collection,
function(Follow $f, string $key, array $collection, int $initial) use($authorId): int {
return $initial + ($f->fromAuthorId()->equals($authorId) ? 1 : 0);
},
0
);
}
public function ofFromAuthorIdAndToAuthorId(AuthorId $fromAuthorId, AuthorId $toAuthorId): ?Follow
{
$candidate = head(
select($this->collection, fn (Follow $u): bool => $u->fromAuthorId()->equals($fromAuthorId) && $u->toAuthorId()->equals($toAuthorId))
);
return $candidate ?? null;
}
public function toAuthorId(AuthorId $authorId): array
{
return select($this->collection, fn (Follow $f): bool => $f->toAuthorId()->equals($authorId));
}
}