Skip to content

Commit

Permalink
Apply fixes from StyleCI
Browse files Browse the repository at this point in the history
  • Loading branch information
sinemacula-ben authored and StyleCIBot committed Apr 18, 2024
1 parent 6817975 commit df27fc3
Showing 1 changed file with 114 additions and 114 deletions.
228 changes: 114 additions & 114 deletions src/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,58 +54,11 @@ public function __construct(
$this->boot();
}

/**
* Reset the scopes.
*
* @return static
*/
public function resetScopes(): static
{
$this->scopes = [];

return $this;
}

/**
* Create a new model instance.
*
* @return \Illuminate\Database\Eloquent\Model
*
* @throws \SineMacula\Repositories\Exceptions\RepositoryException|\Illuminate\Contracts\Container\BindingResolutionException
*/
public function makeModel(): Model
{
$model = $this->app->make($this->model());

if (!$model instanceof Model) {
throw new RepositoryException("Class {$this->model()} must be an instance of Illuminate\\Database\\Eloquent\\Model");
}

return $this->model = $model;
}

/**
* Boot the repository instance.
*
* This is a useful method for setting immediate properties when extending
* the base repository class.
*
* @return void
*/
protected function boot(): void {}

/**
* Return the model class
*
* @return class-string
*/
abstract public function model(): string;

/**
* Trigger a static method call on the repository.
*
* @param string $method
* @param array $arguments
* @param array $arguments
* @return mixed
*/
public static function __callStatic(string $method, array $arguments): mixed
Expand All @@ -117,7 +70,7 @@ public static function __callStatic(string $method, array $arguments): mixed
* Forward method calls to the model
*
* @param string $method
* @param array $arguments
* @param array $arguments
* @return mixed
*/
public function __call(string $method, array $arguments): mixed
Expand All @@ -131,80 +84,41 @@ public function __call(string $method, array $arguments): mixed
}

/**
* Apply the criteria to the current query.
* Reset the scopes.
*
* @return static
*/
protected function applyCriteria(): static
public function resetScopes(): static
{
if ($this->skipCriteria) {

$this->skipCriteria = false;
$this->resetTransientCriteria();

return $this;
}

if ($this->transientCriteria->isNotEmpty()) {

foreach ($this->transientCriteria as $criterion) {
$this->model = $criterion->apply($this->model);
}

$this->resetTransientCriteria();
}

if (!$this->disableCriteria && $this->persistentCriteria->isNotEmpty()) {
foreach ($this->persistentCriteria as $criterion) {
$this->model = $criterion->apply($this->model);
}
}
$this->scopes = [];

return $this;
}

/**
* Apply all accumulated scopes to the model.
* Create a new model instance.
*
* @return static
*/
protected function applyScopes(): static
{
foreach ($this->scopes as $scope) {
if (is_callable($scope)) {
$this->model = $scope($this->model);
}
}

return $this;
}

/**
* Reset the various transient values and return the result.
* @return \Illuminate\Database\Eloquent\Model
*
* @param mixed $result
* @return mixed
* @throws \SineMacula\Repositories\Exceptions\RepositoryException|\Illuminate\Contracts\Container\BindingResolutionException
*/
protected function resetAndReturn(mixed $result): mixed
public function makeModel(): Model
{
$this->resetTransientCriteria();
$this->resetScopes();
$this->resetModel();
$model = $this->app->make($this->model());

return $result;
if (!$model instanceof Model) {
throw new RepositoryException("Class {$this->model()} must be an instance of Illuminate\\Database\\Eloquent\\Model");
}

return $this->model = $model;
}

/**
* Clears all transient criteria.
* Return the model class
*
* @return static
* @return class-string
*/
private function resetTransientCriteria(): static
{
$this->transientCriteria = collect();

return $this;
}
abstract public function model(): string;

/**
* Reset the model instance.
Expand Down Expand Up @@ -388,30 +302,116 @@ public function resetCriteria(): static
}

/**
* Sanitize the given array of criteria to ensure they are valid criteria
* instances.
* Add a new scope.
*
* @param array $criteria
* @return array
* @param \Closure $scope
* @return static
*/
private function sanitizeCriteria(array $criteria): array
public function addScope(Closure $scope): static
{
return array_filter($criteria, fn ($criterion) => $criterion instanceof CriteriaInterface);
$this->scopes[] = $scope;

return $this;
}

/**
* Add a new scope.
* Boot the repository instance.
*
* This is a useful method for setting immediate properties when extending
* the base repository class.
*
* @return void
*/
protected function boot(): void {}

/**
* Apply the criteria to the current query.
*
* @param \Closure $scope
* @return static
*/
public function addScope(Closure $scope): static
protected function applyCriteria(): static
{
$this->scopes[] = $scope;
if ($this->skipCriteria) {

$this->skipCriteria = false;
$this->resetTransientCriteria();

return $this;
}

if ($this->transientCriteria->isNotEmpty()) {

foreach ($this->transientCriteria as $criterion) {
$this->model = $criterion->apply($this->model);
}

$this->resetTransientCriteria();
}

if (!$this->disableCriteria && $this->persistentCriteria->isNotEmpty()) {
foreach ($this->persistentCriteria as $criterion) {
$this->model = $criterion->apply($this->model);
}
}

return $this;
}

/**
* Apply all accumulated scopes to the model.
*
* @return static
*/
protected function applyScopes(): static
{
foreach ($this->scopes as $scope) {
if (is_callable($scope)) {
$this->model = $scope($this->model);
}
}

return $this;
}

/**
* Reset the various transient values and return the result.
*
* @param mixed $result
* @return mixed
*/
protected function resetAndReturn(mixed $result): mixed
{
$this->resetTransientCriteria();
$this->resetScopes();
$this->resetModel();

return $result;
}

/**
* Clears all transient criteria.
*
* @return static
*/
private function resetTransientCriteria(): static
{
$this->transientCriteria = collect();

return $this;
}

/**
* Sanitize the given array of criteria to ensure they are valid criteria
* instances.
*
* @param array $criteria
* @return array
*/
private function sanitizeCriteria(array $criteria): array
{
return array_filter($criteria, fn ($criterion) => $criterion instanceof CriteriaInterface);
}

/**
* Clears all persistent criteria.
*
Expand Down

0 comments on commit df27fc3

Please sign in to comment.