2022-08-23 09:36:10 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs\Remote;
|
|
|
|
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
|
|
|
use App\Models\Host as HostModel;
|
|
|
|
|
|
|
|
class Host implements ShouldQueue
|
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
|
|
public HostModel $host;
|
|
|
|
public string $type;
|
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct(HostModel $host, $type = 'post')
|
|
|
|
{
|
|
|
|
//
|
|
|
|
$this->host = $host;
|
|
|
|
$this->type = $type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
//
|
2022-08-29 17:11:19 +00:00
|
|
|
$this->host->load(['module']);
|
|
|
|
|
2022-08-23 09:36:10 +00:00
|
|
|
$http = Http::remote($this->host->module->api_token, $this->host->module->url);
|
|
|
|
|
|
|
|
switch ($this->type) {
|
2022-08-29 10:59:32 +00:00
|
|
|
case 'patch':
|
|
|
|
$response = $http->patch('hosts/' . $this->host->id, $this->host->toArray());
|
2022-08-23 09:36:10 +00:00
|
|
|
break;
|
|
|
|
case 'post':
|
|
|
|
$response = $http->post('hosts', $this->host->toArray());
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'delete':
|
|
|
|
$response = $http->delete('hosts/' . $this->host->id);
|
|
|
|
|
|
|
|
// if success
|
|
|
|
if ($response->successful()) {
|
|
|
|
$this->host->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$response->successful()) {
|
|
|
|
$this->host->status = 'error';
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->host->save();
|
|
|
|
}
|
|
|
|
}
|