Repo for the search and displace core module including the interface to select files and search and displace operations to run on them. https://searchanddisplace.com
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.
 
 
 
 
 
 

78 lines
2.1 KiB

<?php
namespace App\SearchDisplace\Ingest;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
class SendDocument
{
protected $url;
public function __construct()
{
$this->url = env('SD_INGEST_URL') . '/ingest';
}
public function execute($id, $document)
{
try {
$response = $this->sendRequest($id, $document);
if ($response['status'] === 'fail') {
$message = array_key_exists('message', $response)
? $response['message']
: 'Something went wrong.';
throw new \Exception($message);
}
// The file in Ingest si in Processing state.
} catch (\Exception $exception) {
throw new \Exception($exception->getMessage());
}
}
/**
* Send request to Ingest.
*
* @param $id
* @param $document
* @return mixed
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function sendRequest($id, $document)
{
$client = new Client();
try {
$response = $client->request('post', $this->url, [
'headers' => [
'Accept' => 'application/json',
],
'multipart' => [
[
'name' => 'id',
'contents' => $id,
],
[
'name' => 'document',
'contents' => fopen($document['path'], 'r'),
'filename' => $document['name'],
'Content-type' => $document['type'],
]
],
]);
return json_decode($response->getBody()->getContents(), true);
} catch (ClientException $clientException) {
$error = json_decode($clientException->getResponse()->getBody()->getContents(), true);
throw new \Exception($error['message']);
} catch (\Exception $exception) {
throw $exception;
}
}
}