<commit_before><?php

namespace ShopwareCli\Services;

use ShopwareCli\Config;
use ShopwareCli\Services\PathProvider\PathProvider;

/**
 *
 *
 * Class GitIdentityEnvironment
 * @package ShopwareCli\Services
 */
class GitIdentityEnvironment
{

    protected $wrapperFileName = 'ssh-as.sh';

    protected $sshAliasTemplate = <<<'EOF'
#!/bin/bash
set -e
set -u

ssh -i $SSH_KEYFILE $@
EOF;
    /**
     * @var PathProvider
     */
    private $pathProvider;
    /**
     * @var \ShopwareCli\Config
     */
    private $config;

    public function __construct(PathProvider $pathProvider, Config $config)
    {
        $this->pathProvider = $pathProvider;
        $this->config = $config;
    }

    /**
     * Will return the path to the custom SSH key. Will return null if no
     * custom key is configured
     *
     * @return null|string
     * @throws \RuntimeException
     */
    private function getCustomKey()
    {
        $packageKey = $this->pathProvider->getCliToolPath() . '/assets/ssh.key';

        if (file_exists($packageKey)) {
            return $packageKey;
        }

        if (isset($config['sshKey'])) {
            $keyPath = $config['sshKey'];
            if (!file_exists($keyPath)) {
                throw new \RuntimeException("Could not find ssh key $keyPath");
            }

            return $keyPath;
        }

        return null;
    }

    /**
     * Return path of the git wrapper file. If it doesn't exist, it will be created<commit_msg>Copy ssh key if required<commit_after><?php

namespace ShopwareCli\Services;

use ShopwareCli\Config;
use ShopwareCli\Services\PathProvider\PathProvider;

/**
 *
 *
 * Class GitIdentityEnvironment
 * @package ShopwareCli\Services
 */
class GitIdentityEnvironment
{

    protected $wrapperFileName = 'ssh-as.sh';
    protected $keyFileName = 'ssh.key';

    protected $sshAliasTemplate = <<<'EOF'
#!/bin/bash
set -e
set -u

ssh -i $SSH_KEYFILE $@
EOF;
    /**
     * @var PathProvider
     */
    private $pathProvider;
    /**
     * @var \ShopwareCli\Config
     */
    private $config;

    public function __construct(PathProvider $pathProvider, Config $config)
    {
        $this->pathProvider = $pathProvider;
        $this->config = $config;
    }

    /**
     * Will return the path to the custom SSH key. Will return null if no
     * custom key is configured
     *
     * @return null|string
     * @throws \RuntimeException
     */
    private function getCustomKey()
    {
        $packageKey = $this->pathProvider->getCliToolPath() . '/assets/ssh.key';

        $dir = $this->pathProvider->getRuntimeDir() . '/sw-cli-tools/';
        $sshKeyFile = $dir . $this->keyFileName;

        if (file_exists($sshKeyFile) || $this->writeSshKey($dir, $packageKey)) {
            return $sshKeyFile;
        }

        if (isset($config['sshKey'])) {
            $keyPath = $config['sshKey'];
            if (!file_exists($keyPath)) {
                throw new \RuntimeException("Could not find ssh key $keyPath");
            }

            return $keyPath;
        }

        return null;
    }

    private function writeSshKey($dir, $sshKeyFile)
    {
        if (!is_dir($dir)) {
            mkdir($dir, 0700, true);
        }

        $filename = $dir . $this->keyFileName;
        file_put_contents($filename, file_get_contents($sshKeyFile));
        chmod($filename, 0700);

        return file_exists($filename);
    }

    /**
     * Return path of the git wrapper file. If it doesn't exist, it will be created
{'fraction_non_alphanumeric': 0.12589209249214958, 'fraction_numerical': 0.0022837567799029405, 'mean_word_length': 2.2901408450704226, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 16, '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': '7377690', 'n_tokens_mistral': 1088, 'n_tokens_neox': 1019, 'n_words': 254}