You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
3.2 KiB
128 lines
3.2 KiB
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Spatie\WebhookServer\WebhookCall;
|
|
|
|
class SendToCore implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable;
|
|
|
|
private $url;
|
|
|
|
private $secret;
|
|
|
|
private $filePath;
|
|
|
|
private $id;
|
|
|
|
protected $hasFailed;
|
|
|
|
/**
|
|
* @var \Illuminate\Contracts\Filesystem\Filesystem
|
|
*/
|
|
private $storage;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*
|
|
* @param null $filePath
|
|
* @param bool $hasFailed
|
|
*/
|
|
public function __construct($filePath = null, $hasFailed = false)
|
|
{
|
|
$this->url = env('WEBHOOK_CORE_URL') . '/webhooks';
|
|
$this->secret = env('WEBHOOK_CORE_SECRET');
|
|
$this->filePath = $filePath;
|
|
$this->hasFailed = $hasFailed;
|
|
|
|
$string = str_replace('contracts/', '', $this->filePath);
|
|
$result = explode('.', $string);
|
|
$this->id = $result[0];
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*
|
|
* @return void
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
|
|
*/
|
|
public function handle()
|
|
{
|
|
$content = '';
|
|
|
|
// File exists, send content.
|
|
if ($this->filePath && ! $this->hasFailed) {
|
|
$this->storage = Storage::disk('local');
|
|
|
|
// @TODO Check if the file exists multiple times?
|
|
if ( ! $this->storage->exists($this->filePath)) {
|
|
throw new \Exception('File does not exist yet.');
|
|
}
|
|
|
|
$content = $this->storage->get($this->filePath);
|
|
}
|
|
|
|
$sent = $this->sendTheData($content);
|
|
|
|
// if ($this->filePath && $sent) {
|
|
if ($this->filePath) {
|
|
$this->storage->delete($this->filePath);
|
|
}
|
|
}
|
|
|
|
public function failed()
|
|
{
|
|
if ($this->filePath) {
|
|
if ( ! $this->storage) {
|
|
$this->storage = Storage::disk('local');
|
|
}
|
|
|
|
$this->storage->delete($this->filePath);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Send the data to the core trough webhooks
|
|
*
|
|
* @param $content
|
|
* @return bool
|
|
*/
|
|
protected function sendTheData($content)
|
|
{
|
|
try {
|
|
WebhookCall::create()
|
|
->url($this->url)
|
|
->payload(['data' => [
|
|
'id' => $this->id,
|
|
'content' => $this->encodeContent($content),
|
|
'status' => $content ? 'success' : 'fail',
|
|
]])
|
|
->useSecret($this->secret)
|
|
->dispatch();
|
|
|
|
return true;
|
|
} catch (\Exception $exception) {
|
|
Log::error('SendToCore@sendTheData' . $exception->getMessage());
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
protected function encodeContent($content)
|
|
{
|
|
$encoding = mb_detect_encoding($content, 'UTF-8, ISO-8859-1, WINDOWS-1252, WINDOWS-1251', true);
|
|
|
|
if ($encoding != 'UTF-8') {
|
|
$content = iconv($encoding, 'UTF-8//IGNORE', $content);
|
|
}
|
|
|
|
return $content;
|
|
}
|
|
}
|