Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Tokens/TokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@

namespace Statamic\Eloquent\Tokens;

use Illuminate\Support\Collection;
use Statamic\Contracts\Tokens\Token as TokenContract;
use Statamic\Tokens\TokenRepository as Repository;

class TokenRepository extends Repository
{
public function all(): Collection
{
return app('statamic.eloquent.tokens.model')::query()
->where('expire_at', '>', now())
->get()
->map(fn ($model) => Token::fromModel($model))
->values();
}

public function find(string $token): ?Token
{
$model = app('statamic.eloquent.tokens.model')::whereToken($token)->first();
Expand Down
12 changes: 12 additions & 0 deletions tests/Repositories/TokenRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ public function it_gets_a_token()
$this->assertNull($this->repo->find('unknown'));
}

#[Test]
public function it_gets_all_non_expired_tokens()
{
$this->repo->make('expired', 'ExampleHandler')->expireAt(now()->subMinute())->save();
$this->repo->make('fresh', 'ExampleHandler')->expireAt(now()->addHour())->save();

$this->assertEquals(
['abc', 'fresh'],
$this->repo->all()->map->token()->sort()->values()->all()
);
}

#[Test]
public function it_saves_a_token_to_the_database()
{
Expand Down
Loading