Lae/app/Jobs/Remote/Host.php

87 lines
2.0 KiB
PHP
Raw Normal View History

<?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
*/
2022-09-03 06:01:51 +00:00
public function __construct($host, $type = 'post')
{
//
$this->host = $host;
$this->type = $type;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
//
2022-09-03 06:01:51 +00:00
// dd($this->host);
// $host = HostModel::find($this->host);
$host = $this->host;
$host->load(['module']);
$http = Http::remote($host->module->api_token, $host->module->url);
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());
break;
case 'post':
2022-09-03 06:01:51 +00:00
$response = $http->post('hosts', $host->toArray());
break;
case 'delete':
2022-09-03 06:01:51 +00:00
$response = $http->delete('hosts/' . $host->id);
2022-09-03 10:55:01 +00:00
if ($response->status() === 404) {
$host->delete();
}
return 0;
2022-09-03 06:01:51 +00:00
// if response code is 404
// if ($response->successful() || $response->failed()) {
// $host->delete();
// }
// if success
2022-09-03 06:01:51 +00:00
// if ($response->successful()) {
// $host->delete();
// }
break;
}
if (!$response->successful()) {
2022-09-03 06:01:51 +00:00
$host->status = 'error';
}
2022-09-03 06:01:51 +00:00
$host->save();
}
}