Skip to content

Commit

Permalink
feat(Database): Move getScopedQuery to AbstractQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
pionl committed Nov 28, 2022
1 parent 2bc0248 commit e062fe4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 29 deletions.
31 changes: 4 additions & 27 deletions src/Database/Queries/AbstractEloquentQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ protected function chunkWrite(array $items, int $columnsCount = 0): void
/**
* @param Scope[] $scopes
*
* @return Collection<int, TModel>
* @return Collection<int,TModel>
*/
protected function getAll(array $scopes = []): Collection
{
Expand Down Expand Up @@ -209,31 +209,8 @@ protected function forceDelete(array $scopes = []): int
protected function getQuery(array $scopes = []): Builder
{
$class = $this->getModelClass();

return $this->getScopedQuery($class::query(), $scopes);
}

/**
* @param Builder<TModel> $builder
* @param Scope[]|null[]|null $scopes
*
* @return Builder<TModel>
*/
protected function getScopedQuery(Builder $builder, ?array $scopes): Builder
{
if ($scopes === null) {
return $builder;
}

foreach ($scopes as $scope) {
if ($scope === null) {
continue;
}

// We are unable to reuse applyScope logic due protected methods (our scopes can contain withoutGlobalScope)
$scope->apply($builder, $builder->getModel());
}

return $builder;
/** @var Builder<TModel> $query */
$query = $class::query();
return $this->getScopedQuery($query, $scopes);
}
}
23 changes: 23 additions & 0 deletions src/Database/Queries/AbstractQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,29 @@

namespace LaraStrict\Database\Queries;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Scope;

abstract class AbstractQuery
{
/**
* @template TBuilderModel of \Illuminate\Database\Eloquent\Model
* @param Builder<TBuilderModel> $builder
* @param Scope[]|null[] $scopes
*
* @return Builder<TBuilderModel>
*/
protected function getScopedQuery(Builder $builder, array $scopes): Builder
{
foreach ($scopes as $scope) {
if ($scope === null) {
continue;
}

// We are unable to reuse applyScope logic due protected methods (our scopes can contain withoutGlobalScope)
$scope->apply($builder, $builder->getModel());
}

return $builder;
}
}
3 changes: 1 addition & 2 deletions tests/Feature/Database/Queries/TestScopeQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ class TestScopeQuery extends AbstractEloquentQuery
*/
public function execute(array $scopes): Collection
{
return $this->getQuery($scopes)
->get();
return $this->getAll($scopes);
}

protected function getModelClass(): string
Expand Down
2 changes: 2 additions & 0 deletions tests/Feature/Database/Scopes/TestSoftDeleteScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Tests\LaraStrict\Feature\Database\Scopes;

use Tests\LaraStrict\Feature\Database\Models\Test;
use Tests\LaraStrict\Feature\Database\Queries\TestScopeQuery;
use Tests\LaraStrict\Feature\DatabaseTestCase;

Expand All @@ -20,6 +21,7 @@ public function testSoftDeletingGlobalScope(): void
$this->assertCount(5, $results);

foreach ($results as $result) {
$this->assertInstanceOf(Test::class, $result);
$this->assertNull($result->deleted_at);
}
}
Expand Down

0 comments on commit e062fe4

Please sign in to comment.