Skip to content

Commit

Permalink
Added a builders() method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Helldar committed Jul 30, 2019
1 parent de855c7 commit 119664e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ public function handle() {
$collection_1 = Foo::whereIsActive(true)->get();
$collection_2 = Bar::where('id', '>', 50)->get();
$collection_3 = Baz::query()->get();

$builder_1 = Foo::whereIsActive(true);
$builder_2 = Bar::where('id', '>', 50);
$builder_3 = Baz::query();

$model_1 = Foo::whereIsActive(true)->first();
$model_2 = Bar::where('id', '>', 50)->first();
Expand All @@ -88,6 +92,7 @@ public function handle() {

(new LastModified)
->collections($collection_1, $collection_2, $collection_3)
->builders($builder_1, $builder_2, $builder_3)
->models($model_1, $model_2, $model_3)
->manuals($item_1, $item_2, $item_3)
->update(bool $force = false);
Expand All @@ -102,6 +107,10 @@ use Helldar\LastModified\Services\LastItem;
public function handle() {
$collection_1 = Foo::whereIsActive(false)->get();
$collection_2 = Bar::whereIn('id', [50, 60, 62, 73])->get();

$builder_1 = Foo::whereIsActive(true);
$builder_2 = Bar::where('id', '>', 50);
$builder_3 = Baz::query();

$model_1 = Foo::whereIsActive(false)->first();
$model_2 = Bar::whereIn('id', [50, 60, 62, 73])->first();
Expand All @@ -111,6 +120,7 @@ public function handle() {

(new LastModified)
->collections($collection_1, $collection_2, $collection_3)
->builders($builder_1, $builder_2, $builder_3)
->models($model_1, $model_2, $model_3)
->manuals($item_1, $item_2, $item_3)
->delete(bool $force = false);
Expand Down
16 changes: 16 additions & 0 deletions src/Exceptions/IncorrectBuilderTypeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Helldar\LastModified\Exceptions;

use Exception;
use Illuminate\Database\Eloquent\Builder;

class IncorrectBuilderTypeException extends Exception
{
public function __construct(string $classname)
{
$message = \sprintf('Item must be an instance of %s, instance of %s given.', Builder::class, $classname);

parent::__construct($message, 400);
}
}
23 changes: 23 additions & 0 deletions src/Services/LastModified.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

namespace Helldar\LastModified\Services;

use Helldar\LastModified\Exceptions\IncorrectBuilderTypeException;
use Helldar\LastModified\Exceptions\UrlNotFoundException;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;

class LastModified
{
private $collections = [];

private $builders = [];

private $models = [];

private $manuals = [];
Expand All @@ -20,6 +24,13 @@ public function collections(Collection ...$collections)
return $this;
}

public function builders(...$builders)
{
$this->builders = (array) $builders;

return $this;
}

public function models(...$models)
{
$this->models = (array) $models;
Expand Down Expand Up @@ -51,6 +62,18 @@ public function update(bool $force = false)
});
}

foreach ($this->builders as $builder) {
if ($builder instanceof Builder) {
$builder
->get()
->each(function ($item) {
$this->store($item);
});
} else {
throw new IncorrectBuilderTypeException(\get_class($builder));
}
}

foreach ($this->models as $model) {
$this->store($model);
}
Expand Down

0 comments on commit 119664e

Please sign in to comment.