Skip to content

Commit

Permalink
Records passed to the RecordIdentifier must only implement the getKey…
Browse files Browse the repository at this point in the history
… method
  • Loading branch information
tonysm committed Jun 23, 2021
1 parent 03032b2 commit 05c35c0
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 50 deletions.
24 changes: 4 additions & 20 deletions src/Views/RecordIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ class RecordIdentifier
const NEW_PREFIX = "create";
const DELIMITER = "_";

/** @var Model|TurboStreamable */
/** @var Model */
private $record;

public function __construct(object $record)
{
throw_if(
! $this->isStreamable($record),
sprintf('[%s] must be an instance of Eloquent or TurboStreamable.', get_class($record))
! method_exists($record, 'getKey'),
UnidentifiableRecordException::missingGetKeyMethod($record),
);

$this->record = $record;
}

public function domId(?string $prefix = null): string
{
if ($recordId = $this->streamableKey()) {
if ($recordId = $this->record->getKey()) {
return sprintf('%s%s%s', $this->domClass($prefix), self::DELIMITER, $recordId);
}

Expand All @@ -39,20 +39,4 @@ public function domClass(?string $prefix = null): string

return trim("{$prefix}{$delimiter}{$singular}", $delimiter);
}

protected function streamableKey(): ?string
{
if ($this->record instanceof Model) {
return $this->record->getKey();
}

if ($this->record instanceof TurboStreamable) {
return $this->record->getDomId();
}
}

protected function isStreamable($record): bool
{
return $record instanceof Model || $record instanceof TurboStreamable;
}
}
8 changes: 0 additions & 8 deletions src/Views/TurboStreamable.php

This file was deleted.

15 changes: 15 additions & 0 deletions src/Views/UnidentifiableRecordException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Tonysm\TurboLaravel\Views;

use RuntimeException;

class UnidentifiableRecordException extends RuntimeException
{
public static function missingGetKeyMethod(object $model): self
{
return new self(
sprintf('[%s] must implement a getKey() method.', get_class($model))
);
}
}
6 changes: 2 additions & 4 deletions tests/Stubs/Models/TestTurboStreamable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

namespace Tonysm\TurboLaravel\Tests\Stubs\Models;

use Tonysm\TurboLaravel\Views\TurboStreamable;

class TestTurboStreamable implements TurboStreamable
class TestTurboStreamable
{
public function getDomId()
public function getKey()
{
return 'turbo-dom-id';
}
Expand Down
13 changes: 0 additions & 13 deletions tests/TestTurboStreamable.php

This file was deleted.

9 changes: 4 additions & 5 deletions tests/Views/RecordIdentifierStreamableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Tonysm\TurboLaravel\Tests\Views;

use RuntimeException;
use stdClass;
use Tonysm\TurboLaravel\Tests\Stubs\Models\TestTurboStreamable;
use Tonysm\TurboLaravel\Tests\TestCase;
use Tonysm\TurboLaravel\Tests\TestTurboStreamable;
use Tonysm\TurboLaravel\Views\RecordIdentifier;
use Tonysm\TurboLaravel\Views\UnidentifiableRecordException;

class RecordIdentifierStreamableTest extends TestCase
{
Expand All @@ -32,12 +32,11 @@ public function dom_id_of_streamable_with_custom_prefix()
{
$this->assertEquals("custom_prefix_{$this->singular}_turbo-dom-id", (new RecordIdentifier($this->streamable))->domId("custom_prefix"));
}

/** @test */
public function exception_is_thrown_when_given_non_streamable_instance()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('[stdClass] must be an instance of Eloquent or TurboStreamable.');
$this->expectException(UnidentifiableRecordException::class);

new RecordIdentifier(new stdClass);
}
Expand Down

0 comments on commit 05c35c0

Please sign in to comment.