diff --git a/app/Repositories/Contracts/RepositoryInterface.php b/app/Repositories/Contracts/RepositoryInterface.php index 70684bcd..7eeaac9e 100644 --- a/app/Repositories/Contracts/RepositoryInterface.php +++ b/app/Repositories/Contracts/RepositoryInterface.php @@ -2,18 +2,31 @@ namespace App\Repositories\Contracts; +use Illuminate\Database\Eloquent\Collection as EloquentCollection; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Collection; +/** @template T of Model */ interface RepositoryInterface { + /** @return T */ public function getOne($id): Model; + /** @return T */ + public function getOneBy(array $params): Model; + + /** @return T|null */ public function findOne($id): ?Model; - /** @return Collection */ + /** @return T|null */ + public function findOneBy(array $params): ?Model; + + /** @return Collection */ public function getMany(array $ids, bool $preserveOrder = false): Collection; - /** @return Collection */ - public function getAll(): Collection; + /** @return Collection */ + public function getAll(): EloquentCollection; + + /** @return T|null */ + public function findFirstWhere(...$params): ?Model; } diff --git a/app/Repositories/Repository.php b/app/Repositories/Repository.php index 07bf242c..d181ffb2 100644 --- a/app/Repositories/Repository.php +++ b/app/Repositories/Repository.php @@ -4,72 +4,78 @@ namespace App\Repositories; use App\Repositories\Contracts\RepositoryInterface; use Illuminate\Contracts\Auth\Guard; +use Illuminate\Database\Eloquent\Collection as EloquentCollection; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Collection; -/** @template T of Model */ +/** + * @template T of Model + * @implements RepositoryInterface + */ abstract class Repository implements RepositoryInterface { - private string $modelClass; - protected Guard $auth; - public Model $model; + /** @var class-string $modelClass */ + protected string $modelClass; + protected Guard $auth; + + /** @param class-string $modelClass */ public function __construct(?string $modelClass = null) { $this->modelClass = $modelClass ?: self::guessModelClass(); - $this->model = app($this->modelClass); // This instantiation may fail during a console command if e.g. APP_KEY is empty, // rendering the whole installation failing. rescue(fn () => $this->auth = app(Guard::class)); } + /** @return class-string */ private static function guessModelClass(): string { return preg_replace('/(.+)\\\\Repositories\\\\(.+)Repository$/m', '$1\Models\\\$2', static::class); } - /** @return T */ + /** @inheritDoc */ public function getOne($id): Model { - return $this->model::query()->findOrFail($id); + return $this->modelClass::query()->findOrFail($id); } - /** @return T|null */ - public function findOneBy(array $params): ?Model - { - return $this->model::query()->where($params)->first(); - } - - /** @return T|null */ + /** @inheritDoc */ public function findOne($id): ?Model { - return $this->model::query()->find($id); + return $this->modelClass::query()->find($id); } - /** @return T */ + /** @inheritDoc */ public function getOneBy(array $params): Model { - return $this->model::query()->where($params)->firstOrFail(); + return $this->modelClass::query()->where($params)->firstOrFail(); } - /** @return array|Collection */ + /** @inheritDoc */ + public function findOneBy(array $params): ?Model + { + return $this->modelClass::query()->where($params)->first(); + } + + /** @inheritDoc */ public function getMany(array $ids, bool $preserveOrder = false): Collection { - $models = $this->model::query()->find($ids); + $models = $this->modelClass::query()->find($ids); return $preserveOrder ? $models->orderByArray($ids) : $models; } - /** @return array|Collection */ - public function getAll(): Collection + /** @inheritDoc */ // @phpcs:ignore + public function getAll(): EloquentCollection { - return $this->model::all(); + return $this->modelClass::all(); } - /** @return T|null */ + /** @inheritDoc */ public function findFirstWhere(...$params): ?Model { - return $this->model::query()->firstWhere(...$params); + return $this->modelClass::query()->firstWhere(...$params); } } diff --git a/app/Repositories/SettingRepository.php b/app/Repositories/SettingRepository.php index 75535355..63925e75 100644 --- a/app/Repositories/SettingRepository.php +++ b/app/Repositories/SettingRepository.php @@ -10,7 +10,7 @@ class SettingRepository extends Repository /** @return array */ public function getAllAsKeyValueArray(): array { - return $this->model->pluck('value', 'key')->toArray(); + return $this->modelClass::query()->pluck('value', 'key')->toArray(); } public function getByKey(string $key): mixed