<commit_before><?php

/**
 * Copyright (C) 2016-18 <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * @author <NAME> <https://benjamin.heisig.name/>
 * @copyright Copyright (C) 2016-17 <NAME>
 * @license http://www.gnu.org/licenses/agpl-3.0 GNU Affero General Public License (AGPL)
 * @link https://github.com/bheisig/i-doit-cli
 */

declare(strict_types=1);

namespace bheisig\idoitcli\Command;

use bheisig\idoitcli\Service\Attribute;

/**
 * Command "save"
 */
class Save extends Command {

    /**
     * @var int
     */
    protected $objectTypeID;

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

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

    /**
     * @var int
     */
    protected $objectID;

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

    /**
     * @var array
     */
    protected $object;

    /**
     * @var int
     */
    protected $categoryID;

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

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

    /**
     * @var array
     */
    protected $categoryAttributes;

    /**
     * @var array Attribute key as key and its value as value
     */
    protected $collectedAttributes = [];

    /**
     * @var int
     */
    protected $entryID;

    /**
     * @var array
     */
    protected $entry;

    /**
     * @var array Indexed array of arrays:
     * [
     *     'categoryConstant' => 'C__CATG__MODEL',
     *     'categoryTitle' => 'Model',
     *     'categoryID' => 42,
     *     'attributeKey' => 'serial',
     *     'attributeTitle' => 'Serial number',
     *     'value' => 'abc123'
     * ]
     */
    protected $template = [];

    /**
     * Process some routines before execution
     *
     * @return self Returns itself
     *
     * @throws \Exception on error
     */
    public function setup(): Command {
        parent::setup();

        if ($this->cache->isCached() === false) {
            throw new \RuntimeException(sprintf(
                'Unsufficient data. Please run "%s cache" first.',
                $this->config['composer']['extra']['name']
            ), 500);
        }

        return $this;
    }

    /**
     * Execute command
     *
     * @return self Returns itself
     *
     * @throws \Exception on error
     */
    public function execute(): self {
        $this->log->info($this->getDescription());

        if (array_key_exists(0, $this->config['arguments'])) {
            $this
                ->parseQuery($this->config['arguments'][0]);
        }

        $this
            ->parseAttributes($this->config['options'])
            ->preloadEntry()
            ->analyzeCollectedData()
            ->interviewUser();

        if (!$this->hasCategory() &&
            $this->hasObjectType() &&
            !$this->userInteraction->isInteractive()) {
            $this->loadTemplate();
        }

        if ($this->hasTemplate()) {
            $decision = $this->userInteraction->askYesNo(
                'Add more attributes?'
            );

            if ($decision === true) {
                $this->applyTemplate();
            }
        }

        $this->save();

        return $this;
    }

    /**
     * Try to identify what the user wants to do
     *
     * @param string $query Query
     *
     * @return self Returns itself
     *
     * @throws \Exception on error
     */
    protected function parseQuery(string $query): self {<commit_msg>Fix loading template in interactive mode<commit_after><?php

/**
 * Copyright (C) 2016-18 <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * @author <NAME> <https://benjamin.heisig.name/>
 * @copyright Copyright (C) 2016-17 <NAME>
 * @license http://www.gnu.org/licenses/agpl-3.0 GNU Affero General Public License (AGPL)
 * @link https://github.com/bheisig/i-doit-cli
 */

declare(strict_types=1);

namespace bheisig\idoitcli\Command;

use bheisig\idoitcli\Service\Attribute;

/**
 * Command "save"
 */
class Save extends Command {

    /**
     * @var int
     */
    protected $objectTypeID;

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

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

    /**
     * @var int
     */
    protected $objectID;

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

    /**
     * @var array
     */
    protected $object;

    /**
     * @var int
     */
    protected $categoryID;

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

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

    /**
     * @var array
     */
    protected $categoryAttributes;

    /**
     * @var array Attribute key as key and its value as value
     */
    protected $collectedAttributes = [];

    /**
     * @var int
     */
    protected $entryID;

    /**
     * @var array
     */
    protected $entry;

    /**
     * @var array Indexed array of arrays:
     * [
     *     'categoryConstant' => 'C__CATG__MODEL',
     *     'categoryTitle' => 'Model',
     *     'categoryID' => 42,
     *     'attributeKey' => 'serial',
     *     'attributeTitle' => 'Serial number',
     *     'value' => 'abc123'
     * ]
     */
    protected $template = [];

    /**
     * Process some routines before execution
     *
     * @return self Returns itself
     *
     * @throws \Exception on error
     */
    public function setup(): Command {
        parent::setup();

        if ($this->cache->isCached() === false) {
            throw new \RuntimeException(sprintf(
                'Unsufficient data. Please run "%s cache" first.',
                $this->config['composer']['extra']['name']
            ), 500);
        }

        return $this;
    }

    /**
     * Execute command
     *
     * @return self Returns itself
     *
     * @throws \Exception on error
     */
    public function execute(): self {
        $this->log->info($this->getDescription());

        if (array_key_exists(0, $this->config['arguments'])) {
            $this
                ->parseQuery($this->config['arguments'][0]);
        }

        $this
            ->parseAttributes($this->config['options'])
            ->preloadEntry()
            ->analyzeCollectedData()
            ->interviewUser();

        if (!$this->hasCategory() &&
            $this->hasObjectType() &&
            $this->userInteraction->isInteractive()) {
            $this->loadTemplate();
        }

        if ($this->hasTemplate()) {
            $decision = $this->userInteraction->askYesNo(
                'Add more attributes?'
            );

            if ($decision === true) {
                $this->applyTemplate();
            }
        }

        $this->save();

        return $this;
    }

    /**
     * Try to identify what the user wants to do
     *
     * @param string $query Query
     *
     * @return self Returns itself
     *
     * @throws \Exception on error
     */
    protected function parseQuery(string $query): self {
{'fraction_non_alphanumeric': 0.13288996372430473, 'fraction_numerical': 0.006287787182587666, 'mean_word_length': 1.9021052631578947, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 73, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, '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': '3353283', 'n_tokens_mistral': 2401, 'n_tokens_neox': 2296, 'n_words': 651}