<commit_before><?php

namespace Prezent\DompdfBundle\Wrapper;

use Symfony\Bundle\TwigBundle\TwigEngine;

/**
 * A wrapper class for DOMPDF, to initialize the class, and use it a Symfony application
 *
 * @author <NAME><<EMAIL>>
 */
class DompdfWrapper
{
    /**
     * @var \DOMPDF
     */
    private $pdf;

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

    /**
     * @var TwigEngine
     */
    private $renderer;

    /**
     * Constructor
     *
     * @param string $configFile
     * @param TwigEngine $renderer
     */
    public function __construct($configFile, TwigEngine $renderer)
    {
        $this->configFile = $configFile;
        $this->renderer = $renderer;
    }

    /**
     * Initialize the configuration
     *
     * @throws \RuntimeException If the configfile does not exist
     * @return true
     */
    private function initializeConfig()
    {
        if (file_exists($this->configFile)) {
            require_once $this->configFile;
        } else {
            throw new \RuntimeException(sprintf('Could not find config file "%s"', $this->configFile));
        }

        return true;
    }

    /**
     * Create a pdf document from an HTML string
     *
     * @param string $html The html to be rendered
     * @param string $orientation
     * @param string $paperSize
     * @return bool
     */
    public function createFromHtml($html, $orientation = 'portrait', $paperSize = null)
    {
        $this->initializeConfig();

        if (null === $paperSize) {
            $paperSize = DOMPDF_DEFAULT_PAPER_SIZE;
        }

        $this->pdf = new \DOMPDF();

        $this->pdf->set_paper($paperSize, $orientation);
        $this->pdf->load_html($html);
        $this->pdf->render();

        return true;
    }

    /**
     * Create a pdf document from a twig template
     *
     * @param string $template
     * @param array $data
     * @param string $orientation
     * @param string $paperSize
     * @return bool
     */
    public function createFromTemplate($template, array $data = [], $orientation = 'portrait', $paperSize = null)
    {
        $this->initializeConfig();

        // check if the template exists and is readable
        if (!$this->renderer->exists($template)) {
            throw new \RuntimeException('Template "%s" does not exist', $template);
        }
        $html = $this->renderer->render($template, $data);

        if (null === $paperSize) {
            $paperSize = DOMPDF_DEFAULT_PAPER_SIZE;
        }

        $this->pdf = new \DOMPDF();
        $this->pdf->set_paper($paperSize, $orientation);
        $this->pdf->load_html($html);

        $this->pdf->render();

        return true;
    }

    /**
     * Stream the pdf document
     *
     * @param  string $fileName The name of the document
     * @return bool
     */
    public function stream($fileName)
    {<commit_msg>lets reuse some code<commit_after><?php

namespace Prezent\DompdfBundle\Wrapper;

use Symfony\Bundle\TwigBundle\TwigEngine;

/**
 * A wrapper class for DOMPDF, to initialize the class, and use it a Symfony application
 *
 * @author <NAME><<EMAIL>>
 */
class DompdfWrapper
{
    /**
     * @var \DOMPDF
     */
    private $pdf;

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

    /**
     * @var TwigEngine
     */
    private $renderer;

    /**
     * Constructor
     *
     * @param string $configFile
     * @param TwigEngine $renderer
     */
    public function __construct($configFile, TwigEngine $renderer)
    {
        $this->configFile = $configFile;
        $this->renderer = $renderer;
    }

    /**
     * Initialize the configuration
     *
     * @throws \RuntimeException If the configfile does not exist
     * @return true
     */
    private function initializeConfig()
    {
        if (file_exists($this->configFile)) {
            require_once $this->configFile;
        } else {
            throw new \RuntimeException(sprintf('Could not find config file "%s"', $this->configFile));
        }

        return true;
    }

    /**
     * Create a pdf document from an HTML string
     *
     * @param string $html The html to be rendered
     * @param string $orientation
     * @param string $paperSize
     * @return bool
     */
    public function createFromHtml($html, $orientation = 'portrait', $paperSize = null)
    {
        $this->initializeConfig();

        if (null === $paperSize) {
            $paperSize = DOMPDF_DEFAULT_PAPER_SIZE;
        }

        $this->pdf = new \DOMPDF();

        $this->pdf->set_paper($paperSize, $orientation);
        $this->pdf->load_html($html);
        $this->pdf->render();

        return true;
    }

    /**
     * Create a pdf document from a twig template
     *
     * @param string $template
     * @param array $data
     * @param string $orientation
     * @param string $paperSize
     * @return bool
     */
    public function createFromTemplate($template, array $data = [], $orientation = 'portrait', $paperSize = null)
    {
        // check if the template exists and is readable
        if (!$this->renderer->exists($template)) {
            throw new \RuntimeException('Template "%s" does not exist', $template);
        }
        $html = $this->renderer->render($template, $data);

        return $this->createFromHtml($html, $orientation, $paperSize);
    }

    /**
     * Stream the pdf document
     *
     * @param  string $fileName The name of the document
     * @return bool
     */
    public function stream($fileName)
    {
{'fraction_non_alphanumeric': 0.12631388184124684, 'fraction_numerical': 0.0, 'mean_word_length': 2.1322360953461974, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 52, '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': '5184046', 'n_tokens_mistral': 1605, 'n_tokens_neox': 1573, 'n_words': 420}