refactor: Return a string instead of a resource.

This commit is contained in:
Pol Dellaiera 2022-03-08 15:46:31 +01:00
parent b8992b8eeb
commit 8abed67e1c
No known key found for this signature in database
GPG Key ID: D476DFE9C67467CA
2 changed files with 33 additions and 20 deletions

View File

@ -13,10 +13,5 @@ namespace Chill\DocGeneratorBundle\GeneratorDriver;
interface DriverInterface
{
/**
* @param resource $template
*
* @return resource
*/
public function generateFromResource($template, string $resourceType, array $data, ?string $templateName = null);
public function generateFromString(string $template, string $resourceType, array $data, ?string $templateName = null): string;
}

View File

@ -20,40 +20,40 @@ use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Throwable;
class RelatorioDriver implements DriverInterface
final class RelatorioDriver implements DriverInterface
{
private LoggerInterface $logger;
private HttpClientInterface $client;
private HttpClientInterface $relatorioClient;
private LoggerInterface $logger;
private string $url;
public function __construct(
HttpClientInterface $relatorioClient,
HttpClientInterface $client,
ParameterBagInterface $parameterBag,
LoggerInterface $logger
) {
$this->relatorioClient = $relatorioClient;
$this->client = $client;
$this->logger = $logger;
$this->url = $parameterBag->get('chill_doc_generator')['driver']['relatorio']['url'];
}
public function generateFromResource($template, string $resourceType, array $data, ?string $templateName = null)
public function generateFromString(string $template, string $resourceType, array $data, ?string $templateName = null): string
{
$formFields = [
'variables' => json_encode($data),
'template' => new DataPart($template, $templateName ?? uniqid('template_'), $resourceType),
];
$form = new FormDataPart($formFields);
$form = new FormDataPart(
[
'variables' => json_encode($data),
'template' => new DataPart($template, $templateName ?? uniqid('template_'), $resourceType),
]
);
try {
$response = $this->relatorioClient->request('POST', $this->url, [
$response = $this->client->request('POST', $this->url, [
'headers' => $form->getPreparedHeaders()->toArray(),
'body' => $form->bodyToIterable(),
]);
return $response->toStream();
} catch (HttpExceptionInterface $e) {
$content = $e->getResponse()->getContent(false);
@ -88,5 +88,23 @@ class RelatorioDriver implements DriverInterface
throw $e;
}
try {
$content = $response->getContent();
} catch (Throwable $exception) {
$this
->logger
->error(
'relatorio: Unable to get content from response.',
[
'msg' => $exception->getMessage(),
'e' => $exception->getTraceAsString(),
]
);
throw $exception;
}
return $content;
}
}