<commit_before><?php
/**
 * Created by IntelliJ IDEA.
 * User: kofel
 * Date: 27.11.14
 * Time: 11:49
 */

namespace Gutenberg\Printer;


use Gutenberg\Printable\PrintableInterface;
use Gutenberg\Printer\CUPS\Exception\PrinterException;
use Gutenberg\Printer\CUPS\Exception\InvalidPrinterProfileException;
use Gutenberg\Printer\CUPS\PrinterProfileInterface;
use Symfony\Component\Process\Exception\ProcessTimedOutException;
use Symfony\Component\Process\Exception\RuntimeException;
use Symfony\Component\Process\ProcessBuilder;

class CUPSPrinter {
    const LPR_TIMEOUT = 2;

    /**
     * Path to CUPS's lpr binary
     * @var string
     */
    private $lprBinaryPath = 'lpr';

    /**
     * @var callable
     */
    private $processCallback;

    /**
     * @param PrintableFileInterface $printable
     * @param PrinterProfileInterface $printerProfile
     */
    public function enqueue(PrintableInterface $printable, PrinterProfileInterface $printerProfile)
    {
        $processBuilder = new ProcessBuilder();
        $processBuilder->setArguments($this->getProcessArguments($printable, $printerProfile));
        $processBuilder->setTimeout(self::LPR_TIMEOUT);
        $processBuilder->setInput($printable->getContent());

        try {
            $process = $processBuilder->getProcess();
            if (is_callable($this->processCallback)) {
                $this->processCallback($process);
            }
            $process->run();

            if (!$process->isSuccessful()) {
                $errorOutput = $process->getErrorOutput();

                if (strpos($errorOutput, 'The printer or class does not exist.') !== false)
                    throw new InvalidPrinterProfileException(
                        'Printer profile does not exist.'
                    );
                else
                    throw new PrinterException(
                        $process->getErrorOutput()
                    );
            }
        }
        catch (RuntimeException $e) {
            throw new PrinterException($e->getMessage(), $e->getCode(), $e);
        }
    }

    /**
     * @return string
     */
    public function getLprBinaryPath()
    {
        return $this->lprBinaryPath;
    }

    /**
     * @param string $lprBinaryPath
     */
    public function setLprBinaryPath($lprBinaryPath)
    {
        $this->lprBinaryPath = $lprBinaryPath;
    }

    /**
     * @param PrinterProfileInterface $printerProfile
     * @return array
     */
    private function getOptionArgumentsByPrinterProfile(PrinterProfileInterface $printerProfile)
    {
        $arguments = [];

        foreach ($printerProfile->getOptions() as $key => $value) {
            $arguments[] = '-o';
            $arguments[] = $value !== null ? implode('=', [$key, $value]) : $key;
        }

        return $arguments;
    }

    /**
     * @param PrintableFileInterface $printable
     * @param PrinterProfileInterface $printerProfile
     * @return array
     */
    public function getProcessArguments(PrintableInterface $printable, PrinterProfileInterface $printerProfile)
    {
        return array_merge(
            [$this->lprBinaryPath],
            $this->getOptionArgumentsByPrinterProfile($printerProfile),
            [
                '-P',
                $printerProfile->getName(),
                (string)$printable->getFile()
            ]
        );
    }

    /**
     * @param callable $processCallback
     */
    public function setProcessCallback($processCallback)
    {
        $this->processCallback = $processCallback;
    }
}<commit_msg>* CUPSPrinter sends document as an input to lpr command<commit_after><?php
/**
 * Created by IntelliJ IDEA.
 * User: kofel
 * Date: 27.11.14
 * Time: 11:49
 */

namespace Gutenberg\Printer;


use Gutenberg\Printable\PrintableInterface;
use Gutenberg\Printer\CUPS\Exception\PrinterException;
use Gutenberg\Printer\CUPS\Exception\InvalidPrinterProfileException;
use Gutenberg\Printer\CUPS\PrinterProfileInterface;
use Symfony\Component\Process\Exception\ProcessTimedOutException;
use Symfony\Component\Process\Exception\RuntimeException;
use Symfony\Component\Process\ProcessBuilder;

class CUPSPrinter {
    const LPR_TIMEOUT = 2;

    /**
     * Path to CUPS's lpr binary
     * @var string
     */
    private $lprBinaryPath = 'lpr';

    /**
     * @var callable
     */
    private $processCallback;

    /**
     * @param PrintableFileInterface $printable
     * @param PrinterProfileInterface $printerProfile
     */
    public function enqueue(PrintableInterface $printable, PrinterProfileInterface $printerProfile)
    {
        $processBuilder = new ProcessBuilder();
        $processBuilder->setArguments($this->getProcessArguments($printable, $printerProfile));
        $processBuilder->setTimeout(self::LPR_TIMEOUT);
        $processBuilder->setInput($printable->getContent());

        try {
            $process = $processBuilder->getProcess();
            if (is_callable($this->processCallback)) {
                call_user_func($this->processCallback, $process);
            }
            $process->run();

            if (!$process->isSuccessful()) {
                $errorOutput = $process->getErrorOutput();

                if (strpos($errorOutput, 'The printer or class does not exist.') !== false)
                    throw new InvalidPrinterProfileException(
                        'Printer profile does not exist.'
                    );
                else
                    throw new PrinterException(
                        $process->getErrorOutput()
                    );
            }
        }
        catch (RuntimeException $e) {
            throw new PrinterException($e->getMessage(), $e->getCode(), $e);
        }
    }

    /**
     * @return string
     */
    public function getLprBinaryPath()
    {
        return $this->lprBinaryPath;
    }

    /**
     * @param string $lprBinaryPath
     */
    public function setLprBinaryPath($lprBinaryPath)
    {
        $this->lprBinaryPath = $lprBinaryPath;
    }

    /**
     * @param PrinterProfileInterface $printerProfile
     * @return array
     */
    private function getOptionArgumentsByPrinterProfile(PrinterProfileInterface $printerProfile)
    {
        $arguments = [];

        foreach ($printerProfile->getOptions() as $key => $value) {
            $arguments[] = '-o';
            $arguments[] = $value !== null ? implode('=', [$key, $value]) : $key;
        }

        return $arguments;
    }

    /**
     * @param PrintableFileInterface $printable
     * @param PrinterProfileInterface $printerProfile
     * @return array
     */
    public function getProcessArguments(PrintableInterface $printable, PrinterProfileInterface $printerProfile)
    {
        return array_merge(
            [$this->lprBinaryPath],
            $this->getOptionArgumentsByPrinterProfile($printerProfile),
            [
                '-P',
                $printerProfile->getName(),
                (string)$printable->getFile()
            ]
        );
    }

    /**
     * @param callable $processCallback
     */
    public function setProcessCallback($processCallback)
    {
        $this->processCallback = $processCallback;
    }
}
{'fraction_non_alphanumeric': 0.11357956123299083, 'fraction_numerical': 0.0030547070258261596, 'mean_word_length': 2.2489851150202975, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 49, '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': '233916', 'n_tokens_mistral': 2001, 'n_tokens_neox': 1920, 'n_words': 364}