<commit_before><?php
namespace ShinyDeploy\Domain;

use RuntimeException;
use ShinyDeploy\Core\Domain;
use ShinyDeploy\Core\Responder;
use ShinyDeploy\Domain\Database\Repositories;
use ShinyDeploy\Domain\Database\Servers;
use ShinyDeploy\Domain\Server\Server;

class Deployment extends Domain
{
    /** @var Server $server */
    protected $server;

    /** @var Repository $repository */
    protected $repository;

    /** @var LogResponder $logResponder */
    protected $logResponder;

    /** @var array $changedFiles */
    protected $changedFiles = [];

    /** @var string $encryptionKey */
    protected $encryptionKey;


    public function setEncryptionKey($encryptionKey)
    {
        if (empty($encryptionKey)) {
            throw new \InvalidArgumentException('Encryption key can not be empty.');
        }
        $this->encryptionKey = $encryptionKey;
    }

    public function init(array $data)
    {
        $this->data = $data;
        $servers = new Servers($this->config, $this->logger);
        $servers->setEnryptionKey($this->encryptionKey);
        $repositories = new Repositories($this->config, $this->logger);
        $repositories->setEnryptionKey($this->encryptionKey);
        $this->server = $servers->getServer($data['server_id']);
        $this->repository = $repositories->getRepository($data['repository_id']);
    }

    /**
     * Setter for the websocket log repsonder.
     *
     * @param Responder $logResponder
     */
    public function setLogResponder(Responder $logResponder)
    {
        $this->logResponder = $logResponder;
    }

    /**
     * Returns list of changed files.
     *
     * @return array
     */
    public function getChangedFiles()
    {
        return $this->changedFiles;
    }

