Skip to content

Commit

Permalink
add pulse and wip on repository
Browse files Browse the repository at this point in the history
  • Loading branch information
maks-oleksyuk committed Dec 29, 2024
1 parent cdcede9 commit 85d4322
Show file tree
Hide file tree
Showing 10 changed files with 2,003 additions and 156 deletions.
11 changes: 11 additions & 0 deletions app/Data/Filters/Contracts/FiltersData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace App\Data\Filters\Contracts;

/**
* This interface is a contract for implementing classes to define
* filtering functionality for the application (models) data.
*/
interface FiltersData {}
18 changes: 18 additions & 0 deletions app/Data/Filters/Models/UserFilters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace App\Data\Filters\Models;

use App\Data\Filters\Contracts\FiltersData;
use Spatie\LaravelData\Data;

final class UserFilters extends Data implements FiltersData
{
/**
* @param array<int> $ids
*/
public function __construct(
public ?array $ids = null,
) {}
}
65 changes: 65 additions & 0 deletions app/Repositories/Contracts/BaseRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace App\Repositories\Contracts;

use App\Data\Filters\Contracts\FiltersData;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

/**
* @template TModel of Model
*
* @implements BaseRepositoryInterface<TModel>
*/
abstract readonly class BaseRepository implements BaseRepositoryInterface
{
public function __construct(
protected Model $model,
) {}

public function query(): Builder
{
return $this->model->newQuery();
}

public function find(int|string $id): ?Model
{
return $this->query()->findOrFail($id);
}

public function getAll(int $perPage = -1, int $page = 1): Collection|LengthAwarePaginator
{
return $perPage === -1
? $this->query()->get()
: $this->query()->paginate(perPage: $perPage, page: $page);
}

public function getFiltered(FiltersData $filters, array $order = [], int $perPage = -1, int $page = 1): Collection|LengthAwarePaginator
{
$query = $this->getFilteredQuery($filters, $order);

return $perPage === -1
? $query->get()
: $query->paginate(perPage: $perPage, page: $page);
}

/**
* @param Builder<TModel|Model> $query
* @param array<int|string, string> $order
* @return Builder<TModel|Model>
*/
protected function addQueryOrder(Builder $query, array $order): Builder
{
foreach ($order as $column => $direction) {
is_int($column)
? $query->orderBy($direction)
: $query->orderBy($column, $direction);
}

return $query;
}
}
32 changes: 30 additions & 2 deletions app/Repositories/Contracts/BaseRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace App\Repositories\Contracts;

use App\Data\Filters\Contracts\FiltersData;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

Expand All @@ -13,9 +16,34 @@
interface BaseRepositoryInterface
{
/**
* @return Collection<int, TModel>
* @return Builder<TModel|Model>
*/
public function all(): Collection;
public function query(): Builder;

/**
* @return TModel|Model
*/
public function find(int|string $id): ?Model;

/**
* @return Collection<int, TModel|Model>|LengthAwarePaginator<TModel|Model>
*/
public function getAll(int $perPage = -1, int $page = 1): Collection|LengthAwarePaginator;

/**
* @param array<int|string, string> $order
* @return Collection<int, TModel|Model>|LengthAwarePaginator<TModel|Model>
*/
public function getFiltered(
FiltersData $filters,
array $order = [],
int $perPage = -1,
int $page = 1,
): Collection|LengthAwarePaginator;

/**
* @param array<int|string, string> $order
* @return Builder<TModel|Model>
*/
public function getFilteredQuery(FiltersData $filters, array $order = []): Builder;
}
25 changes: 15 additions & 10 deletions app/Repositories/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@

namespace App\Repositories;

use App\Data\Filters\Contracts\FiltersData;
use App\Data\Filters\Models\UserFilters;
use App\Models\User;
use App\Repositories\Contracts\BaseRepositoryInterface;
use Illuminate\Database\Eloquent\Collection;
use App\Repositories\Contracts\BaseRepository;
use Illuminate\Database\Eloquent\Builder;

/**
* @implements BaseRepositoryInterface<User>
* @extends BaseRepository<User>
*/
final readonly class UserRepository implements BaseRepositoryInterface
final readonly class UserRepository extends BaseRepository
{
public function all(): Collection
/**
* @param UserFilters $filters
*/
public function getFilteredQuery(UserFilters|FiltersData $filters, array $order = []): Builder
{
return User::all();
}
$query = $this->query()
->when($filters->ids,
fn ($query) => $query->whereIn('id', $filters->ids)
);

public function find(int|string $id): User
{
return User::query()->findOrFail($id);
return $this->addQueryOrder($query, $order);
}
}
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"php": "^8.3",
"filament/filament": "^3.2",
"laravel/framework": "^11.35",
"maks-oleksyuk/laravel-composer-scaffold": "0.0.1"
"laravel/pulse": "^1.3",
"maks-oleksyuk/laravel-composer-scaffold": "0.0.1",
"spatie/laravel-data": "^4.11"
},
"require-dev": {
"driftingly/rector-laravel": "^2.0",
Expand Down
Loading

0 comments on commit 85d4322

Please sign in to comment.