Skip to content

Commit 818ac15

Browse files
committed
chore!: drop support for entity name
1 parent df80573 commit 818ac15

File tree

3 files changed

+38
-43
lines changed

3 files changed

+38
-43
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,12 @@ use CalebDW\SqlEntities\Facades\SqlEntity;
226226
use CalebDW\SqlEntities\SqlEntityManager;
227227
use CalebDW\SqlEntities\View;
228228

229-
// Create a single entity by name, class, or instance
230-
SqlEntity::create('recent_orders_view');
229+
// Create a single entity by class or instance
230+
SqlEntity::create(RecentOrdersView::class);
231231
resolve(SqlEntityManager::class)->create(RecentOrdersView::class);
232232
resolve('sql-entities')->create(new RecentOrdersView());
233233

234-
// Similarly, you can drop a single entity using the name, class, or instance
234+
// Similarly, you can drop a single entity using the class or instance
235235
SqlEntity::drop(RecentOrdersView::class);
236236

237237
// Create or drop all entities

src/SqlEntityManager.php

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,55 +14,59 @@
1414
use Illuminate\Database\Connection;
1515
use Illuminate\Database\DatabaseManager;
1616
use Illuminate\Support\Collection;
17+
use Illuminate\Support\ItemNotFoundException;
1718
use InvalidArgumentException;
1819

1920
class SqlEntityManager
2021
{
22+
/** @var Collection<class-string<SqlEntity>, SqlEntity> */
23+
public readonly Collection $entities;
24+
2125
/**
2226
* The active grammar instances.
2327
*
2428
* @var array<string, Grammar>
2529
*/
2630
protected array $grammars = [];
2731

32+
/** @param Collection<int, SqlEntity> $entities */
2833
public function __construct(
29-
/** @var Collection<int, SqlEntity> */
30-
public readonly Collection $entities,
34+
Collection $entities,
3135
protected DatabaseManager $db,
3236
) {
37+
$this->entities = $entities
38+
->keyBy(fn ($entity) => $entity::class);
3339
}
3440

35-
/** @throws InvalidArgumentException if the entity is not found. */
36-
public function get(string $name, ?string $connection = null): SqlEntity
41+
/**
42+
* Get the entity by class.
43+
*
44+
* @param class-string<SqlEntity> $name
45+
* @throws ItemNotFoundException
46+
*/
47+
public function get(string $name): SqlEntity
3748
{
38-
$entity = $this->entities->firstWhere(
39-
fn (SqlEntity $e) => $e->name() === $name
40-
&& $e->connectionName() === $connection,
41-
);
49+
$entity = $this->entities->get($name);
4250

43-
throw_if(
44-
$entity === null,
45-
new InvalidArgumentException("Entity [{$name}] not found."),
46-
);
51+
if ($entity === null) {
52+
throw new ItemNotFoundException("Entity [{$name}] not found.");
53+
}
4754

4855
return $entity;
4956
}
5057

5158
/**
5259
* Create an entity.
5360
*
54-
* @param class-string<SqlEntity>|string|SqlEntity $entity The entity name, class, or instance.
55-
* @throws InvalidArgumentException if the entity is not found.
61+
* @param class-string<SqlEntity>|SqlEntity $entity
62+
* @throws ItemNotFoundException
5663
*/
5764
public function create(SqlEntity|string $entity): void
5865
{
5966
if (is_string($entity)) {
60-
$entity = class_exists($entity)
61-
? resolve($entity)
62-
: $this->get($entity);
67+
$entity = $this->get($entity);
6368
}
6469

65-
assert($entity instanceof SqlEntity);
6670
$connection = $this->connection($entity);
6771

6872
if (! $entity->creating($connection)) {
@@ -78,18 +82,15 @@ public function create(SqlEntity|string $entity): void
7882
/**
7983
* Drop an entity.
8084
*
81-
* @param class-string<SqlEntity>|string|SqlEntity $entity The entity name, class, or instance.
82-
* @throws InvalidArgumentException if the entity is not found.
85+
* @param class-string<SqlEntity>|SqlEntity $entity
86+
* @throws ItemNotFoundException
8387
*/
8488
public function drop(SqlEntity|string $entity): void
8589
{
8690
if (is_string($entity)) {
87-
$entity = class_exists($entity)
88-
? resolve($entity)
89-
: $this->get($entity);
91+
$entity = $this->get($entity);
9092
}
9193

92-
assert($entity instanceof SqlEntity);
9394
$connection = $this->connection($entity);
9495

9596
if (! $entity->dropping($connection)) {
@@ -144,9 +145,7 @@ protected function createGrammar(string $driver, Connection $connection): Gramma
144145
'pgsql' => new PostgresGrammar($connection),
145146
'sqlite' => new SQLiteGrammar($connection),
146147
'sqlsrv' => new SqlServerGrammar($connection),
147-
default => throw new InvalidArgumentException(
148-
"Unsupported driver [{$driver}].",
149-
),
148+
default => throw new InvalidArgumentException("Unsupported driver [{$driver}]."),
150149
};
151150
}
152151
}

tests/Feature/SqlEntityManagerTest.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use CalebDW\SqlEntities\View;
88
use Illuminate\Database\Connection;
99
use Illuminate\Database\DatabaseManager;
10+
use Illuminate\Support\ItemNotFoundException;
1011
use Workbench\Database\Entities\views\FooConnectionUserView;
1112
use Workbench\Database\Entities\views\UserView;
1213

@@ -39,21 +40,18 @@
3940
->not->toBeEmpty();
4041

4142
describe('get', function () {
42-
it('returns the entity by name', function () {
43-
$entity = test()->manager->get('user_view');
43+
it('returns the entity by class', function (string $class) {
44+
$entity = test()->manager->get($class);
4445

45-
expect($entity)->toBeInstanceOf(UserView::class);
46-
});
47-
48-
it('returns the entity by name and connection', function () {
49-
$entity = test()->manager->get('user_view', 'foo');
50-
51-
expect($entity)->toBeInstanceOf(FooConnectionUserView::class);
52-
});
46+
expect($entity)->toBeInstanceOf($class);
47+
})->with([
48+
UserView::class,
49+
FooConnectionUserView::class,
50+
]);;
5351

5452
it('throws an exception for unknown entity', function () {
5553
$entity = test()->manager->get('unknown');
56-
})->throws(InvalidArgumentException::class, 'Entity [unknown] not found.');
54+
})->throws(ItemNotFoundException::class, 'Entity [unknown] not found.');
5755
});
5856

5957
describe('create', function () {
@@ -66,7 +64,6 @@
6664

6765
test()->manager->create($entity);
6866
})->with('drivers')->with([
69-
'name' => 'user_view',
7067
'class' => UserView::class,
7168
'entity' => new UserView(),
7269
]);
@@ -93,7 +90,6 @@
9390

9491
test()->manager->drop($entity);
9592
})->with('drivers')->with([
96-
'name' => 'user_view',
9793
'class' => UserView::class,
9894
'entity' => new UserView(),
9995
]);

0 commit comments

Comments
 (0)