<commit_before><?php

/*
 *  This file is part of SplashSync Project.
 *
 *  Copyright (C) 2015-2019 Splash Sync  <www.splashsync.com>
 *
 *  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.
 *
 *  For the full copyright and license information, please view the LICENSE
 *  file that was distributed with this source code.
 */

namespace   Splash\Router;

use ArrayObject;
use Splash\Core\SplashCore      as Splash;

/**
 * Server Request Routiung Class, Execute/Route actions on Objects Service Requests.
 * This file is included only in case on NuSOAP call to slave server.
 *
 * @author      <NAME> <<EMAIL>>
 */
class Files
{
    /**
     * @abstract   Task execution router. Receive task detail and execute requiered task operations.
     *
     * @param ArrayObject $task Full Task Request Array
     *
     * @return ArrayObject Task results, or False if KO
     */
    public static function action($task)
    {
        //====================================================================//
        // Stack Trace
        Splash::log()->trace();

        //====================================================================//
        // Initial Response
        $response = self::getEmptyResponse($task);

        //====================================================================//
        // Safety Check - Minimal Parameters
        //====================================================================//
        $inputs = self::validateInputs($task);
        if (!$inputs) {
            return $response;
        }

        //====================================================================//
        // Execute Action
        switch ($task->name) {
            //====================================================================//
            //  READING A FILE INFORMATIONS
            case SPL_F_ISFILE:
                $response->data = Splash::file()->isFile($inputs['path'], $inputs['md5']);

                break;
            //====================================================================//
            //  READING A FILE CONTENTS
            case SPL_F_GETFILE:
                $response->data = Splash::file()->readFile($inputs['path'], $inputs['md5']);

                break;
            default:
                Splash::log()->err('File - Requested task not found => '.$task->name);

                break;
        }

        //====================================================================//
        // Task results prot treatment
        if (false != $response->data) {
            $response->result = true;
        }

        return $response;
    }

    /**
     * @abstract   Verify Task Inputs.
     *
     * @param ArrayObject $task Full Task Request Array
     *
     * @return array|false
     */
    private static function validateInputs($task)
    {
        //====================================================================//
        // Safety Check - Minimal Parameters
        //====================================================================//
        // Verify Requested Object Type is Available
        if (empty($task->params)) {
            Splash::log()->err('File Router - Missing Task Parameters... ');

            return false;
        }
        //====================================================================//
        // Verify Requested File Path is Available
        if (!isset($task->params->path) && !isset($task->params->file)) {
            Splash::log()->err('File Router - Missing File Path... ');

            return false;
        }
        //====================================================================//
        // Verify Requested File Path is Available
        if (empty($task->params->path) && empty($task->params->file)) {
            Splash::log()->err('File Router - Missing File Path... ');

            return false;
        }
        //====================================================================//
        // Verify Requested File Md5 is Available (but Says File Missing, for safety)
        if (empty($task->params->md5)) {
            Splash::log()->err('File Router - Missing File Path... ');

            return false;
        }
        //====================================================================//
        // Verify Requested Object Type is Valid
        if (true != Splash::validate()->isValidLocalClass()) {
            Splash::log()->err('File Router - Local Core Class is Invalid... ');

            return false;
        }

        //====================================================================//
        // Return Parameters
        return array(
            'path' => empty($task->params->path) ? $task->params->file : $task->params->path,
            'md5' => $task->params->md5,
        );
    }

    //====================================================================//
    //  LOW LEVEL FUNCTIONS
    //====================================================================//

