Skip to content

Commit

Permalink
Create read model for article
Browse files Browse the repository at this point in the history
We are leaking domain logic to the infrastructure layer by allowing
a query to return the Article domain entity directly.

In this commit we introduce a read model that can be exposed by the
query handler instead. This fixes the domain leaking into the
infrastructure layer.
  • Loading branch information
LVoogd committed Feb 4, 2024
1 parent 31c4ad8 commit 6581c8f
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
namespace App\Common\Infrastructure\Client\Graphql\Scalar;

use DateTime;
use DateTimeImmutable;
use GraphQL\Language\AST\StringValueNode;
use Overblog\GraphQLBundle\Annotation as GraphQL;

#[GraphQL\Scalar(name: "DateTime")]
final readonly class DateTimeType
{
public static function serialize(DateTime $value): string
public static function serialize(DateTime|DateTimeImmutable $value): string
{
return $value->format('Y-m-d H:i:s');
}
Expand Down
30 changes: 30 additions & 0 deletions src/Feed/Application/Model/Article/ArticleReadModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Feed\Application\Model\Article;

use App\Feed\Application\Model\Source\SourceReadModel;
use App\Feed\Domain\Article\Article;
use DateTimeImmutable;

final readonly class ArticleReadModel
{
private function __construct(
public string $title,
public string $summary,
public string $url,
public SourceReadModel $source,
public DateTimeImmutable $updated,
) {
}

public static function fromArticle(Article $article): self
{
return new self(
$article->getTitle(),
$article->getSummary(),
(string) $article->getUrl(),
SourceReadModel::fromSource($article->getSource()),
DateTimeImmutable::createFromMutable($article->getUpdated()),
);
}
}
22 changes: 22 additions & 0 deletions src/Feed/Application/Model/Source/SourceReadModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Feed\Application\Model\Source;

use App\Feed\Domain\Source\Source;

final readonly class SourceReadModel
{
private function __construct(
public string $id,
public string $name,
) {
}

public static function fromSource(Source $source): self
{
return new self(
$source->getId(),
$source->getName(),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Feed\Application\Query\Article\Handler;

use App\Feed\Application\Model\Article\ArticleReadModel;
use App\Feed\Application\Query\Article\LatestUpdatedArticlesQuery;
use App\Feed\Domain\Article\Article;
use App\Feed\Domain\Article\ArticleRepository;
Expand All @@ -14,10 +15,13 @@ public function __construct(
}

/**
* @return list<Article>
* @return list<ArticleReadModel>
*/
public function __invoke(LatestUpdatedArticlesQuery $query): array
{
return $this->articleRepository->findLatest($query->offset, $query->numberOfArticles);
return array_map(
ArticleReadModel::fromArticle(...),
$this->articleRepository->findLatest($query->offset, $query->numberOfArticles),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __invoke(int $first, ?string $after): Connection|Promise
$articleTypes = [];

foreach ($articles as $article) {
$articleTypes[] = ArticleType::createFromArticle($article);
$articleTypes[] = ArticleType::createFromArticleReadModel($article);
}

return $articleTypes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace App\Feed\Infrastructure\Client\Graphql\Type\Article;

use App\Common\Infrastructure\Client\Graphql\Scalar\DateTimeType;
use App\Feed\Domain\Article\Article;
use App\Feed\Application\Model\Article\ArticleReadModel;
use App\Feed\Infrastructure\Client\Graphql\Type\Source\SourceType;
use DateTime;
use DateTimeImmutable;
use Overblog\GraphQLBundle\Annotation as GraphQL;

#[GraphQL\Type(name: 'Article')]
Expand All @@ -21,18 +21,18 @@ public function __construct(
#[GraphQL\Field]
public SourceType $source,
#[GraphQl\Field(type: "DateTime")]
public DateTime $updated,
public DateTimeImmutable $updated,
) {
}

public static function createFromArticle(Article $article): self
public static function createFromArticleReadModel(ArticleReadModel $article): self
{
return new self(
$article->getTitle(),
$article->getSummary(),
(string) $article->getUrl(),
SourceType::createFromSource($article->getSource()),
$article->getUpdated(),
$article->title,
$article->summary,
$article->url,
SourceType::createFromSourceReadModel($article->source),
$article->updated,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Feed\Infrastructure\Client\Graphql\Type\Source;

use App\Feed\Application\Model\Source\SourceReadModel;
use App\Feed\Domain\Source\Source;
use Overblog\GraphQLBundle\Annotation as GraphQL;

Expand All @@ -16,11 +17,11 @@ public function __construct(
) {
}

public static function createFromSource(Source $source): self
public static function createFromSourceReadModel(SourceReadModel $source): self
{
return new self(
$source->getId(),
$source->getName(),
$source->id,
$source->name,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Unit\Feed\Application\Query\Article\Handler;

use App\Feed\Application\Model\Article\ArticleReadModel;
use App\Feed\Application\Query\Article\Handler\LatestUpdatedArticlesHandler;
use App\Feed\Application\Query\Article\LatestUpdatedArticlesQuery;
use App\Feed\Domain\Article\ArticleRepository;
Expand Down Expand Up @@ -46,9 +47,9 @@ public function it_should_retrieve_last_n_articles(): void
// Assert
self::assertCount(3, $articles);

self::assertSame($articles[0], $article5);
self::assertSame($articles[1], $article3);
self::assertSame($articles[2], $article1);
self::assertEquals($articles[0], ArticleReadModel::fromArticle($article5));
self::assertEquals($articles[1], ArticleReadModel::fromArticle($article3));
self::assertEquals($articles[2], ArticleReadModel::fromArticle($article1));
}

/**
Expand All @@ -74,9 +75,9 @@ public function it_should_return_results_after_given_offset(): void
// Assert
self::assertCount(3, $articles);

self::assertSame($articles[0], $article6);
self::assertSame($articles[1], $article2);
self::assertSame($articles[2], $article4);
self::assertEquals($articles[0], ArticleReadModel::fromArticle($article6));
self::assertEquals($articles[1], ArticleReadModel::fromArticle($article2));
self::assertEquals($articles[2], ArticleReadModel::fromArticle($article4));
}

/**
Expand Down Expand Up @@ -114,8 +115,8 @@ public function it_should_return_subset_if_asked_articles_expands_available(): v
// Assert
self::assertCount(3, $articles);

self::assertSame($articles[0], $article3);
self::assertSame($articles[1], $article1);
self::assertSame($articles[2], $article2);
self::assertEquals($articles[0], ArticleReadModel::fromArticle($article3));
self::assertEquals($articles[1], ArticleReadModel::fromArticle($article1));
self::assertEquals($articles[2], ArticleReadModel::fromArticle($article2));
}
}

0 comments on commit 6581c8f

Please sign in to comment.