133 lines
3.3 KiB
PHP
133 lines
3.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Repositories;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Exception;
|
|
|
|
/**
|
|
* Base repository implementation using Eloquent models with transaction support.
|
|
*/
|
|
class BaseRepository implements BaseRepositoryInterface
|
|
{
|
|
public function __construct(protected Model $model)
|
|
{
|
|
}
|
|
|
|
/**
|
|
* @param array<int, string> $columns
|
|
* @return Collection<int, Model>
|
|
*/
|
|
public function all(array $columns = ['*']): Collection
|
|
{
|
|
return $this->model->newQuery()->get($columns);
|
|
}
|
|
|
|
/**
|
|
* @param int|string $id
|
|
* @param array<int, string> $columns
|
|
*/
|
|
public function find(int|string $id, array $columns = ['*']): ?Model
|
|
{
|
|
return $this->model->newQuery()->find($id, $columns);
|
|
}
|
|
|
|
/**
|
|
* Create a new model instance with transaction support.
|
|
*
|
|
* @param array<string, mixed> $attributes
|
|
* @throws Exception
|
|
*/
|
|
public function create(array $attributes): Model
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
// Uses mass assignment; ensure $fillable is set on the model
|
|
$model = $this->model->newQuery()->create($attributes);
|
|
|
|
DB::commit();
|
|
|
|
return $model;
|
|
} catch (Exception $e) {
|
|
DB::rollBack();
|
|
Log::error('Error creating ' . get_class($this->model) . ': ' . $e->getMessage(), [
|
|
'attributes' => $attributes,
|
|
'exception' => $e
|
|
]);
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update an existing model instance with transaction support.
|
|
*
|
|
* @param int|string $id
|
|
* @param array<string, mixed> $attributes
|
|
* @throws Exception
|
|
*/
|
|
public function update(int|string $id, array $attributes): bool
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$instance = $this->find($id);
|
|
if (! $instance) {
|
|
DB::rollBack();
|
|
return false;
|
|
}
|
|
|
|
$result = $instance->fill($attributes)->save();
|
|
|
|
DB::commit();
|
|
|
|
return $result;
|
|
} catch (Exception $e) {
|
|
DB::rollBack();
|
|
Log::error('Error updating ' . get_class($this->model) . ' with ID ' . $id . ': ' . $e->getMessage(), [
|
|
'id' => $id,
|
|
'attributes' => $attributes,
|
|
'exception' => $e
|
|
]);
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Delete a model instance with transaction support.
|
|
*
|
|
* @param int|string $id
|
|
* @throws Exception
|
|
*/
|
|
public function delete(int|string $id): bool
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$instance = $this->find($id);
|
|
if (! $instance) {
|
|
DB::rollBack();
|
|
return false;
|
|
}
|
|
|
|
$result = (bool) $instance->delete();
|
|
|
|
DB::commit();
|
|
|
|
return $result;
|
|
} catch (Exception $e) {
|
|
DB::rollBack();
|
|
Log::error('Error deleting ' . get_class($this->model) . ' with ID ' . $id . ': ' . $e->getMessage(), [
|
|
'id' => $id,
|
|
'exception' => $e
|
|
]);
|
|
throw $e;
|
|
}
|
|
}
|
|
}
|