    /**
     * Executes an actual deployment.
     *
     * @param bool $listMode If true changed files are only listed but not acutually deployed.
     * @throws RuntimeException
     * @return bool
     */
    public function deploy($listMode = false)
    {
        if (empty($this->data)) {
            throw new RuntimeException('Deployment data not found. Initialization missing?');
        }
        if (empty($this->server)) {
            throw new RuntimeException('Server object not found.');
        }
        if (empty($this->repository)) {
            throw new RuntimeException('Repository object not found');
        }

        $this->logResponder->log('Checking prerequisites...');
        if ($this->checkPrerequisites() === false) {
            $this->logResponder->error('Prerequisites check failed. Aborting job.');
            return false;
        }

        $this->logResponder->log('Preparing local repository...');
        if ($this->prepareRepository() === false) {
            $this->logResponder->error('Preparation of local repository failed. Aborting job.');
            return false;
        }

        if ($listMode === false) {
            $this->logResponder->log('Running tasks...');
            if ($this->runTasks('before') === false) {
                $this->logResponder->error('Running tasks failed. Aborting job.');
                return false;
            }
        }

        $this->logResponder->log('Estimating remote revision...');
        $remoteRevision = $this->getRemoteRevision();
        if ($remoteRevision === false) {
            $this->logResponder->error('Could not estimate remote revision. Aborting job.');
            return false;
        }

        $this->logResponder->log('Switching branch...');
        if ($this->switchBranch() === false) {
            $this->logResponder->error('Could not swtich to selected branch. Aborting job.');
            return false;
        }

        $this->logResponder->log('Estimating local revision...');
        $localRevision = $this->getLocalRevision();
        if ($localRevision === false) {
            $this->logResponder->error('Could not estimate local revision. Aborting job.');
            return false;
        }

        // If remote server is up to date we can stop right here:
        if ($localRevision === $remoteRevision) {
            if ($listMode === false) {
                $this->logResponder->info('Remote server is aleady up to date.');
            }
            return true;
        }

        $this->logResponder->log('Collecting changed files...');
        $changedFiles = $this->getChangedFilesList($localRevision, $remoteRevision);
        if (empty($changedFiles)) {
            $this->logResponder->error('Could not estimate changed files.');
            return false;<commit_msg>Switch git branches before running possible git pull<commit_after><?php
namespace ShinyDeploy\Domain;

use RuntimeException;
use ShinyDeploy\Core\Domain;
use ShinyDeploy\Core\Responder;
use ShinyDeploy\Domain\Database\Repositories;
use ShinyDeploy\Domain\Database\Servers;
use ShinyDeploy\Domain\Server\Server;

class Deployment extends Domain
{
    /** @var Server $server */
    protected $server;

    /** @var Repository $repository */
    protected $repository;

    /** @var LogResponder $logResponder */
    protected $logResponder;

    /** @var array $changedFiles */
    protected $changedFiles = [];

    /** @var string $encryptionKey */
    protected $encryptionKey;


    public function setEncryptionKey($encryptionKey)
    {
        if (empty($encryptionKey)) {
            throw new \InvalidArgumentException('Encryption key can not be empty.');
        }
        $this->encryptionKey = $encryptionKey;
    }

    public function init(array $data)
    {
        $this->data = $data;
        $servers = new Servers($this->config, $this->logger);
        $servers->setEnryptionKey($this->encryptionKey);
        $repositories = new Repositories($this->config, $this->logger);
        $repositories->setEnryptionKey($this->encryptionKey);
        $this->server = $servers->getServer($data['server_id']);
        $this->repository = $repositories->getRepository($data['repository_id']);
    }

    /**
     * Setter for the websocket log repsonder.
     *
     * @param Responder $logResponder
     */
    public function setLogResponder(Responder $logResponder)
    {
        $this->logResponder = $logResponder;
    }

    /**
     * Returns list of changed files.
     *
     * @return array
     */
    public function getChangedFiles()
    {
        return $this->changedFiles;
    }

    /**
     * Executes an actual deployment.
     *
     * @param bool $listMode If true changed files are only listed but not acutually deployed.
     * @throws RuntimeException
     * @return bool
     */
    public function deploy($listMode = false)
    {
        if (empty($this->data)) {
            throw new RuntimeException('Deployment data not found. Initialization missing?');
        }
        if (empty($this->server)) {
            throw new RuntimeException('Server object not found.');
        }
        if (empty($this->repository)) {
            throw new RuntimeException('Repository object not found');
        }

        $this->logResponder->log('Checking prerequisites...');
        if ($this->checkPrerequisites() === false) {
            $this->logResponder->error('Prerequisites check failed. Aborting job.');
            return false;
        }

        $this->logResponder->log('Switching branch...');
        if ($this->switchBranch() === false) {
            $this->logResponder->error('Could not swtich to selected branch. Aborting job.');
            return false;
        }

        $this->logResponder->log('Preparing local repository...');
        if ($this->prepareRepository() === false) {
            $this->logResponder->error('Preparation of local repository failed. Aborting job.');
            return false;
        }

        if ($listMode === false) {
            $this->logResponder->log('Running tasks...');
            if ($this->runTasks('before') === false) {
                $this->logResponder->error('Running tasks failed. Aborting job.');
                return false;
            }
        }

        $this->logResponder->log('Estimating remote revision...');
        $remoteRevision = $this->getRemoteRevision();
        if ($remoteRevision === false) {
            $this->logResponder->error('Could not estimate remote revision. Aborting job.');
            return false;
        }

        $this->logResponder->log('Estimating local revision...');
        $localRevision = $this->getLocalRevision();
        if ($localRevision === false) {
            $this->logResponder->error('Could not estimate local revision. Aborting job.');
            return false;
        }

        // If remote server is up to date we can stop right here:
        if ($localRevision === $remoteRevision) {
            if ($listMode === false) {
                $this->logResponder->info('Remote server is aleady up to date.');
            }
            return true;
        }

        $this->logResponder->log('Collecting changed files...');
        $changedFiles = $this->getChangedFilesList($localRevision, $remoteRevision);
        if (empty($changedFiles)) {
            $this->logResponder->error('Could not estimate changed files.');
            return false;
{'fraction_non_alphanumeric': 0.13286789571288316, 'fraction_numerical': 0.0, 'mean_word_length': 2.355783308931186, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 115, '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': '806347', 'n_tokens_mistral': 2528, 'n_tokens_neox': 2402, 'n_words': 588}