Skip to content

Commit

Permalink
Add findOrThrow method to article repository
Browse files Browse the repository at this point in the history
  • Loading branch information
LVoogd committed Feb 18, 2024
1 parent 8a70591 commit a903928
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src-dev/Feed/Repository/InMemoryArticleRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Feed\Domain\Article\Article;
use App\Feed\Domain\Article\ArticleId;
use App\Feed\Domain\Article\ArticleRepository;
use App\Feed\Domain\Article\Exception\ArticleNotFoundException;
use App\Feed\Domain\Article\Url\Url;

final class InMemoryArticleRepository implements ArticleRepository
Expand All @@ -26,6 +27,12 @@ public function find(ArticleId $id): ?Article
return $this->entities[(string) $id] ?? null;
}


public function findOrThrow(ArticleId $id): Article
{
return $this->find($id) ?? throw ArticleNotFoundException::withArticleId($id);
}

public function count(): int
{
return count($this->entities);
Expand Down
6 changes: 6 additions & 0 deletions src/Feed/Domain/Article/ArticleRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Feed\Domain\Article;

use App\Feed\Domain\Article\Exception\ArticleNotFoundException;
use App\Feed\Domain\Article\Url\Url;

interface ArticleRepository
Expand All @@ -10,6 +11,11 @@ public function save(Article ...$articles): void;

public function find(ArticleId $id): ?Article;

/**
* @throws ArticleNotFoundException
*/
public function findOrThrow(ArticleId $id): Article;

public function count(): int;

/**
Expand Down
13 changes: 13 additions & 0 deletions src/Feed/Domain/Article/Exception/ArticleNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Feed\Domain\Article\Exception;

use App\Feed\Domain\Article\ArticleId;

final class ArticleNotFoundException extends \Exception
{
public static function withArticleId(ArticleId $articleId): self
{
return new self(sprintf('Article with id %s not found.', (string) $articleId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Feed\Domain\Article\Article;
use App\Feed\Domain\Article\ArticleId;
use App\Feed\Domain\Article\ArticleRepository;
use App\Feed\Domain\Article\Exception\ArticleNotFoundException;
use Doctrine\Common\Collections\Criteria;
use Doctrine\Persistence\ManagerRegistry;
use Webmozart\Assert\Assert;
Expand All @@ -30,6 +31,11 @@ public function find(ArticleId $id): ?Article
return $this->findWithoutLocking($id);
}

public function findOrThrow(ArticleId $id): Article
{
return $this->find($id) ?? throw ArticleNotFoundException::withArticleId($id);
}

public function count(): int
{
$count = $this->createQueryBuilder('a')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Functional\Feed\Infrastructure\Persistence\Doctrine\Article;

use App\Feed\Domain\Article\ArticleId;
use App\Feed\Domain\Article\Exception\ArticleNotFoundException;
use App\Feed\Infrastructure\Persistence\Doctrine\Article\DoctrineArticleRepository;
use DateTime;
use Dev\Feed\Factory\ArticleFactory;
Expand Down Expand Up @@ -102,4 +104,16 @@ public function it_should_count_the_articles(): void
// Assert
self::assertSame(3, $count);
}

/**
* @test
*/
public function it_should_throw_when_trying_to_find_a_non_existing_article_id(): void
{
// Assert
self::expectException(ArticleNotFoundException::class);

// Act
$this->repository->findOrThrow(new ArticleId('ddb01803-ef13-4195-bed1-3320a6b443ba'));
}
}

0 comments on commit a903928

Please sign in to comment.