<commit_before><?php

namespace Reliv\PipeRat\Provider;

use Reliv\PipeRat\Options\GenericOptions;
use Reliv\PipeRat\ServiceModel\BaseControllerModel;
use Reliv\PipeRat\ServiceModel\BaseMethodModel;
use Reliv\PipeRat\ServiceModel\BaseResourceModel;
use Reliv\PipeRat\ServiceModel\BaseServiceModelCollection;
use Reliv\PipeRat\ServiceModel\ResourceModel;

/**
 * Class ConfigResourceModelProvider
 *
 * @author    <NAME> <<EMAIL>>
 * @copyright 2016 Reliv International
 * @license   License.txt
 * @link      https://github.com/reliv
 */
class ConfigResourceModelProvider extends ConfigAbstractResourceModelProvider implements ResourceModelProvider
{
    /**
     * @var array
     */
    protected $resourceModels = [];

    /**
     * getMethodsAllowed
     *
     * @param $resourceKey
     *
     * @return mixed
     */
    protected function getMethodsAllowed($resourceKey)
    {
        return $this->getResourceValue($resourceKey, 'methodsAllowed', []);
    }

    /**
     * buildServiceModelCollection
     *
     * @param array $serviceNames
     * @param array $serviceOptionsArrays
     * @param array $servicePriorities
     *
     * @return BaseServiceModelCollection
     */
    protected function buildServiceModelCollection(
        array $serviceNames,
        array $serviceOptionsArrays,
        array $servicePriorities
    ) {
        $services = [];
        foreach ($serviceNames as $serviceAlias => $serviceName) {
            $services[$serviceAlias] = $this->serviceManager->get($serviceName);
        }

        $serviceOptions = [];
        foreach ($serviceOptionsArrays as $serviceAlias => $serviceOptionsArray) {
            $serviceOptions[$serviceAlias] = new GenericOptions($serviceOptionsArray);
        }

        return new BaseServiceModelCollection(
            $services,
            $serviceOptions,
            $servicePriorities
        );
    }

    /**
     * buildMethodModels
     *
     * @param string $resourceKey
     *
     * @return array
     */
    protected function buildMethodModels($resourceKey)
    {
        $methods = $this->getDefaultValue($resourceKey, 'methods', []);
        $userMethods = $this->getResourceValue($resourceKey, 'methods', []);

        $customMethods = [];

        foreach ($userMethods as $key => $method) {
            if (array_key_exists($key, $methods)) {
                $methods[$key] = array_merge_recursive($methods[$key], $method);
            } else {
                $customMethods[$key] = $method;
            }
        }

        $methods = array_merge($customMethods, $methods);

        $returnMethods = [];

        foreach ($methods as $key => $method) {
            $methodOptions = new GenericOptions($method);

            $preServiceModel = $this->buildServiceModelCollection(
                $methodOptions->get('preServiceNames', []),
                $methodOptions->get('preServiceOptions', []),
                $methodOptions->get('preServicePriority', [])
            );

            $postServiceModel = $this->buildServiceModelCollection(
                $methodOptions->get('postServiceNames', []),
                $methodOptions->get('postServiceOptions', []),
                $methodOptions->get('postServicePriority', [])
            );

            $options = $methodOptions->getOptions('options');

            $name = $methodOptions->get('name', $key);

            $returnMethods[$name] = new BaseMethodModel(
                $name,
                $methodOptions->get('description'),
                $methodOptions->get('httpVerb'),
                $methodOptions->get('path'),
                $preServiceModel,
                $postServiceModel,
                $options
            );
        }

        return $returnMethods;
    }

    /**
     * buildMethodPriorities
     *
     * @param $resourceKey
     *
     * @return mixed
     */
    public function getMethodPriorities($resourceKey)
    {
        $defaultMethodPriorities = $this->getDefaultValue($resourceKey, 'methodPriority', []);
        $userMethodPriorities = $this->getResourceValue($resourceKey, 'methodPriority', []);

        return array_merge($defaultMethodPriorities, $userMethodPriorities);
    }

