Skip to content

Commit

Permalink
Read layer usage in Write layer (example)
Browse files Browse the repository at this point in the history
  • Loading branch information
theUniC committed Mar 16, 2024
1 parent bf2e941 commit 649269f
Show file tree
Hide file tree
Showing 14 changed files with 197 additions and 11 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "project",
"license": "proprietary",
"require": {
"php": ">=8.1",
"php": ">=8.2",
"ext-ctype": "*",
"ext-iconv": "*",
"ext-json": "*",
Expand Down
4 changes: 4 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ services:
Symfony\Component\Messenger\MessageBusInterface $projectionBus: "@projection.bus"

Redis: '@snc_redis.default'
string $filePath: "%kernel.project_dir%/data/authors-with-more-than-thousand-followers.txt"

# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
Expand Down Expand Up @@ -186,4 +187,7 @@ services:
- name: "doctrine.orm.entity_listener"
event: "postPersist"
entity: 'App\Entity\User'

Cheeper\Chapter9\DomainModel\Author\AuthorsWithMoreThanAThousandOfFollowers:
class: 'Cheeper\Chapter9\Infrastructure\DomainModel\Author\TextFileAuthorsWithMoreThanAThousandOfFollowers'
#end-ignore
2 changes: 2 additions & 0 deletions data/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Cheeper\Chapter9\Application\Author\Command;

final readonly class UpgradeAuthorsWithMoreThanAThousandOfFollowersCommand
{
public function __construct()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Cheeper\Chapter9\Application\Author\Command;

use Cheeper\Chapter9\DomainModel\Author\AuthorRepository;
use Cheeper\Chapter9\DomainModel\Author\AuthorsWithMoreThanAThousandOfFollowers;

final readonly class UpgradeAuthorsWithMoreThanAThousandOfFollowersCommandHandler
{
public function __construct(
private AuthorRepository $authors,
private AuthorsWithMoreThanAThousandOfFollowers $authorsWithMoreThanAThousandOfFollowers
) {
}

public function __invoke(UpgradeAuthorsWithMoreThanAThousandOfFollowersCommand $command): void
{
$authorsWithMoreThanAThousandOfFollowers = $this->authorsWithMoreThanAThousandOfFollowers;
$authorIds = $authorsWithMoreThanAThousandOfFollowers();

foreach ($authorIds as $authorId) {
$author = $this->authors->ofId($authorId);
$author->upgrade();
$this->authors->save($author);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);


namespace Cheeper\Chapter9\Application\Author\Event;

use Cheeper\AllChapters\DomainModel\Author\AuthorId;
use Cheeper\Chapter7\Application\Author\Projection\IncrementCountFollowersProjection;
use Cheeper\Chapter7\Application\ProjectionBus;
use Cheeper\Chapter8\DomainModel\Follow\AuthorFollowed;
use Cheeper\Chapter8\DomainModel\Follow\FollowRepository;

final readonly class WhenAuthorFollowedThenListAuthorsWithMoreThanThousandFollowersEventHandler
{
public function __construct(
private ProjectionBus $projectionBus,
private FollowRepository $followRepository
) {
}

public function __invoke(AuthorFollowed $event): void
{
if ($this->followRepository->numberOfFollowersFor(AuthorId::fromString($event->toAuthorId())) >= 1000) {
$this->projectionBus->project(
IncrementCountFollowersProjection::ofAuthor(
$event->toAuthorId()
)
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);


namespace Cheeper\Chapter9\Application\Author\Projection;

final readonly class AddAuthorToListOfAuthorsWithMoreThanThousandFollowersProjection
{
public function __construct(
public string $authorId
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);


namespace Cheeper\Chapter9\Application\Author\Projection;

final readonly class AddAuthorToListOfAuthorsWithMoreThanThousandFollowersProjectionHandler
{
public function __construct(
private string $filePath
) {
}

public function __invoke(AddAuthorToListOfAuthorsWithMoreThanThousandFollowersProjection $projection): void
{
file_put_contents($this->filePath, $projection->authorId . PHP_EOL, FILE_APPEND);
}
}
34 changes: 26 additions & 8 deletions src/Cheeper/Chapter9/DomainModel/Author/Author.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ final class Author
{
use EventSourcedTrait;

private string $authorId;
private string $userName;
private string $email;
private ?string $name = null;
private ?string $biography = null;
private ?string $location = null;
private ?string $website = null;
private ?DateTimeImmutable $birthDate = null;
private string $authorId;
private string $userName;
private string $email;
private ?string $name = null;
private ?string $biography = null;
private ?string $location = null;
private ?string $website = null;
private ?DateTimeImmutable $birthDate = null;
private bool $upgraded = false;

private function __construct()
{
Expand Down Expand Up @@ -119,6 +120,13 @@ protected function applyThat(DomainEvent $event): void
$this->$modifier($event);
}

public function upgrade(): void
{
$this->recordApplyAndPublishThat(
new AuthorWasUpgraded($this->authorId)
);
}

protected function applyNewAuthorSigned(NewAuthorSigned $event)
{
$this->authorId = $event->authorId();
Expand All @@ -136,6 +144,11 @@ protected function applyAuthorEmailChangedSigned(AuthorEmailChanged $event)
$this->email = $event->authorEmail();
}

private function applyAuthorWasUpgraded(): void
{
$this->upgraded = true;
}

public function authorId(): AuthorId
{
return AuthorId::fromString($this->authorId);
Expand Down Expand Up @@ -176,6 +189,11 @@ public function birthDate(): ?BirthDate
return $this->birthDate !== null ? BirthDate::fromString($this->birthDate->format('Y-m-d')) : null;
}

public function upgraded(): bool
{
return $this->upgraded;
}

//...
}
// end-snippet
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
interface AuthorRepository
{
public function ofId(AuthorId $authorId): ?Author;
public function add(Author $author): void;
public function save(Author $author): void;
}
//end-snippet
19 changes: 19 additions & 0 deletions src/Cheeper/Chapter9/DomainModel/Author/AuthorWasUpgraded.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);


namespace Cheeper\Chapter9\DomainModel\Author;

use Cheeper\Chapter7\Application\MessageTrait;
use Cheeper\Chapter7\DomainModel\DomainEvent;

final readonly class AuthorWasUpgraded implements DomainEvent
{
use MessageTrait;

public function __construct(
public string $authorId
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Cheeper\Chapter9\DomainModel\Author;

use Cheeper\AllChapters\DomainModel\Author\AuthorId;

interface AuthorsWithMoreThanAThousandOfFollowers
{
/** @return AuthorId[] */
public function __invoke(): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function ofUserName(UserName $userName): ?Author
}
*/

public function add(Author $author): void
public function save(Author $author): void
{
$this->eventStore->append(
new EventStream(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Cheeper\Chapter9\Infrastructure\DomainModel\Author;

use Cheeper\AllChapters\DomainModel\Author\AuthorId;
use Cheeper\Chapter9\DomainModel\Author\AuthorsWithMoreThanAThousandOfFollowers;

final readonly class TextFileAuthorsWithMoreThanAThousandOfFollowers implements AuthorsWithMoreThanAThousandOfFollowers
{
public function __construct(
private string $filePath
) {
}

public function __invoke(): array
{
return array_map(
fn(string $authorId) => AuthorId::fromString($authorId),
file($this->filePath)
);
}
}

0 comments on commit 649269f

Please sign in to comment.