<commit_before><?php

/**
 * This file is part of tenside/core.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * This project is provided in good faith and hope to be usable by anyone.
 *
 * @package    tenside/core
 * @author     <NAME> <<EMAIL>>
 * @copyright  2015 <NAME> <<EMAIL>>
 * @license    https://github.com/tenside/core/blob/master/LICENSE MIT
 * @link       https://github.com/tenside/core
 * @filesource
 */

namespace Tenside\Task;

use Composer\IO\ConsoleIO;
use Composer\IO\IOInterface;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Tenside\Util\JsonArray;

/**
 * Abstract base class for tasks.
 */
abstract class Task
{
    /**
     * The type of the task.
     */
    const SETTING_TYPE = 'type';

    /**
     * The id of the task.
     */
    const SETTING_ID = 'id';

    /**
     * This state determines that the task is still running.
     */
    const STATE_RUNNING = 'RUNNING';

    /**
     * This state determines that the task has been finished.
     */
    const STATE_FINISHED = 'FINISHED';

    /**
     * This state determines that the task has been finished with errors.
     */
    const STATE_ERROR = 'ERROR';

    /**
     * The task file to write to.
     *
     * @var JsonArray
     */
    protected $file;

    /**
     * The input/output handler.
     *
     * @var IOInterface
     */
    private $inputOutput;

    /**
     * Task constructor.
     *
     * @param JsonArray $file The json file to write to.
     */
    public function __construct(JsonArray $file)
    {
        $this->file = $file;
    }

    /**
     * Retrieve the task id.
     *
     * @return string
     */
    public function getId()
    {
        return $this->file->get('id');
    }

    /**
     * Retrieve the current output.
     *
     * @return string
     */
    public function getOutput()
    {
        return $this->file->get('output');
    }

    /**
     * Retrieve the task type name.
     *
     * @return string
     */
    abstract public function getType();

    /**
     * Perform the task.
     *
     * @return void
     */
    abstract public function perform();

    /**
     * Add some output.
     *
     * @param string $string The output string to append to the output.
     *
     * @return void
     */
    public function addOutput($string)
    {
        $this->file->set('output', $this->getOutput() . $string);
    }

    /**
     * Retrieve the IO interface.
     *
     * @return IOInterface
     */
    public function getIO()
    {
        if (!isset($this->inputOutput)) {
            $this->inputOutput = new ConsoleIO($this->getInput(), new TaskOutput($this), new HelperSet([]));
        }

        return $this->inputOutput;
    }

    /**
     * Retrieve the current status of a task.<commit_msg>Extract logging of tasks into logfile<commit_after><?php

/**
 * This file is part of tenside/core.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * This project is provided in good faith and hope to be usable by anyone.
 *
 * @package    tenside/core
 * @author     <NAME> <<EMAIL>>
 * @copyright  2015 <NAME> <<EMAIL>>
 * @license    https://github.com/tenside/core/blob/master/LICENSE MIT
 * @link       https://github.com/tenside/core
 * @filesource
 */

namespace Tenside\Task;

use Composer\IO\ConsoleIO;
use Composer\IO\IOInterface;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Tenside\Util\JsonArray;

/**
 * Abstract base class for tasks.
 */
abstract class Task
{
    /**
     * The type of the task.
     */
    const SETTING_TYPE = 'type';

    /**
     * The id of the task.
     */
    const SETTING_ID = 'id';

    /**
     * This state determines that the task is still running.
     */
    const STATE_RUNNING = 'RUNNING';

    /**
     * This state determines that the task has been finished.
     */
    const STATE_FINISHED = 'FINISHED';

    /**
     * This state determines that the task has been finished with errors.
     */
    const STATE_ERROR = 'ERROR';

    /**
     * The task file to write to.
     *
     * @var JsonArray
     */
    protected $file;

    /**
     * The log file to write to.
     *
     * @var string
     */
    protected $logFile;

    /**
     * The input/output handler.
     *
     * @var IOInterface
     */
    private $inputOutput;

    /**
     * Task constructor.
     *
     * @param JsonArray $file The json file to write to.
     */
    public function __construct(JsonArray $file)
    {
        $this->file = $file;
    }

    /**
     * Retrieve the task id.
     *
     * @return string
     */
    public function getId()
    {
        return $this->file->get('id');
    }

    /**
     * Retrieve the current output.
     *
     * @return string
     */
    public function getOutput()
    {
        if ($this->logFile && is_file($this->logFile)) {
            return file_get_contents($this->logFile);
        }

        return '';
    }

    /**
     * Retrieve the task type name.
     *
     * @return string
     */
    abstract public function getType();

    /**
     * Perform the task.
     *
     * @return void
     */
    abstract public function perform();

    /**
     * Add some output.
     *
     * @param string $string The output string to append to the output.
     *
     * @return void
     *
     * @throws \LogicException When called prior to perform().
     */
    public function addOutput($string)
    {
        if (!$this->logFile) {
            throw new \LogicException('The has not started to run yet.');
        }
        $file = fopen($this->logFile, 'a+t');
        fputs($file, $string);
        fclose($file);
    }

    /**
     * Retrieve the IO interface.
     *
     * @return IOInterface
     */
    public function getIO()
    {
        if (!isset($this->inputOutput)) {
            $this->inputOutput = new ConsoleIO($this->getInput(), new TaskOutput($this), new HelperSet([]));
        }

        return $this->inputOutput;
    }

    /**
     * Retrieve the current status of a task.
{'fraction_non_alphanumeric': 0.12525410476935106, 'fraction_numerical': 0.0012509773260359655, 'mean_word_length': 1.9776536312849162, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 45, 'https://': 4, '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': '3466894', 'n_tokens_mistral': 1941, 'n_tokens_neox': 1901, 'n_words': 557}