    /**
     * get
     *
     * @param string $resourceKey
     *
     * @return ResourceModel
     */
    public function get($resourceKey)
    {
        if (array_key_exists($resourceKey, $this->resourceModels)) {
            return $this->resourceModels[$resourceKey];
        }

        // ControllerModel
        $controllerServiceAlias = $this->buildValue(
            $resourceKey,
            'controllerServiceName',
            'UNDEFINED'
        );

        $controllerService = $this->serviceManager->get(
            $controllerServiceAlias
        );

        $controllerOptions = $this->buildOptions(
            $resourceKey,
            'controllerServiceOptions'
        );

        $controllerModel = new BaseControllerModel(
            $controllerServiceAlias,
            $controllerService,
            $controllerOptions
        );

        // Methods
        $methodsAllowed = $this->getMethodsAllowed($resourceKey);
        $methodPriorities = $this->getMethodPriorities($resourceKey);
        $methodModels = $this->buildMethodModels($resourceKey);
        $path = $this->getResourceValue($resourceKey, 'path');

        $preServiceModel = $this->buildServiceModelCollection(
            $this->buildMergeValue($resourceKey, 'preServiceNames', []),
            $this->buildMergeValue($resourceKey, 'preServiceOptions', []),
            $this->buildMergeValue($resourceKey, 'preServicePriority', [])<commit_msg>Update Extractors<commit_after><?php

namespace Reliv\PipeRat\Provider;

use Reliv\PipeRat\Options\GenericOptions;
use Reliv\PipeRat\ServiceModel\BaseControllerModel;
use Reliv\PipeRat\ServiceModel\BaseMethodModel;
use Reliv\PipeRat\ServiceModel\BaseResourceModel;
use Reliv\PipeRat\ServiceModel\BaseServiceModelCollection;
use Reliv\PipeRat\ServiceModel\ResourceModel;

/**
 * Class ConfigResourceModelProvider
 *
 * @author    <NAME> <<EMAIL>>
 * @copyright 2016 Reliv International
 * @license   License.txt
 * @link      https://github.com/reliv
 */
class ConfigResourceModelProvider extends ConfigAbstractResourceModelProvider implements ResourceModelProvider
{
    /**
     * @var array
     */
    protected $resourceModels = [];

    /**
     * getMethodsAllowed
     *
     * @param $resourceKey
     *
     * @return mixed
     */
    protected function getMethodsAllowed($resourceKey)
    {
        return $this->getResourceValue($resourceKey, 'methodsAllowed', []);
    }

    /**
     * buildServiceModelCollection
     *
     * @param array $serviceNames
     * @param array $serviceOptionsArrays
     * @param array $servicePriorities
     *
     * @return BaseServiceModelCollection
     */
    protected function buildServiceModelCollection(
        array $serviceNames,
        array $serviceOptionsArrays,
        array $servicePriorities
    ) {
        $services = [];
        foreach ($serviceNames as $serviceAlias => $serviceName) {
            $services[$serviceAlias] = $this->serviceManager->get($serviceName);
        }

        $serviceOptions = [];
        foreach ($serviceOptionsArrays as $serviceAlias => $serviceOptionsArray) {
            $serviceOptions[$serviceAlias] = new GenericOptions($serviceOptionsArray);
        }

        return new BaseServiceModelCollection(
            $services,
            $serviceOptions,
            $servicePriorities
        );
    }

    /**
     * buildMethodModels
     *
     * @param string $resourceKey
     *
     * @return array
     */
    protected function buildMethodModels($resourceKey)
    {
        $methods = $this->getDefaultValue($resourceKey, 'methods', []);
        $userMethods = $this->getResourceValue($resourceKey, 'methods', []);

        $customMethods = [];

        foreach ($userMethods as $key => $method) {
            if (array_key_exists($key, $methods)) {
                $methods[$key] = array_merge_recursive($methods[$key], $method);
            } else {
                $customMethods[$key] = $method;
            }
        }

        $methods = array_merge($customMethods, $methods);

        $returnMethods = [];

        foreach ($methods as $key => $method) {
            $methodOptions = new GenericOptions($method);

            $preServiceModel = $this->buildServiceModelCollection(
                $methodOptions->get('preServiceNames', []),
                $methodOptions->get('preServiceOptions', []),
                $methodOptions->get('preServicePriority', [])
            );

            $postServiceModel = $this->buildServiceModelCollection(
                $methodOptions->get('postServiceNames', []),
                $methodOptions->get('postServiceOptions', []),
                $methodOptions->get('postServicePriority', [])
            );

            $options = $methodOptions->getOptions('options');

            $name = $methodOptions->get('name', $key);

            $returnMethods[$name] = new BaseMethodModel(
                $name,
                $methodOptions->get('description'),
                $methodOptions->get('httpVerb'),
                $methodOptions->get('path'),
                $preServiceModel,
                $postServiceModel,
                $options
            );
        }

        return $returnMethods;
    }

    /**
     * buildMethodPriorities
     *
     * @param $resourceKey
     *
     * @return mixed
     */
    public function getMethodPriorities($resourceKey)
    {
        $defaultMethodPriorities = $this->getDefaultValue($resourceKey, 'methodPriority', []);
        $userMethodPriorities = $this->getResourceValue($resourceKey, 'methodPriority', []);

        return array_merge($defaultMethodPriorities, $userMethodPriorities);
    }

    /**
     * get
     *
     * @param string $resourceKey
     *
     * @return mixed
     * @throws \Exception
     */
    public function get($resourceKey)
    {
        if (array_key_exists($resourceKey, $this->resourceModels)) {
            return $this->resourceModels[$resourceKey];
        }

        // ControllerModel
        $controllerServiceAlias = $this->buildValue(
            $resourceKey,
            'controllerServiceName'
        );

        if(empty($controllerServiceAlias)) {
            throw new \Exception("controllerServiceName missing $resourceKey");
        }

        $controllerService = $this->serviceManager->get(
            $controllerServiceAlias
        );

        $controllerOptions = $this->buildOptions(
            $resourceKey,
            'controllerServiceOptions'
        );

        $controllerModel = new BaseControllerModel(
            $controllerServiceAlias,
            $controllerService,
            $controllerOptions
        );

        // Methods
        $methodsAllowed = $this->getMethodsAllowed($resourceKey);
        $methodPriorities = $this->getMethodPriorities($resourceKey);
        $methodModels = $this->buildMethodModels($resourceKey);
        $path = $this->getResourceValue($resourceKey, 'path');

        $preServiceModel = $this->buildServiceModelCollection(
            $this->buildMergeValue($resourceKey, 'preServiceNames', []),
            $this->buildMergeValue($resourceKey, 'preServiceOptions', []),
            $this->buildMergeValue($resourceKey, 'preServicePriority', [])
{'fraction_non_alphanumeric': 0.12025593829432904, 'fraction_numerical': 0.0007012008063809273, 'mean_word_length': 2.3677685950413223, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 85, 'https://': 2, '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': '7049452', 'n_tokens_mistral': 2974, 'n_tokens_neox': 2864, 'n_words': 468}