Skip to content
This repository has been archived by the owner on Dec 3, 2024. It is now read-only.

Feature | Models on Haystacks #45

Merged
merged 9 commits into from
Oct 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 65 additions & 1 deletion src/Builders/HaystackBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@
namespace Sammyjo20\LaravelHaystack\Builders;

use Closure;
use Carbon\Carbon;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Traits\Conditionable;
use Sammyjo20\LaravelHaystack\Models\Haystack;
use Sammyjo20\LaravelHaystack\Data\PendingData;
use Sammyjo20\LaravelHaystack\Data\HaystackOptions;
use Sammyjo20\LaravelHaystack\Casts\SerializedModel;
use Sammyjo20\LaravelHaystack\Helpers\ClosureHelper;
use Sammyjo20\LaravelHaystack\Helpers\DataValidator;
use Sammyjo20\LaravelHaystack\Contracts\StackableJob;
use Sammyjo20\LaravelHaystack\Data\PendingHaystackBale;
use Sammyjo20\LaravelHaystack\Exceptions\HaystackModelExists;

class HaystackBuilder
{
Expand Down Expand Up @@ -105,6 +108,13 @@ class HaystackBuilder
*/
protected Collection $initialData;

/**
* Closure to execute before saving
*
* @var Closure|null
*/
protected ?Closure $beforeSave = null;

/**
* Constructor
*/
Expand Down Expand Up @@ -375,6 +385,28 @@ public function withData(string $key, mixed $value, string $cast = null): static
return $this;
}

/**
* Store a model to be shared across all haystack jobs.
*
* @param Model $model
* @param string|null $key
* @return $this
*
* @throws HaystackModelExists
*/
public function withModel(Model $model, string $key = null): static
{
$key = 'model:'.($key ?? $model::class);

if ($this->initialData->has($key)) {
throw new HaystackModelExists($key);
}

$this->initialData->put($key, new PendingData($key, $model, SerializedModel::class));

return $this;
}

/**
* Create the Haystack
*
Expand Down Expand Up @@ -468,6 +500,11 @@ protected function createHaystack(): Haystack
$haystack->on_paused = $this->onPaused;
$haystack->middleware = $this->globalMiddleware;
$haystack->options = $this->options;

if ($this->beforeSave instanceof Closure) {
$haystack = tap($haystack, $this->beforeSave);
}

$haystack->save();

// We'll bulk insert the jobs and the data for efficient querying.
Expand Down Expand Up @@ -586,4 +623,31 @@ public function getGlobalMiddleware(): ?Closure
{
return $this->globalMiddleware;
}

/**
* Specify a closure to run before saving the Haystack
*
* @param Closure $closure
* @return $this
*/
public function beforeSave(Closure $closure): static
{
$this->beforeSave = $closure;

return $this;
}

/**
* Set an option on the Haystack Options.
*
* @param string $option
* @param mixed $value
* @return $this
*/
public function setOption(string $option, mixed $value): static
{
$this->options->$option = $value;

return $this;
}
}
49 changes: 49 additions & 0 deletions src/Casts/SerializedModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Sammyjo20\LaravelHaystack\Casts;

use InvalidArgumentException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Sammyjo20\LaravelHaystack\Data\SerializedModel as SerializedModelData;

class SerializedModel implements CastsAttributes
{
/**
* Unserialize a job.
*
* @param $model
* @param string $key
* @param $value
* @param array $attributes
* @return mixed|null
*/
public function get($model, string $key, $value, array $attributes)
{
return isset($value) ? unserialize($value, ['allowed_classes' => true])->model : null;
}

/**
* Serialize a model.
*
* @param $model
* @param string $key
* @param $value
* @param array $attributes
* @return mixed|string|null
*/
public function set($model, string $key, $value, array $attributes)
{
if (blank($value)) {
return null;
}

if (! $value instanceof Model) {
throw new InvalidArgumentException('The provided value must be a model.');
}

return serialize(new SerializedModelData($value));
}
}
21 changes: 19 additions & 2 deletions src/Concerns/ManagesBales.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,14 +329,31 @@ public function getData(string $key, mixed $default = null): mixed
return $data instanceof HaystackData ? $data->value : $default;
}

/**
* Retrieve a shared model
*
* @param string $key
* @param mixed|null $default
* @return mixed
*/
public function getModel(string $key, mixed $default = null): mixed
{
return $this->getData('model:'.$key, $default);
}

