2021-04-16 00:59:43 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Abstracts;
|
|
|
|
|
|
|
|
use App\Abstracts\Http\FormRequest;
|
2022-02-14 09:05:20 +03:00
|
|
|
use App\Interfaces\Job\HasOwner;
|
|
|
|
use App\Interfaces\Job\HasSource;
|
|
|
|
use App\Interfaces\Job\ShouldCreate;
|
|
|
|
use App\Interfaces\Job\ShouldDelete;
|
|
|
|
use App\Interfaces\Job\ShouldUpdate;
|
2021-04-16 00:59:43 +03:00
|
|
|
use App\Traits\Jobs;
|
|
|
|
use App\Traits\Relationships;
|
2022-02-14 09:05:20 +03:00
|
|
|
use App\Traits\Sources;
|
2021-04-16 00:59:43 +03:00
|
|
|
use App\Traits\Uploads;
|
2022-02-15 13:20:07 +03:00
|
|
|
use App\Utilities\QueueCollection;
|
2021-04-16 00:59:43 +03:00
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
2022-02-14 09:05:20 +03:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2021-04-16 00:59:43 +03:00
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
|
|
abstract class JobShouldQueue implements ShouldQueue
|
|
|
|
{
|
2022-02-14 09:05:20 +03:00
|
|
|
use InteractsWithQueue, Jobs, Queueable, Relationships, SerializesModels, Sources, Uploads;
|
|
|
|
|
|
|
|
protected $model;
|
|
|
|
|
|
|
|
protected $request;
|
|
|
|
|
|
|
|
public function __construct(...$arguments)
|
|
|
|
{
|
|
|
|
$this->booting(...$arguments);
|
|
|
|
$this->bootCreate(...$arguments);
|
|
|
|
$this->bootUpdate(...$arguments);
|
|
|
|
$this->bootDelete(...$arguments);
|
|
|
|
$this->booted(...$arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function booting(...$arguments): void
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
public function bootCreate(...$arguments): void
|
|
|
|
{
|
|
|
|
if (! $this instanceof ShouldCreate) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$request = $this->getRequestInstance($arguments[0]);
|
2022-02-15 13:20:07 +03:00
|
|
|
if ($request instanceof QueueCollection) {
|
2022-02-14 09:05:20 +03:00
|
|
|
$this->request = $request;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this instanceof HasOwner) {
|
|
|
|
$this->setOwner();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this instanceof HasSource) {
|
|
|
|
$this->setSource();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function bootUpdate(...$arguments): void
|
|
|
|
{
|
|
|
|
if (! $this instanceof ShouldUpdate) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($arguments[0] instanceof Model) {
|
|
|
|
$this->model = $arguments[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
$request = $this->getRequestInstance($arguments[1]);
|
2022-02-15 13:20:07 +03:00
|
|
|
if ($request instanceof QueueCollection) {
|
2022-02-14 09:05:20 +03:00
|
|
|
$this->request = $request;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function bootDelete(...$arguments): void
|
|
|
|
{
|
|
|
|
if (! $this instanceof ShouldDelete) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($arguments[0] instanceof Model) {
|
|
|
|
$this->model = $arguments[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function booted(...$arguments): void
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
2021-04-16 00:59:43 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if request is array and if so, convert to a request class.
|
|
|
|
*
|
|
|
|
* @param mixed $request
|
|
|
|
* @return \Illuminate\Foundation\Http\FormRequest
|
|
|
|
*
|
|
|
|
* @deprecated Request is not serializable so can't use it with queues.
|
|
|
|
*/
|
|
|
|
public function getRequestInstance($request)
|
|
|
|
{
|
|
|
|
return $this->getRequestAsCollection($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Covert the request to collection.
|
|
|
|
*
|
|
|
|
* @param mixed $request
|
2022-02-15 13:20:07 +03:00
|
|
|
* @return \App\Utilities\QueueCollection
|
2021-04-16 00:59:43 +03:00
|
|
|
*/
|
|
|
|
public function getRequestAsCollection($request)
|
|
|
|
{
|
|
|
|
if (is_array($request)) {
|
|
|
|
$data = $request;
|
|
|
|
|
|
|
|
$request = new class() extends FormRequest {};
|
|
|
|
|
|
|
|
$request->merge($data);
|
|
|
|
}
|
|
|
|
|
2022-02-15 13:20:07 +03:00
|
|
|
return new QueueCollection($request->all());
|
2021-04-16 00:59:43 +03:00
|
|
|
}
|
2022-02-14 09:05:20 +03:00
|
|
|
|
|
|
|
public function setOwner(): void
|
|
|
|
{
|
2022-02-15 13:20:07 +03:00
|
|
|
if (! $this->request instanceof QueueCollection) {
|
2022-02-14 09:05:20 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->request->has('created_by')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->request->merge(['created_by' => user_id()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setSource(): void
|
|
|
|
{
|
2022-02-15 13:20:07 +03:00
|
|
|
if (! $this->request instanceof QueueCollection) {
|
2022-02-14 09:05:20 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->request->has('created_from')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->request->merge(['created_from' => $this->getSourceName($this->request)]);
|
|
|
|
}
|
2021-04-16 00:59:43 +03:00
|
|
|
}
|