<commit_before><?php

namespace Ekyna\Bundle\CommerceBundle\Service\Document;

use Exception;
use GuzzleHttp\Client;
use RuntimeException;

/**
 * Class PdfGenerator
 * @package Ekyna\Bundle\CommerceBundle\Service\Document
 * @author  <NAME> <<EMAIL>>
 */
class PdfGenerator
{
    /**
     * Micro service API url.
     *
     * @var string
     */
    private $endpoint;

    /**
     * @var string
     */
    private $token;


    /**
     * Constructor.
     *
     * @param string $endpoint
     * @param string $token
     */
    public function __construct(string $endpoint, string $token)
    {
        $this->endpoint = $endpoint;
        $this->token = $token;
    }

    /**
     * Generates a PDF form the given URL.
     *
     * @param string $url
     * @param array  $options
     *
     * @return string
     */
    public function generateFromUrl(string $url, array $options = []): string
    {
        return $this->generate(array_replace($options, ['url' => $url]));
    }

    /**
     * Generates a PDF form the given URL.
     *
     * @param string $html
     * @param array  $options
     *
     * @return string
     */
    public function generateFromHtml(string $html, array $options = []): string
    {
        return $this->generate(array_replace($options, ['html' => $html]));
    }

    private function generate(array $options) : string
    {
        $options = array_replace_recursive([
            'orientation' => 'portrait',
            'format'      => 'A4',
            'paper'       => [
                'width'  => null,
                'height' => null,
                'unit'   => 'in',
            ],
            'margins'     => [
                'top'    => 6,
                'right'  => 6,
                'bottom' => 6,
                'left'   => 6,
                'unit'   => 'mm',
            ],
            'header'      => null,
            'footer'      => null,
        ], $options);

        $client = new Client();

        try {
            $response = $client->request('GET', $this->endpoint, [
                'json'    => $options,
                'headers' => [
                    'X-AUTH-TOKEN' => $this->token,
                ],
            ]);

            if (200 !== $response->getStatusCode()) {
                throw new RuntimeException("PDF web service did not respond correctly.");
            }
        } catch (Exception $e) {
            $test = $e->getMessage();

            throw new RuntimeException(
                "PDF web service did not respond correctly."
            );
        }

        return $response->getBody()->getContents();
    }
}<commit_msg>[Commerce] Pdf generator : make 3 attempts.<commit_after><?php

namespace Ekyna\Bundle\CommerceBundle\Service\Document;

use Exception;
use GuzzleHttp\Client;
use RuntimeException;

/**
 * Class PdfGenerator
 * @package Ekyna\Bundle\CommerceBundle\Service\Document
 * @author  <NAME> <<EMAIL>>
 */
class PdfGenerator
{
    /**
     * Micro service API url.
     *
     * @var string
     */
    private $endpoint;

    /**
     * @var string
     */
    private $token;


    /**
     * Constructor.
     *
     * @param string $endpoint
     * @param string $token
     */
    public function __construct(string $endpoint, string $token)
    {
        $this->endpoint = $endpoint;
        $this->token = $token;
    }

    /**
     * Generates a PDF form the given URL.
     *
     * @param string $url
     * @param array  $options
     *
     * @return string
     */
    public function generateFromUrl(string $url, array $options = []): string
    {
        return $this->generate(array_replace($options, ['url' => $url]));
    }

    /**
     * Generates a PDF form the given URL.
     *
     * @param string $html
     * @param array  $options
     *
     * @return string
     */
    public function generateFromHtml(string $html, array $options = []): string
    {
        return $this->generate(array_replace($options, ['html' => $html]));
    }

    private function generate(array $options) : string
    {
        $options = array_replace_recursive([
            'orientation' => 'portrait',
            'format'      => 'A4',
            'paper'       => [
                'width'  => null,
                'height' => null,
                'unit'   => 'in',
            ],
            'margins'     => [
                'top'    => 6,
                'right'  => 6,
                'bottom' => 6,
                'left'   => 6,
                'unit'   => 'mm',
            ],
            'header'      => null,
            'footer'      => null,
        ], $options);

        $client = new Client();

        for ($i = 2; $i >= 0; $i--) {
            try {
                $response = $client->request('GET', $this->endpoint, [
                    // TODO Timeouts
                    'json'    => $options,
                    'headers' => [
                        'X-AUTH-TOKEN' => $this->token,
                    ],
                ]);

                if (200 !== $response->getStatusCode()) {
                    usleep(50000);
                    continue;
                }
            } catch (Exception $e) {
                if (0 == $i) {
                    throw new RuntimeException("PDF web service did not respond correctly.");
                }
            }

            return $response->getBody()->getContents();
        }

        throw new RuntimeException("Failed to generate PDF.");
    }
}
{'fraction_non_alphanumeric': 0.13975155279503104, 'fraction_numerical': 0.004567044208987943, 'mean_word_length': 1.4182862190812722, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7199699', 'n_tokens_mistral': 1568, 'n_tokens_neox': 1490, 'n_words': 360}