-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Ryan Mitchell <[email protected]> Co-authored-by: Jason Varga <[email protected]>
- Loading branch information
1 parent
b3b2d76
commit 0a08a75
Showing
7 changed files
with
207 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
database/migrations/2024_03_07_100000_create_tokens_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
use Statamic\Eloquent\Database\BaseMigration as Migration; | ||
|
||
return new class extends Migration | ||
{ | ||
public function up() | ||
{ | ||
Schema::create($this->prefix('tokens'), function (Blueprint $table) { | ||
$table->id(); | ||
$table->string('token')->unique(); | ||
$table->string('handler'); | ||
$table->jsonb('data'); | ||
$table->timestamp('expire_at'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
public function down() | ||
{ | ||
Schema::dropIfExists($this->prefix('tokens')); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace Statamic\Eloquent\Tokens; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Statamic\Contracts\Tokens\Token as Contract; | ||
use Statamic\Tokens\Token as AbstractToken; | ||
|
||
class Token extends AbstractToken | ||
{ | ||
protected $model; | ||
|
||
public static function fromModel(Model $model) | ||
{ | ||
return (new static($model->token, $model->handler, $model->data)) | ||
->expireAt($model->expire_at) | ||
->model($model); | ||
} | ||
|
||
public function toModel() | ||
{ | ||
return self::makeModelFromContract($this); | ||
} | ||
|
||
public static function makeModelFromContract(Contract $source) | ||
{ | ||
$class = app('statamic.eloquent.tokens.model'); | ||
|
||
return $class::firstOrNew(['token' => $source->token()])->fill([ | ||
'handler' => $source->handler(), | ||
'data' => $source->data(), | ||
'expire_at' => $source->expiry(), | ||
]); | ||
} | ||
|
||
public function model($model = null) | ||
{ | ||
if (func_num_args() === 0) { | ||
return $this->model; | ||
} | ||
|
||
$this->model = $model; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Statamic\Eloquent\Tokens; | ||
|
||
use Statamic\Eloquent\Database\BaseModel; | ||
|
||
class TokenModel extends BaseModel | ||
{ | ||
protected $guarded = []; | ||
|
||
protected $table = 'tokens'; | ||
|
||
protected $casts = [ | ||
'data' => 'json', | ||
'expire_at' => 'datetime', | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Statamic\Eloquent\Tokens; | ||
|
||
use Statamic\Contracts\Tokens\Token as TokenContract; | ||
use Statamic\Tokens\TokenRepository as Repository; | ||
|
||
class TokenRepository extends Repository | ||
{ | ||
public function find(string $token): ?Token | ||
{ | ||
$model = app('statamic.eloquent.tokens.model')::whereToken($token)->first(); | ||
|
||
if (! $model) { | ||
return null; | ||
} | ||
|
||
return Token::fromModel($model); | ||
} | ||
|
||
public function save(TokenContract $token): bool | ||
{ | ||
return $token->toModel()->save(); | ||
} | ||
|
||
public function delete(TokenContract $token): bool | ||
{ | ||
return $token->toModel()->delete(); | ||
} | ||
|
||
public function collectGarbage(): void | ||
{ | ||
app('statamic.eloquent.tokens.model')::where('expire_at', '<', now())->delete(); | ||
} | ||
|
||
public static function bindings(): array | ||
{ | ||
return [ | ||
TokenContract::class => Token::class, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Tests\Repositories; | ||
|
||
use Carbon\Carbon; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Statamic\Contracts\Tokens\Token; | ||
use Statamic\Eloquent\Tokens\TokenRepository; | ||
use Tests\TestCase; | ||
|
||
class TokenRepositoryTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->repo = new TokenRepository(); | ||
|
||
$this->repo->make('abc', 'ExampleHandler', ['foo' => 'bar'])->save(); | ||
} | ||
|
||
/** @test */ | ||
public function it_gets_a_token() | ||
{ | ||
tap($this->repo->find('abc'), function ($token) { | ||
$this->assertInstanceOf(Token::class, $token); | ||
$this->assertEquals('abc', $token->token()); | ||
$this->assertEquals('ExampleHandler', $token->handler()); | ||
$this->assertEquals(['foo' => 'bar'], $token->data()->all()); | ||
$this->assertInstanceOf(Carbon::class, $token->expiry()); | ||
}); | ||
|
||
$this->assertNull($this->repo->find('unknown')); | ||
} | ||
|
||
/** @test */ | ||
public function it_saves_a_token_to_the_database() | ||
{ | ||
$token = $this->repo->make('new', 'ExampleHandler', ['foo' => 'bar']); | ||
|
||
$this->assertNull($this->repo->find('new')); | ||
|
||
$this->repo->save($token); | ||
|
||
$this->assertNotNull($item = $this->repo->find('new')); | ||
$this->assertEquals(['foo' => 'bar'], $item->data()->all()); | ||
} | ||
} |