Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x] Implement find or fail on EntryRepository #9506

Merged
merged 14 commits into from
Feb 20, 2024
2 changes: 2 additions & 0 deletions src/Contracts/Entries/EntryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public function whereInCollection(array $handles);

public function find($id);

public function findOrFail(string|int $id);
duncanmcclean marked this conversation as resolved.
Show resolved Hide resolved

public function findByUri(string $uri);

public function make();
Expand Down
15 changes: 15 additions & 0 deletions src/Exceptions/EntryNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Statamic\Exceptions;

class EntryNotFoundException extends \Exception
{
protected string|int $id;

public function setEntry(string|int $id): object
duncanmcclean marked this conversation as resolved.
Show resolved Hide resolved
{
$this->message = "No entry results for id {$id}";

return $this;
}
}
12 changes: 12 additions & 0 deletions src/Stache/Repositories/EntryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Statamic\Contracts\Entries\EntryRepository as RepositoryContract;
use Statamic\Contracts\Entries\QueryBuilder;
use Statamic\Entries\EntryCollection;
use Statamic\Exceptions\EntryNotFoundException;
use Statamic\Stache\Query\EntryQueryBuilder;
use Statamic\Stache\Stache;
use Statamic\Support\Arr;
Expand Down Expand Up @@ -43,6 +44,17 @@ public function find($id): ?Entry
return $this->query()->where('id', $id)->first();
}

public function findOrFail($id): Entry|EntryNotFoundException
duncanmcclean marked this conversation as resolved.
Show resolved Hide resolved
{
$result = $this->query()->where('id', $id)->first();

if (is_null($result)) {
duncanmcclean marked this conversation as resolved.
Show resolved Hide resolved
throw (new EntryNotFoundException)->setEntry($id);
}

return $result;
}

public function findByUri(string $uri, ?string $site = null): ?Entry
{
$site = $site ?? $this->stache->sites()->first();
Expand Down
22 changes: 22 additions & 0 deletions tests/Stache/Repositories/EntryRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Statamic\Contracts\Entries\Entry;
use Statamic\Entries\EntryCollection;
use Statamic\Exceptions\EntryNotFoundException;
use Statamic\Facades\Collection;
use Statamic\Facades\Entry as EntryAPI;
use Statamic\Stache\Repositories\EntryRepository;
Expand Down Expand Up @@ -227,4 +228,25 @@ public function it_can_delete()
$this->assertNull($item = $this->repo->find('test-blog-entry'));
$this->assertFileDoesNotExist($path);
}

/** @test */
public function it_throws_an_error_when_find_or_fail_fails()
{
$this->expectException(EntryNotFoundException::class);

$invalidStringId = '00000000-0000-0000-0000-000000000000';
$invalidIntId = 123;

$this->repo->findOrFail($invalidStringId);
$this->repo->findOrFail($invalidIntId);
}

/** @test */
public function it_finds_an_entry_when_find_or_fail_is_queried()
{
$entry = $this->repo->findOrFail('alphabetical-bravo');

$this->assertInstanceOf(Entry::class, $entry);
$this->assertEquals('Bravo', $entry->get('title'));
}
}
Loading