From 49cae7c8d2624fa208a9d56e08d19ddc26d81de9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=BCneyt=20=C5=9Eent=C3=BCrk?= Date: Mon, 14 Feb 2022 09:05:20 +0300 Subject: [PATCH] update JobShouldQueue file --- app/Abstracts/JobShouldQueue.php | 106 ++++++++++++++++++++++++++++++- 1 file changed, 105 insertions(+), 1 deletion(-) diff --git a/app/Abstracts/JobShouldQueue.php b/app/Abstracts/JobShouldQueue.php index 04adaa208..4a3c6e8da 100644 --- a/app/Abstracts/JobShouldQueue.php +++ b/app/Abstracts/JobShouldQueue.php @@ -3,17 +3,95 @@ namespace App\Abstracts; use App\Abstracts\Http\FormRequest; +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; use App\Traits\Jobs; use App\Traits\Relationships; +use App\Traits\Sources; use App\Traits\Uploads; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; +use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Collection; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; abstract class JobShouldQueue implements ShouldQueue { - use InteractsWithQueue, Jobs, Queueable, Relationships, SerializesModels, Uploads; + 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]); + if ($request instanceof Collection) { + $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]); + if ($request instanceof Collection) { + $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 + { + // + } /** * Check if request is array and if so, convert to a request class. @@ -46,4 +124,30 @@ abstract class JobShouldQueue implements ShouldQueue return collect($request->all()); } + + public function setOwner(): void + { + if (! $this->request instanceof Collection) { + return; + } + + if ($this->request->has('created_by')) { + return; + } + + $this->request->merge(['created_by' => user_id()]); + } + + public function setSource(): void + { + if (! $this->request instanceof Collection) { + return; + } + + if ($this->request->has('created_from')) { + return; + } + + $this->request->merge(['created_from' => $this->getSourceName($this->request)]); + } }