    /**
     *      @abstract     Build an Empty Task Response
     *
     *      @param  ArrayObject     $task       Task To Execute
     *
     *      @return ArrayObject   Task Result ArrayObject
     */
    private static function getEmptyResponse($task)
    {
        //====================================================================//<commit_msg>WIP: Full Refactoring<commit_after><?php

/*
 *  This file is part of SplashSync Project.
 *
 *  Copyright (C) 2015-2019 Splash Sync  <www.splashsync.com>
 *
 *  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.
 *
 *  For the full copyright and license information, please view the LICENSE
 *  file that was distributed with this source code.
 */

namespace   Splash\Router;

use ArrayObject;
use Splash\Core\SplashCore      as Splash;

/**
 * Server Request Routiung Class, Execute/Route actions on Objects Service Requests.
 * This file is included only in case on NuSOAP call to slave server.
 *
 * @author      <NAME> <<EMAIL>>
 */
class Files
{
    /**
     * @abstract   Task execution router. Receive task detail and execute requiered task operations.
     *
     * @param ArrayObject $task Full Task Request Array
     *
     * @return ArrayObject Task results, or False if KO
     */
    public static function action($task)
    {
        //====================================================================//
        // Stack Trace
        Splash::log()->trace();

        //====================================================================//
        // Initial Response
        $response = self::getEmptyResponse($task);

        //====================================================================//
        // Safety Check - Minimal Parameters
        //====================================================================//
        $inputs = self::validateInputs($task);
        if (!$inputs) {
            return $response;
        }

        //====================================================================//
        // Execute Action
        switch ($task->name) {
            //====================================================================//
            //  READING A FILE INFORMATIONS
            case SPL_F_ISFILE:
                $response->data = Splash::file()->isFile($inputs['path'], $inputs['md5']);

                break;
            //====================================================================//
            //  READING A FILE CONTENTS
            case SPL_F_GETFILE:
                $response->data = Splash::file()->readFile($inputs['path'], $inputs['md5']);

                break;
            default:
                Splash::log()->err('File - Requested task not found => '.$task->name);

                break;
        }

        //====================================================================//
        // Task results prot treatment
        if (false != $response->data) {
            $response->result = true;
        }

        return $response;
    }

    /**
     * @abstract   Verify Task Inputs.
     *
     * @param ArrayObject $task Full Task Request Array
     *
     * @return array|false
     */
    private static function validateInputs($task)
    {
        //====================================================================//
        // Safety Check - Minimal Parameters
        //====================================================================//
        // Verify Requested Object Type is Available
        if (empty($task->params)) {
            Splash::log()->err('File Router - Missing Task Parameters... ');

            return false;
        }
        //====================================================================//
        // Verify Requested File Path is Available
        if (empty($task->params->path) && empty($task->params->file)) {
            Splash::log()->err('File Router - Missing File Path... ');

            return false;
        }
        //====================================================================//
        // Verify Requested File Md5 is Available (but Says File Missing, for safety)
        if (empty($task->params->md5)) {
            Splash::log()->err('File Router - Missing File Path... ');

            return false;
        }
        //====================================================================//
        // Verify Requested Object Type is Valid
        if (true != Splash::validate()->isValidLocalClass()) {
            Splash::log()->err('File Router - Local Core Class is Invalid... ');

            return false;
        }
        //====================================================================//
        // Detect File Path
        $filePath = (empty($task->params->path) && !empty($task->params->file))
                ? $task->params->file
                : $task->params->path;
        //====================================================================//
        // Return Parameters
        return array(
            'path' => $filePath,
            'md5' => $task->params->md5,
        );
    }

    //====================================================================//
    //  LOW LEVEL FUNCTIONS
    //====================================================================//

    /**
     *      @abstract     Build an Empty Task Response
     *
     *      @param  ArrayObject     $task       Task To Execute
     *
     *      @return ArrayObject   Task Result ArrayObject
     */
    private static function getEmptyResponse($task)
    {
        //====================================================================//
{'fraction_non_alphanumeric': 0.32656655543196145, 'fraction_numerical': 0.0025954764553207266, 'mean_word_length': 2.3684046206681235, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 84, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4463753', 'n_tokens_mistral': 2496, 'n_tokens_neox': 2288, 'n_words': 652}