<commit_before><?php

namespace BeubiQA\Application\Selenium;

use BeubiQA\Application\Lib\ResponseWaitter;
use BeubiQA\Application\Selenium\Options\SeleniumStartOptions;
use Symfony\Component\Process\ExecutableFinder;
use Symfony\Component\Process\Process;

class SeleniumStarter
{
    /** @var Process */
    private $process;
    /** @var ResponseWaitter */
    private $responseWaitter;
    /** @var ExecutableFinder */
    private $exeFinder;
    /**  @var SeleniumStartOptions */
    private $seleniumOptions;
    /**  @var string */
    private $startCommand;

    /**
     * @param Process          $process
     * @param ResponseWaitter  $responseWaitter
     * @param ExecutableFinder $exeFinder
     */
    public function __construct(SeleniumStartOptions $seleniumOptions, Process $process, ResponseWaitter $responseWaitter, ExecutableFinder $exeFinder)
    {
        $this->seleniumOptions = $seleniumOptions;
        $this->process = $process;
        $this->responseWaitter = $responseWaitter;
        $this->exeFinder = $exeFinder;
    }
    private function setStartCommand($command)
    {
        $this->startCommand = $command;
    }
    public function getStartCommand()
    {
        return $this->startCommand;
    }

    public function start()
    {
        $seleniumUrl = $this->seleniumOptions->getSeleniumUrl();
        $seleniumQuery = $this->seleniumOptions->getSeleniumQuery();
        $seleniumJarLocation = $this->seleniumOptions->getSeleniumJarLocation();
        if (!$seleniumUrl || !$seleniumQuery || !$seleniumJarLocation) {
            throw new \LogicException('Url, Query and Selenium Jar Location is mandatory and Jar Location should point to a .jar file.');
        }
        if (!is_file($seleniumJarLocation)) {
            throw new \RuntimeException('Selenium jar is not a file');
        }
        if (!is_readable($seleniumJarLocation)) {
            throw new \RuntimeException('Selenium jar not readable - '.$seleniumJarLocation);
        }
        if ($this->isSeleniumAvailable()) {
            throw new \RuntimeException('Selenium was already started');
        }
        $this->setStartCommand($this->createStartCommand());
        $this->process->setCommandLine($this->getStartCommand());
        $this->process->start();
        $this->responseWaitter->waitUntilAvailable($seleniumUrl, $seleniumQuery);
    }

    public function isSeleniumAvailable()
    {
        return $this->responseWaitter->isAvailable($this->seleniumOptions->getSeleniumUrl(), $this->seleniumOptions->getSeleniumQuery());
    }

    /**
     * @param array $options
     *
     * @return string
     */
    private function createStartCommand()
    {
        $cmd = $this->exeFinder->find('java').' -jar '.$this->seleniumOptions->getSeleniumJarLocation();
        if ($this->seleniumOptions->isXvfbEnabled()) {
            $xvfbCmd = 'DISPLAY=:1 '.$this->exeFinder->find('xvfb-run').' --server-args=":1 -screen 0 1280x800x24 -ac +extension RANDR"';
            $cmd = $xvfbCmd.' '.$cmd;
        }
        $seleniumExtraArgs = $this->seleniumOptions->getSeleniumExtraArguments();
        if ($seleniumExtraArgs) {
            foreach ($seleniumExtraArgs as $optionName => $optionValue) {
                $cmd .= ' -'.$optionName.' '.$optionValue;<commit_msg>do not start xvfb server, just use a display - in future I should allow set up this<commit_after><?php

namespace BeubiQA\Application\Selenium;

use BeubiQA\Application\Lib\ResponseWaitter;
use BeubiQA\Application\Selenium\Options\SeleniumStartOptions;
use Symfony\Component\Process\ExecutableFinder;
use Symfony\Component\Process\Process;

class SeleniumStarter
{
    /** @var Process */
    private $process;
    /** @var ResponseWaitter */
    private $responseWaitter;
    /** @var ExecutableFinder */
    private $exeFinder;
    /**  @var SeleniumStartOptions */
    private $seleniumOptions;
    /**  @var string */
    private $startCommand;

    /**
     * @param Process          $process
     * @param ResponseWaitter  $responseWaitter
     * @param ExecutableFinder $exeFinder
     */
    public function __construct(SeleniumStartOptions $seleniumOptions, Process $process, ResponseWaitter $responseWaitter, ExecutableFinder $exeFinder)
    {
        $this->seleniumOptions = $seleniumOptions;
        $this->process = $process;
        $this->responseWaitter = $responseWaitter;
        $this->exeFinder = $exeFinder;
    }
    private function setStartCommand($command)
    {
        $this->startCommand = $command;
    }
    public function getStartCommand()
    {
        return $this->startCommand;
    }

    public function start()
    {
        $seleniumUrl = $this->seleniumOptions->getSeleniumUrl();
        $seleniumQuery = $this->seleniumOptions->getSeleniumQuery();
        $seleniumJarLocation = $this->seleniumOptions->getSeleniumJarLocation();
        if (!$seleniumUrl || !$seleniumQuery || !$seleniumJarLocation) {
            throw new \LogicException('Url, Query and Selenium Jar Location is mandatory and Jar Location should point to a .jar file.');
        }
        if (!is_file($seleniumJarLocation)) {
            throw new \RuntimeException('Selenium jar is not a file');
        }
        if (!is_readable($seleniumJarLocation)) {
            throw new \RuntimeException('Selenium jar not readable - '.$seleniumJarLocation);
        }
        if ($this->isSeleniumAvailable()) {
            throw new \RuntimeException('Selenium was already started');
        }
        $this->setStartCommand($this->createStartCommand());
        $this->process->setCommandLine($this->getStartCommand());
        $this->process->start();
        $this->responseWaitter->waitUntilAvailable($seleniumUrl, $seleniumQuery);
    }

    public function isSeleniumAvailable()
    {
        return $this->responseWaitter->isAvailable($this->seleniumOptions->getSeleniumUrl(), $this->seleniumOptions->getSeleniumQuery());
    }

    /**
     * @param array $options
     *
     * @return string
     */
    private function createStartCommand()
    {
        $cmd = $this->exeFinder->find('java').' -jar '.$this->seleniumOptions->getSeleniumJarLocation();
        if ($this->seleniumOptions->isXvfbEnabled()) {
            $xvfbCmd = 'DISPLAY=:21 ';
            $cmd = $xvfbCmd.' '.$cmd;
        }
        $seleniumExtraArgs = $this->seleniumOptions->getSeleniumExtraArguments();
        if ($seleniumExtraArgs) {
            foreach ($seleniumExtraArgs as $optionName => $optionValue) {
                $cmd .= ' -'.$optionName.' '.$optionValue;
{'fraction_non_alphanumeric': 0.12860852298762793, 'fraction_numerical': 0.0021383839926683978, 'mean_word_length': 2.9684848484848483, 'pattern_counts': {'":': 1, '<': 5, '<?xml version=': 0, '>': 79, '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': '2548933', 'n_tokens_mistral': 1949, 'n_tokens_neox': 1877, 'n_words': 365}