<commit_before><?php

namespace Terminus\Models;

use GuzzleHttp\TransferStats as TransferStats;
use Terminus\Exceptions\TerminusException;
use Terminus\Config;
use Terminus\Collections\Backups;
use Terminus\Collections\Bindings;
use Terminus\Collections\Commits;
use Terminus\Collections\Hostnames;
use Terminus\Collections\Workflows;

class Environment extends TerminusModel
{
  /**
   * @var Backups
   */
    public $backups;
  /**
   * @var Bindings
   */
    public $bindings;
  /**
   * @var Commits
   */
    public $commits;
  /**
   * @var Hostnames
   */
    public $hostnames;
  /**
   * @var Site
   */
    public $site;
  /**
   * @var Workflows
   */
    public $workflows;

  /**
   * Object constructor
   *
   * @param object $attributes Attributes of this model
   * @param array  $options    Options with which to configure this model
   */
    public function __construct($attributes, array $options = [])
    {
        parent::__construct($attributes, $options);
        $this->site = $options['collection']->site;
        $options = ['environment' => $this,];
        $this->backups   = new Backups($options);
        $this->bindings  = new Bindings($options);
        $this->commits   = new Commits($options);
        $this->hostnames = new Hostnames($options);
        $this->workflows = new Workflows($options);
    }

  /**
   * Apply upstream updates
   *
   * @param boolean $updatedb True to run update.php
   * @param boolean $xoption  True to automatically resolve merge conflicts
   * @return Workflow
   */
    public function applyUpstreamUpdates($updatedb = true, $xoption = false)
    {
        $params = ['updatedb' => $updatedb, 'xoption' => $xoption];
        $workflow = $this->workflows->create('apply_upstream_updates', compact('params'));
        return $workflow;
    }

  /**
   * Gives cacheserver connection info for this environment
   *
   * @return array
   */
    public function cacheserverConnectionInfo()
    {
        $cacheserver_binding = (array)$this->bindings->getByType('cacheserver');
        if (!empty($cacheserver_binding)) {
            do {
                $next_binding = array_shift($cacheserver_binding);
                if (is_null($next_binding)) {
                    break;
                }
                $cache_binding = $next_binding;
            } while (!is_null($cache_binding) && $cache_binding->get('environment') != $this->id);

            $password = $cache_binding->get('password');
            $hostname = $cache_binding->get('host');
            $port = $cache_binding->get('port');
            $url = "redis://pantheon:$password@$hostname:$port";
            $command = "redis-cli -h $hostname -p $port -a $password";
            $info = [
            'password' => <PASSWORD>,
            'host' => $hostname,
            'port' => $port,
            'url' => $url,
            'command' => $command,
            ];
            return $info;
        }
    }

  /**
   * Changes connection mode
   *
   * @param string $value Connection mode, "git" or "sftp"
   * @return Workflow|string
   */
    public function changeConnectionMode($value)
    {
        $current_mode = $this->info('connection_mode');
        if ($value == $current_mode) {
            $reply = "The connection mode is already set to $value.";
            return $reply;
        }
        switch ($value) {
            case 'git':
                $workflow_name = 'enable_git_mode';
                break;
            case 'sftp':
                $workflow_name = 'enable_on_server_development';
                break;
        }<commit_msg>Ensure Environment::cacheserverConnectionInfo() always returns a value<commit_after><?php

namespace Terminus\Models;

use GuzzleHttp\TransferStats as TransferStats;
use Terminus\Collections\Backups;
use Terminus\Collections\Bindings;
use Terminus\Collections\Commits;
use Terminus\Collections\Hostnames;
use Terminus\Collections\Workflows;
use Terminus\Config;
use Terminus\Exceptions\TerminusException;

class Environment extends TerminusModel
{
  /**
   * @var Backups
   */
    public $backups;
  /**
   * @var Bindings
   */
    public $bindings;
  /**
   * @var Commits
   */
    public $commits;
  /**
   * @var Hostnames
   */
    public $hostnames;
  /**
   * @var Site
   */
    public $site;
  /**
   * @var Workflows
   */
    public $workflows;

  /**
   * Object constructor
   *
   * @param object $attributes Attributes of this model
   * @param array  $options    Options with which to configure this model
   */
    public function __construct($attributes, array $options = [])
    {
        parent::__construct($attributes, $options);
        $this->site = $options['collection']->site;
        $options = ['environment' => $this,];
        $this->backups   = new Backups($options);
        $this->bindings  = new Bindings($options);
        $this->commits   = new Commits($options);
        $this->hostnames = new Hostnames($options);
        $this->workflows = new Workflows($options);
    }

  /**
   * Apply upstream updates
   *
   * @param boolean $updatedb True to run update.php
   * @param boolean $xoption  True to automatically resolve merge conflicts
   * @return Workflow
   */
    public function applyUpstreamUpdates($updatedb = true, $xoption = false)
    {
        $params = ['updatedb' => $updatedb, 'xoption' => $xoption];
        $workflow = $this->workflows->create('apply_upstream_updates', compact('params'));
        return $workflow;
    }

  /**
   * Gives cacheserver connection info for this environment
   *
   * @return array
   */
    public function cacheserverConnectionInfo()
    {
        $info = [];
        $cacheserver_binding = (array)$this->bindings->getByType('cacheserver');
        if (!empty($cacheserver_binding)) {
            do {
                $next_binding = array_shift($cacheserver_binding);
                if (is_null($next_binding)) {
                    break;
                }
                $cache_binding = $next_binding;
            } while (!is_null($cache_binding) && $cache_binding->get('environment') != $this->id);

            $password = $cache_binding->get('password');
            $hostname = $cache_binding->get('host');
            $port = $cache_binding->get('port');
            $url = "redis://pantheon:$password@$hostname:$port";
            $command = "redis-cli -h $hostname -p $port -a $password";
            $info = [
            'password' => <PASSWORD>,
            'host' => $hostname,
            'port' => $port,
            'url' => $url,
            'command' => $command,
            ];
        }
        return $info;
    }

  /**
   * Changes connection mode
   *
   * @param string $value Connection mode, "git" or "sftp"
   * @return Workflow|string
   */
    public function changeConnectionMode($value)
    {
        $current_mode = $this->info('connection_mode');
        if ($value == $current_mode) {
            $reply = "The connection mode is already set to $value.";
            return $reply;
        }
        switch ($value) {
            case 'git':
                $workflow_name = 'enable_git_mode';
                break;
            case 'sftp':
                $workflow_name = 'enable_on_server_development';
                break;
        }
{'fraction_non_alphanumeric': 0.13319587628865978, 'fraction_numerical': 0.0, 'mean_word_length': 2.2266075388026607, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 55, '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': '2346401', 'n_tokens_mistral': 2119, 'n_tokens_neox': 2099, 'n_words': 472}