<commit_before><?php

namespace PhpPact\Standalone\MockService;

use Exception;
use GuzzleHttp\Exception\ConnectException;
use PhpPact\Http\GuzzleClient;
use PhpPact\Standalone\Exception\HealthCheckFailedException;
use PhpPact\Standalone\Installer\InstallManager;
use PhpPact\Standalone\Installer\Service\InstallerInterface;
use PhpPact\Standalone\MockService\Service\MockServerHttpService;
use PhpPact\Standalone\Runner\ProcessRunner;

/**
 * Ruby Standalone Mock Server Wrapper
 * Class MockServer.
 */
class MockServer
{
    /** @var MockServerConfig */
    private $config;

    /** @var InstallManager */
    private $installManager;

    /** @var MockServerHttpService */
    private $httpService;

    /** @var ProcessRunner */
    private $processRunner;

    /**
     * MockServer constructor.
     *
     * @param MockServerConfigInterface  $config
     * @param null|MockServerHttpService $httpService
     */
    public function __construct(MockServerConfigInterface $config, MockServerHttpService $httpService = null)
    {
        $this->config         = $config;
        $this->installManager = new InstallManager();

        if (!$httpService) {
            $this->httpService = new MockServerHttpService(new GuzzleClient(), $this->config);
        } else {
            $this->httpService = $httpService;
        }
    }

    /**
     * Start the Mock Server. Verify that it is running.
     *
     * @throws Exception
     *
     * @return int process ID of the started Mock Server
     */
    public function start(): int
    {
        $scripts = $this->installManager->install();

        $this->processRunner = new ProcessRunner($scripts->getMockService(), $this->getArguments());

        $processId =  $this->processRunner->run();

        $result = $this->verifyHealthCheck();
        if ($result) {
            $retrySec = $this->config->getHealthCheckRetrySec();
            \sleep($retrySec);
        }

        return $processId;
    }

    /**
     * Stop the Mock Server process.
     *
     * @return bool Was stopping successful?
     */
    public function stop(): bool
    {
        return $this->processRunner->stop();
    }

    /**
     * Wrapper to add a custom installer.
     *
     * @param InstallerInterface $installer
     *
     * @return self
     */
    public function registerInstaller(InstallerInterface $installer): self
    {
        $this->installManager->registerInstaller($installer);

        return $this;
    }

    /**
     * Build an array of command arguments.
     *
     * @return array
     */
    private function getArguments(): array
    {
        $results = [];

        $consumer = escapeshellarg($this->config->getConsumer());
        $provider = escapeshellarg($this->config->getProvider());
        $pactDir = escapeshellarg($this->config->getPactDir());

        $results[] = 'service';
        $results[] = "--consumer={$consumer}";
        $results[] = "--provider={$provider}";
        $results[] = "--pact-dir={$pactDir}";
        $results[] = "--pact-file-write-mode={$this->config->getPactFileWriteMode()}";
        $results[] = "--host={$this->config->getHost()}";
        $results[] = "--port={$this->config->getPort()}";

        if ($this->config->hasCors()) {
            $results[] = '--cors=true';
        }

        if ($this->config->getPactSpecificationVersion() !== null) {
            $results[] = "--pact-specification-version={$this->config->getPactSpecificationVersion()}";
        }
<commit_msg>Update MockServer.php<commit_after><?php

namespace PhpPact\Standalone\MockService;

use Exception;
use GuzzleHttp\Exception\ConnectException;
use PhpPact\Http\GuzzleClient;
use PhpPact\Standalone\Exception\HealthCheckFailedException;
use PhpPact\Standalone\Installer\InstallManager;
use PhpPact\Standalone\Installer\Service\InstallerInterface;
use PhpPact\Standalone\MockService\Service\MockServerHttpService;
use PhpPact\Standalone\Runner\ProcessRunner;

/**
 * Ruby Standalone Mock Server Wrapper
 * Class MockServer.
 */
class MockServer
{
    /** @var MockServerConfig */
    private $config;

    /** @var InstallManager */
    private $installManager;

    /** @var MockServerHttpService */
    private $httpService;

    /** @var ProcessRunner */
    private $processRunner;

    /**
     * MockServer constructor.
     *
     * @param MockServerConfigInterface  $config
     * @param null|MockServerHttpService $httpService
     */
    public function __construct(MockServerConfigInterface $config, MockServerHttpService $httpService = null)
    {
        $this->config         = $config;
        $this->installManager = new InstallManager();

        if (!$httpService) {
            $this->httpService = new MockServerHttpService(new GuzzleClient(), $this->config);
        } else {
            $this->httpService = $httpService;
        }
    }

    /**
     * Start the Mock Server. Verify that it is running.
     *
     * @throws Exception
     *
     * @return int process ID of the started Mock Server
     */
    public function start(): int
    {
        $scripts = $this->installManager->install();

        $this->processRunner = new ProcessRunner($scripts->getMockService(), $this->getArguments());

        $processId =  $this->processRunner->run();

        $result = $this->verifyHealthCheck();
        if ($result) {
            $retrySec = $this->config->getHealthCheckRetrySec();
            \sleep($retrySec);
        }

        return $processId;
    }

    /**
     * Stop the Mock Server process.
     *
     * @return bool Was stopping successful?
     */
    public function stop(): bool
    {
        return $this->processRunner->stop();
    }

    /**
     * Wrapper to add a custom installer.
     *
     * @param InstallerInterface $installer
     *
     * @return self
     */
    public function registerInstaller(InstallerInterface $installer): self
    {
        $this->installManager->registerInstaller($installer);

        return $this;
    }

    /**
     * Build an array of command arguments.
     *
     * @return array
     */
    private function getArguments(): array
    {
        $results = [];

        $consumer = \escapeshellarg($this->config->getConsumer());
        $provider = \escapeshellarg($this->config->getProvider());
        $pactDir  = \escapeshellarg($this->config->getPactDir());

        $results[] = 'service';
        $results[] = "--consumer={$consumer}";
        $results[] = "--provider={$provider}";
        $results[] = "--pact-dir={$pactDir}";
        $results[] = "--pact-file-write-mode={$this->config->getPactFileWriteMode()}";
        $results[] = "--host={$this->config->getHost()}";
        $results[] = "--port={$this->config->getPort()}";

        if ($this->config->hasCors()) {
            $results[] = '--cors=true';
        }

        if ($this->config->getPactSpecificationVersion() !== null) {
            $results[] = "--pact-specification-version={$this->config->getPactSpecificationVersion()}";
        }

{'fraction_non_alphanumeric': 0.14391988555078683, 'fraction_numerical': 0.0, 'mean_word_length': 2.598044261451364, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 77, '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': '2488528', 'n_tokens_mistral': 1998, 'n_tokens_neox': 1973, 'n_words': 385}