/**
* Retrieve all the data from the Haystack.
*
* @param bool $includeModels
* @return Collection
*/
public function allData(): Collection
public function allData(bool $includeModels = false): Collection
{
return $this->data()->orderBy('id')->get()->mapWithKeys(function ($value, $key) {
$data = $this->data()
->when($includeModels === false, fn ($query) => $query->where('key', 'NOT LIKE', 'model:%'))
->orderBy('id')->get();

return $data->mapWithKeys(function ($value, $key) {
return [$value->key => $value->value];
});
}
Expand Down
36 changes: 36 additions & 0 deletions src/Concerns/Stackable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

use Carbon\CarbonInterface;
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Model;
use Sammyjo20\LaravelHaystack\Models\Haystack;
use Sammyjo20\LaravelHaystack\Enums\FinishStatus;
use Sammyjo20\LaravelHaystack\Models\HaystackBale;
use Sammyjo20\LaravelHaystack\Data\HaystackOptions;
use Sammyjo20\LaravelHaystack\Helpers\CarbonHelper;
use Sammyjo20\LaravelHaystack\Contracts\StackableJob;
use Sammyjo20\LaravelHaystack\Tests\Exceptions\StackableException;
Expand Down Expand Up @@ -237,6 +239,18 @@ public function getHaystackData(string $key, mixed $default = null): mixed
return $this->haystack->getData($key, $default);
}

/**
* Get a shared model
*
* @param string $model
* @param mixed|null $default
* @return Model|null
*/
public function getHaystackModel(string $model, mixed $default = null): ?Model
{
return $this->haystack->getModel($model, $default);
}

/**
* Get all data on the haystack.
*
Expand Down Expand Up @@ -269,4 +283,26 @@ public function setHaystackBaleAttempts(int $attempts): static

return $this;
}

/**
* Get the options on the Haystack
*
* @return HaystackOptions
*/
public function getHaystackOptions(): HaystackOptions
{
return $this->haystack->options;
}

/**
* Retrieve a haystack option
*
* @param string $option
* @param mixed|null $default
* @return mixed
*/
public function getHaystackOption(string $option, mixed $default = null): mixed
{
return $this->haystack->options->$option ?? $default;
}
}
12 changes: 12 additions & 0 deletions src/Data/HaystackOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,16 @@ class HaystackOptions
* @var bool
*/
public bool $allowFailures = false;

/**
* Allow additional properties to be added to Haystack options.
*
* @param string $name
* @param $value
* @return void
*/
public function __set(string $name, $value): void
{
$this->$name = $value;
}
}
18 changes: 18 additions & 0 deletions src/Data/SerializedModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Sammyjo20\LaravelHaystack\Data;

use Illuminate\Queue\SerializesModels;
use Illuminate\Database\Eloquent\Model;

class SerializedModel
{
use SerializesModels;

public function __construct(public Model $model)
{
//
}
}
23 changes: 23 additions & 0 deletions src/Exceptions/HaystackModelExists.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Sammyjo20\LaravelHaystack\Exceptions;

use Exception;
use Illuminate\Support\Str;

class HaystackModelExists extends Exception
{
/**
* Constructor
*
* @param string $key
*/
public function __construct(string $key)
{
$key = Str::remove('model:', $key);

parent::__construct(sprintf('Model with the key "%s" has already been defined on the Haystack. Use the second argument to define a custom key.', $key));
}
}
10 changes: 10 additions & 0 deletions src/Models/Haystack.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,28 @@

namespace Sammyjo20\LaravelHaystack\Models;

use Closure;
use Carbon\CarbonImmutable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Prunable;
use Sammyjo20\LaravelHaystack\Casts\Serialized;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Sammyjo20\LaravelHaystack\Data\HaystackOptions;
use Sammyjo20\LaravelHaystack\Concerns\ManagesBales;
use Sammyjo20\LaravelHaystack\Casts\SerializeClosure;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Sammyjo20\LaravelHaystack\Builders\HaystackBuilder;
use Sammyjo20\LaravelHaystack\Database\Factories\HaystackFactory;

/**
* @property Closure $on_then
* @property Closure $on_catch
* @property Closure $on_finally
* @property Closure $on_paused
* @property Closure $middleware
* @property HaystackOptions $options
*/
class Haystack extends Model
{
use HasFactory;
Expand Down
Loading