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