129 lines
2.8 KiB
PHP
Raw Permalink Normal View History

2019-11-16 10:21:14 +03:00
<?php
namespace App\Abstracts;
use App\Abstracts\Http\FormRequest;
2021-09-06 11:53:57 +03:00
use App\Interfaces\Job\HasOwner;
2021-09-07 10:33:34 +03:00
use App\Interfaces\Job\HasSource;
2021-09-06 11:53:57 +03:00
use App\Interfaces\Job\ShouldCreate;
use App\Interfaces\Job\ShouldDelete;
use App\Interfaces\Job\ShouldUpdate;
2019-11-16 10:21:14 +03:00
use App\Traits\Jobs;
use App\Traits\Relationships;
2021-09-07 10:33:34 +03:00
use App\Traits\Sources;
2019-11-16 10:21:14 +03:00
use App\Traits\Uploads;
2021-09-06 11:53:57 +03:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
2019-11-16 10:21:14 +03:00
2021-04-16 00:59:43 +03:00
abstract class Job
2019-11-16 10:21:14 +03:00
{
2021-09-07 10:33:34 +03:00
use Jobs, Relationships, Sources, Uploads;
2019-11-16 10:21:14 +03:00
2021-09-06 11:53:57 +03:00
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 Request) {
$this->request = $request;
}
if ($this instanceof HasOwner) {
$this->setOwner();
}
2021-09-07 10:33:34 +03:00
if ($this instanceof HasSource) {
$this->setSource();
}
2021-09-06 11:53:57 +03:00
}
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 Request) {
$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
{
//
}
2019-11-16 10:21:14 +03:00
public function getRequestInstance($request)
{
if (!is_array($request)) {
return $request;
}
$class = new class() extends FormRequest {};
return $class->merge($request);
}
2021-09-06 11:53:57 +03:00
public function setOwner(): void
{
if (! $this->request instanceof Request) {
return;
}
if ($this->request->has('created_by')) {
return;
}
$this->request->merge(['created_by' => user_id()]);
}
2021-09-07 10:33:34 +03:00
public function setSource(): void
{
if (! $this->request instanceof Request) {
return;
}
if ($this->request->has('created_from')) {
return;
}
$this->request->merge(['created_from' => $this->getSourceName($this->request)]);
}
2019-11-16 10:21:14 +03:00
}