refactor(datatable): require column listing resolver dependency in DataTableService
This commit is contained in:
parent
2c2a0502d6
commit
0411e1d73b
10 changed files with 87 additions and 19 deletions
|
|
@ -8,12 +8,17 @@ use App\Contracts\Executor;
|
|||
use App\Lib\PrivateS3Disk;
|
||||
use App\Lib\PublicS3Disk;
|
||||
use App\Services\ActionExecutorService;
|
||||
use App\Services\DataTableService;
|
||||
use App\Services\IcsGeneratorService;
|
||||
use Carbon\CarbonInterface;
|
||||
use Illuminate\Contracts\Foundation\Application;
|
||||
use Illuminate\Contracts\Session\Session;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Illuminate\Support\Facades\Date;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Facades\URL;
|
||||
use Illuminate\Support\Facades\Vite;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
|
@ -64,6 +69,15 @@ class AppServiceProvider extends ServiceProvider
|
|||
Config::string('app.name', 'La Boutique de Cocotte'),
|
||||
)
|
||||
);
|
||||
|
||||
$this->app->bind(
|
||||
DataTableService::class,
|
||||
fn (Application $app): DataTableService => new DataTableService(
|
||||
request: $app->make(Request::class),
|
||||
session: $app->make(Session::class),
|
||||
columnListingResolver: static fn (string $table): array => Schema::getColumnListing($table),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@ use Illuminate\Contracts\Database\Query\Builder;
|
|||
use Illuminate\Contracts\Session\Session;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Shared\Enums\HasBaseEnum;
|
||||
use Shared\Pagination\Pagination;
|
||||
|
||||
|
|
@ -25,12 +24,12 @@ class DataTableService
|
|||
private const string CACHE_SORT_WAY_KEY = 'sort_way';
|
||||
|
||||
/**
|
||||
* @param (\Closure(string): array<int, string>)|null $columnListingResolver
|
||||
* @param \Closure(string): array<int, string> $columnListingResolver
|
||||
*/
|
||||
public function __construct(
|
||||
protected readonly Request $request,
|
||||
protected readonly Session $session,
|
||||
protected readonly ?\Closure $columnListingResolver = null,
|
||||
protected readonly \Closure $columnListingResolver,
|
||||
) {
|
||||
}
|
||||
|
||||
|
|
@ -86,9 +85,7 @@ class DataTableService
|
|||
private function hasTableColumn(string $table, string $column): bool
|
||||
{
|
||||
if (! isset(self::$columnsCache[$table])) {
|
||||
self::$columnsCache[$table] = $this->columnListingResolver !== null
|
||||
? ($this->columnListingResolver)($table)
|
||||
: Schema::getColumnListing($table);
|
||||
self::$columnsCache[$table] = ($this->columnListingResolver)($table);
|
||||
}
|
||||
|
||||
return in_array($column, self::$columnsCache[$table], true);
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ use Domain\Models\ValueObjects\DiscountValue;
|
|||
use Illuminate\Http\Request;
|
||||
use Illuminate\Session\NullSessionHandler;
|
||||
use Illuminate\Session\Store;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Shared\Pagination\PageNumber;
|
||||
use Shared\Pagination\PageSize;
|
||||
use Shared\Pagination\Pagination;
|
||||
|
|
@ -38,7 +39,13 @@ describe('GetPaginatedDiscountsQuery', function (): void {
|
|||
$discount2->is_old_price_displayed = false;
|
||||
$discount2->save();
|
||||
|
||||
$query = new GetPaginatedDiscountsQuery(new DataTableService(Request::create('/'), new Store('test', new NullSessionHandler())));
|
||||
$query = new GetPaginatedDiscountsQuery(
|
||||
new DataTableService(
|
||||
Request::create('/'),
|
||||
new Store('test', new NullSessionHandler()),
|
||||
static fn (string $table): array => Schema::getColumnListing($table),
|
||||
),
|
||||
);
|
||||
$input = new GetPaginatedDiscountsQueryInput(
|
||||
pagination: Pagination::ofPage(new PageNumber(1), new PageSize(10)),
|
||||
);
|
||||
|
|
@ -55,7 +62,13 @@ describe('GetPaginatedDiscountsQuery', function (): void {
|
|||
|
||||
it('throws when an invalid input is passed', function (): void {
|
||||
// GIVEN
|
||||
$query = new GetPaginatedDiscountsQuery(new DataTableService(Request::create('/'), new Store('test', new NullSessionHandler())));
|
||||
$query = new GetPaginatedDiscountsQuery(
|
||||
new DataTableService(
|
||||
Request::create('/'),
|
||||
new Store('test', new NullSessionHandler()),
|
||||
static fn (string $table): array => Schema::getColumnListing($table),
|
||||
),
|
||||
);
|
||||
$invalidInput = new class () implements QueryInput
|
||||
{
|
||||
};
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ use App\Services\DataTableService;
|
|||
use Illuminate\Http\Request;
|
||||
use Illuminate\Session\NullSessionHandler;
|
||||
use Illuminate\Session\Store;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Shared\Pagination\PageNumber;
|
||||
use Shared\Pagination\PageSize;
|
||||
use Shared\Pagination\Pagination;
|
||||
|
|
@ -31,7 +32,13 @@ describe('GetPaginatedFaqsQuery', function (): void {
|
|||
$faq2->answer = 'Second answer.';
|
||||
$faq2->save();
|
||||
|
||||
$query = new GetPaginatedFaqsQuery(new DataTableService(Request::create('/'), new Store('test', new NullSessionHandler())));
|
||||
$query = new GetPaginatedFaqsQuery(
|
||||
new DataTableService(
|
||||
Request::create('/'),
|
||||
new Store('test', new NullSessionHandler()),
|
||||
static fn (string $table): array => Schema::getColumnListing($table),
|
||||
),
|
||||
);
|
||||
$input = new GetPaginatedFaqsQueryInput(
|
||||
pagination: Pagination::ofPage(new PageNumber(1), new PageSize(10)),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ use Illuminate\Foundation\Testing\RefreshDatabase;
|
|||
use Illuminate\Http\Request;
|
||||
use Illuminate\Session\NullSessionHandler;
|
||||
use Illuminate\Session\Store;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Shared\Pagination\PageNumber;
|
||||
use Shared\Pagination\PageSize;
|
||||
use Shared\Pagination\Pagination;
|
||||
|
|
@ -27,7 +28,11 @@ describe('GetPaginatedPickupExceptionsQuery', function () {
|
|||
// GIVEN
|
||||
PickupException::factory()->count(3)->create();
|
||||
|
||||
$dataTableService = new DataTableService(Request::create('/'), new Store('test', new NullSessionHandler()));
|
||||
$dataTableService = new DataTableService(
|
||||
Request::create('/'),
|
||||
new Store('test', new NullSessionHandler()),
|
||||
static fn (string $table): array => Schema::getColumnListing($table),
|
||||
);
|
||||
$query = new GetPaginatedPickupExceptionsQuery($dataTableService);
|
||||
$input = new GetPaginatedPickupExceptionsQueryInput(
|
||||
pagination: Pagination::ofPage(new PageNumber(1), new PageSize(10)),
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ use Domain\Models\ValueObjects\DiscountValue;
|
|||
use Illuminate\Http\Request;
|
||||
use Illuminate\Session\NullSessionHandler;
|
||||
use Illuminate\Session\Store;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Infrastructure\Adapters\Repositories\EloquentProductRepository;
|
||||
use Money\Currencies\ISOCurrencies;
|
||||
use Money\Formatter\IntlMoneyFormatter;
|
||||
|
|
@ -90,7 +91,11 @@ describe('GetBoPaginatedProductsQuery', function (): void {
|
|||
$prod3 = $createProduct('Prod without Discount', $range, null);
|
||||
|
||||
$query = new GetBoPaginatedProductsQuery(
|
||||
new DataTableService(Request::create('/'), new Store('test', new NullSessionHandler())),
|
||||
new DataTableService(
|
||||
Request::create('/'),
|
||||
new Store('test', new NullSessionHandler()),
|
||||
static fn (string $table): array => Schema::getColumnListing($table),
|
||||
),
|
||||
new IntlMoneyFormatter(new \NumberFormatter('fr_FR', \NumberFormatter::CURRENCY), new ISOCurrencies()),
|
||||
new ProductDtoAssemblerService(new EloquentProductRepository()),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ use Illuminate\Foundation\Testing\RefreshDatabase;
|
|||
use Illuminate\Http\Request;
|
||||
use Illuminate\Session\NullSessionHandler;
|
||||
use Illuminate\Session\Store;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Shared\Pagination\PageNumber;
|
||||
use Shared\Pagination\PageSize;
|
||||
use Shared\Pagination\Pagination;
|
||||
|
|
@ -38,7 +39,11 @@ describe('GetPaginatedRangesQuery', function () {
|
|||
$range2->status = true;
|
||||
$range2->save();
|
||||
|
||||
$dataTableService = new DataTableService(Request::create('/'), new Store('test', new NullSessionHandler()));
|
||||
$dataTableService = new DataTableService(
|
||||
Request::create('/'),
|
||||
new Store('test', new NullSessionHandler()),
|
||||
static fn (string $table): array => Schema::getColumnListing($table),
|
||||
);
|
||||
$query = new GetPaginatedRangesQuery($dataTableService);
|
||||
$input = new GetPaginatedRangesQueryInput(
|
||||
pagination: Pagination::ofPage(new PageNumber(1), new PageSize(10)),
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ use Illuminate\Foundation\Testing\RefreshDatabase;
|
|||
use Illuminate\Http\Request;
|
||||
use Illuminate\Session\NullSessionHandler;
|
||||
use Illuminate\Session\Store;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Shared\Pagination\PageNumber;
|
||||
use Shared\Pagination\PageSize;
|
||||
use Shared\Pagination\Pagination;
|
||||
|
|
@ -31,7 +32,11 @@ describe('GetPaginatedShopsQuery', function () {
|
|||
$shop2 = Shop::factory()->create(['name' => 'Shop B']);
|
||||
$exception = PickupException::factory()->create();
|
||||
|
||||
$dataTableService = new DataTableService(Request::create('/'), new Store('test', new NullSessionHandler()));
|
||||
$dataTableService = new DataTableService(
|
||||
Request::create('/'),
|
||||
new Store('test', new NullSessionHandler()),
|
||||
static fn (string $table): array => Schema::getColumnListing($table),
|
||||
);
|
||||
$query = new GetPaginatedShopsQuery($dataTableService);
|
||||
$input = new GetPaginatedShopsQueryInput(
|
||||
pagination: Pagination::ofPage(new PageNumber(1), new PageSize(10)),
|
||||
|
|
@ -57,7 +62,11 @@ describe('GetPaginatedShopsQuery', function () {
|
|||
|
||||
$shop1->pickupExceptions()->attach($exception->id);
|
||||
|
||||
$dataTableService = new DataTableService(Request::create('/'), new Store('test', new NullSessionHandler()));
|
||||
$dataTableService = new DataTableService(
|
||||
Request::create('/'),
|
||||
new Store('test', new NullSessionHandler()),
|
||||
static fn (string $table): array => Schema::getColumnListing($table),
|
||||
);
|
||||
$query = new GetPaginatedShopsQuery($dataTableService);
|
||||
$input = new GetPaginatedShopsQueryInput(
|
||||
pagination: Pagination::ofPage(new PageNumber(1), new PageSize(10)),
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ use Illuminate\Http\Request;
|
|||
use Illuminate\Session\NullSessionHandler;
|
||||
use Illuminate\Session\Store;
|
||||
use Illuminate\Support\Facades\Date;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Shared\Pagination\PageNumber;
|
||||
use Shared\Pagination\PageSize;
|
||||
use Shared\Pagination\Pagination;
|
||||
|
|
@ -34,7 +35,13 @@ describe('GetPaginatedSlidesQuery', function (): void {
|
|||
$slide2->order = 2;
|
||||
$slide2->save();
|
||||
|
||||
$query = new GetPaginatedSlidesQuery(new DataTableService(Request::create('/'), new Store('test', new NullSessionHandler())));
|
||||
$query = new GetPaginatedSlidesQuery(
|
||||
new DataTableService(
|
||||
Request::create('/'),
|
||||
new Store('test', new NullSessionHandler()),
|
||||
static fn (string $table): array => Schema::getColumnListing($table),
|
||||
),
|
||||
);
|
||||
$input = new GetPaginatedSlidesQueryInput(
|
||||
pagination: Pagination::ofPage(new PageNumber(1), new PageSize(10)),
|
||||
);
|
||||
|
|
@ -75,7 +82,13 @@ describe('GetPaginatedSlidesQuery', function (): void {
|
|||
$futureSlide->date_end = $now->copy()->addDays(5);
|
||||
$futureSlide->save();
|
||||
|
||||
$query = new GetPaginatedSlidesQuery(new DataTableService(Request::create('/'), new Store('test', new NullSessionHandler())));
|
||||
$query = new GetPaginatedSlidesQuery(
|
||||
new DataTableService(
|
||||
Request::create('/'),
|
||||
new Store('test', new NullSessionHandler()),
|
||||
static fn (string $table): array => Schema::getColumnListing($table),
|
||||
),
|
||||
);
|
||||
$input = new GetPaginatedSlidesQueryInput(
|
||||
pagination: Pagination::ofPage(new PageNumber(1), new PageSize(10)),
|
||||
publishedOnly: true,
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ describe('DataTableService', function () {
|
|||
$session = mock(Session::class);
|
||||
$session->shouldReceive('get')->andReturn(null);
|
||||
|
||||
$service = new DataTableService($request, $session);
|
||||
$service = new DataTableService($request, $session, static fn (string $table): array => []);
|
||||
|
||||
// WHEN
|
||||
$sort = $service->getSortRequest();
|
||||
|
|
@ -42,7 +42,7 @@ describe('DataTableService', function () {
|
|||
$session->shouldReceive('get')->with('noroute.sort_col', 'undefined')->andReturn('name');
|
||||
$session->shouldReceive('get')->with('noroute.sort_way')->andReturn('asc');
|
||||
|
||||
$service = new DataTableService($request, $session);
|
||||
$service = new DataTableService($request, $session, static fn (string $table): array => []);
|
||||
|
||||
// WHEN
|
||||
$sort = $service->getSortRequest();
|
||||
|
|
@ -63,7 +63,7 @@ describe('DataTableService', function () {
|
|||
])->once();
|
||||
$session->shouldReceive('get')->andReturn(null);
|
||||
|
||||
$service = new DataTableService($request, $session);
|
||||
$service = new DataTableService($request, $session, static fn (string $table): array => []);
|
||||
|
||||
// WHEN
|
||||
$service->resetSortSession('noroute');
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue