<commit_before><?php

namespace Platformsh\Cli\Service;

use Platformsh\Cli\SshCert\Certifier;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;

class SshDiagnostics
{
    const _SSH_ERROR_EXIT_CODE = 255;

    private $ssh;
    private $sshKey;
    private $certifier;
    private $stdErr;
    private $api;
    private $config;

    private $connectionTestResult;

    public function __construct(Ssh $ssh, OutputInterface $output, Certifier $certifier, SshKey $sshKey, Api $api, Config $config)
    {
        $this->ssh = $ssh;
        $this->stdErr = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output;
        $this->sshKey = $sshKey;
        $this->certifier = $certifier;
        $this->api = $api;
        $this->config = $config;
    }

    /**
     * Checks whether the SSH connection fails due to MFA requirements.
     *
     * @param string $uri
     * @param Process|null $failedProcess
     *
     * @return bool
     */
    private function connectionFailedDueToMFA($uri, $failedProcess = null)
    {
        if (($cert = $this->certifier->getExistingCertificate()) && $cert->hasMfa()) {
            // MFA is verified.
            return false;
        }
        $failedProcess = $failedProcess ?: $this->testConnection($uri);

        return stripos($failedProcess->getErrorOutput(), 'reason: access requires MFA') !== false;
    }

    /**
     * Checks if SSH authentication succeeded, even if the connection failed.
     *
     * This occurs if the SSH key or certificate was correct, but the requested
     * service does not exist or the user does not have access to it.
     *
     * @param string $uri
     * @param Process|null $failedProcess
     *
     * @return bool
     */
    private function authenticationSucceded($uri, $failedProcess = null)
    {
        $failedProcess = $failedProcess ?: $this->testConnection($uri);
        $stdErr = $failedProcess->getErrorOutput();

        return stripos($stdErr, "reason: service doesn't exist") !== false
            || stripos($stdErr, 'reason: service not found') !== false
            || stripos($stdErr, 'you successfully authenticated, but') !== false;
    }

    /**
     * Tests the SSH connection (and caches the result).
     *
     * @param string $uri
     * @param bool   $reset
     *
     * @return Process
     *   A process (already run) that tested the SSH connection.
     */
    private function testConnection($uri, $reset = false)
    {
        if (!$reset && isset($this->connectionTestResult)) {
            return $this->connectionTestResult;
        }
        $this->stdErr->writeln('Making test connection to diagnose SSH errors', OutputInterface::VERBOSITY_VERBOSE);
        $process = new Process($this->ssh->getSshCommand([], $uri, 'exit'));
        $process->run();
        return $this->connectionTestResult = $process;
    }

    /**
     * Hackily finds a host from an SSH URI.
     *
     * @param string $uri
     *
     * @return string|false
     */
    private function getHost($uri)
    {
        // Parse the SSH URI to get the hostname.
        if (\strpos($uri, '@') !== false) {
            list(, $uri) = \explode('@', $uri, 2);
        }
        if (\strpos($uri, '://') !== false) {
            list(, $uri) = \explode('://', $uri, 2);
        }
        if (\strpos($uri, ':') !== false) {
            list($uri, ) = \explode(':', $uri, 2);
        }
        return \parse_url('ssh://' . $uri, PHP_URL_HOST);
    }
<commit_msg>Move test connection messages to debug verbosity<commit_after><?php

namespace Platformsh\Cli\Service;

use Platformsh\Cli\SshCert\Certifier;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;

class SshDiagnostics
{
    const _SSH_ERROR_EXIT_CODE = 255;

    private $ssh;
    private $sshKey;
    private $certifier;
    private $stdErr;
    private $api;
    private $config;

    private $connectionTestResult;

    public function __construct(Ssh $ssh, OutputInterface $output, Certifier $certifier, SshKey $sshKey, Api $api, Config $config)
    {
        $this->ssh = $ssh;
        $this->stdErr = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output;
        $this->sshKey = $sshKey;
        $this->certifier = $certifier;
        $this->api = $api;
        $this->config = $config;
    }

    /**
     * Checks whether the SSH connection fails due to MFA requirements.
     *
     * @param string $uri
     * @param Process|null $failedProcess
     *
     * @return bool
     */
    private function connectionFailedDueToMFA($uri, $failedProcess = null)
    {
        if (($cert = $this->certifier->getExistingCertificate()) && $cert->hasMfa()) {
            // MFA is verified.
            return false;
        }
        $failedProcess = $failedProcess ?: $this->testConnection($uri);

        return stripos($failedProcess->getErrorOutput(), 'reason: access requires MFA') !== false;
    }

    /**
     * Checks if SSH authentication succeeded, even if the connection failed.
     *
     * This occurs if the SSH key or certificate was correct, but the requested
     * service does not exist or the user does not have access to it.
     *
     * @param string $uri
     * @param Process|null $failedProcess
     *
     * @return bool
     */
    private function authenticationSucceded($uri, $failedProcess = null)
    {
        $failedProcess = $failedProcess ?: $this->testConnection($uri);
        $stdErr = $failedProcess->getErrorOutput();

        return stripos($stdErr, "reason: service doesn't exist") !== false
            || stripos($stdErr, 'reason: service not found') !== false
            || stripos($stdErr, 'you successfully authenticated, but') !== false;
    }

    /**
     * Tests the SSH connection (and caches the result).
     *
     * @param string $uri
     * @param bool   $reset
     *
     * @return Process
     *   A process (already run) that tested the SSH connection.
     */
    private function testConnection($uri, $reset = false)
    {
        if (!$reset && isset($this->connectionTestResult)) {
            return $this->connectionTestResult;
        }
        $this->stdErr->writeln('Making test connection to diagnose SSH errors', OutputInterface::VERBOSITY_DEBUG);
        $process = new Process($this->ssh->getSshCommand([], $uri, 'exit'));
        $process->run();
        $this->stdErr->writeln('Test connection complete', OutputInterface::VERBOSITY_DEBUG);
        return $this->connectionTestResult = $process;
    }

    /**
     * Hackily finds a host from an SSH URI.
     *
     * @param string $uri
     *
     * @return string|false
     */
    private function getHost($uri)
    {
        // Parse the SSH URI to get the hostname.
        if (\strpos($uri, '@') !== false) {
            list(, $uri) = \explode('@', $uri, 2);
        }
        if (\strpos($uri, '://') !== false) {
            list(, $uri) = \explode('://', $uri, 2);
        }
        if (\strpos($uri, ':') !== false) {
            list($uri, ) = \explode(':', $uri, 2);
        }
        return \parse_url('ssh://' . $uri, PHP_URL_HOST);
    }

{'fraction_non_alphanumeric': 0.13608332191311498, 'fraction_numerical': 0.0016445114430587912, 'mean_word_length': 2.480209823557463, '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': '6414094', 'n_tokens_mistral': 2175, 'n_tokens_neox': 2098, 'n_words': 566}