<commit_before><?php
/**
 * Created by PhpStorm.
 * User: amblin
 * Date: 05/07/15
 * Time: 15:21
 */

namespace phparsenal\fastforward\Command;


use phparsenal\fastforward\Model\Setting;

class Set extends AbstractCommand implements CommandInterface
{

    protected $name = 'set';

    /**
     * @param array $argv
     */
    public function run($argv)
    {
        $this->prepareArguments();
        try {
            $args = $this->cli->arguments;
            $args->parse();
            $key = $args->get('key');
            $value = $args->get('value');

            if ($key !== null && $value !== null) {
                $this->set($key, $value);
                return;
            }
            throw new \Exception();
        } catch (\Exception $e) {
            $this->cli->arguments->usage($this->cli, $argv);
        }
    }

    private function prepareArguments()
    {
        $this->cli->arguments->add(
            array(
                'set' => array(
                    'description' => 'Command to set a variable',
                    'required' => true
                ),
                'key' => array(
                    'description' => 'Name or key of the setting',
                ),
                'value' => array(
                    'description' => 'Value to be set',
                )
            )
        );
    }

    /**
     * @param string $key
     * @param string $value
     */
    public function set($key, $value)
    {
        $oldValue = $this->client->get($key);
        if ($oldValue === null) {
            $this->cli
                ->out("Inserting new setting:")
                ->out("$key = $value");
        } elseif ($oldValue !== $value) {
            $this->cli
                ->out("Changing setting:")
                ->out("$key = $oldValue --> <bold>$value</bold>");
        } else {
            $this->cli
                ->out("Setting already up-to-date:")
                ->out("$key = $value");
        }
        $this->client->set($key, $value);
    }
    
}<commit_msg>Add option --list -l to Set<commit_after><?php
/**
 * Created by PhpStorm.
 * User: amblin
 * Date: 05/07/15
 * Time: 15:21
 */

namespace phparsenal\fastforward\Command;


use phparsenal\fastforward\Model\Setting;

class Set extends AbstractCommand implements CommandInterface
{

    protected $name = 'set';

    /**
     * @param array $argv
     */
    public function run($argv)
    {
        $this->prepareArguments();
        try {
            $args = $this->cli->arguments;
            $args->parse();
            $key = $args->get('key');
            $value = $args->get('value');

            if ($key !== null && $value !== null) {
                $this->set($key, $value);
                return;
            }
            if ($args->defined('list')) {
                $this->listAll();
                return;
            }
            throw new \Exception();
        } catch (\Exception $e) {
            $this->cli->arguments->usage($this->cli, $argv);
        }
    }

    private function prepareArguments()
    {
        $this->cli->arguments->add(
            array(
                'list' => array(
                    'prefix' => 'l',
                    'longPrefix' => 'list',
                    'description' => 'Show a list of all current settings',
                    'noValue' => true
                ),
                'set' => array(
                    'description' => 'Command to set a variable',
                    'required' => true
                ),
                'key' => array(
                    'description' => 'Name or key of the setting',
                ),
                'value' => array(
                    'description' => 'Value to be set',
                )
            )
        );
    }

    /**
     * @param string $key
     * @param string $value
     */
    public function set($key, $value)
    {
        $oldValue = $this->client->get($key);
        if ($oldValue === null) {
            $this->cli
                ->out("Inserting new setting:")
                ->out("$key = $value");
        } elseif ($oldValue !== $value) {
            $this->cli
                ->out("Changing setting:")
                ->out("$key = $oldValue --> <bold>$value</bold>");
        } else {
            $this->cli
                ->out("Setting already up-to-date:")
                ->out("$key = $value");
        }
        $this->client->set($key, $value);
    }

    public function listAll()
    {
        $settings = Setting::select()->orderAsc('key')->all();
        foreach ($settings as $setting) {
            $this->cli->out('"' . $setting->key . '" "' . $setting->value . '"');
        }
    }
}
{'fraction_non_alphanumeric': 0.1631265930331351, 'fraction_numerical': 0.004248088360237893, 'mean_word_length': 1.307202351788339, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 90, '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': '5858707', 'n_tokens_mistral': 1344, 'n_tokens_neox': 1272, 'n_words': 285}