<commit_before><?php
/**
 * This file is part of Railt package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
declare(strict_types=1);

namespace Railt\Routing;

use Illuminate\Support\Str;
use Railt\Container\ContainerInterface;
use Railt\Reflection\Contracts\Definitions\TypeDefinition;
use Railt\Reflection\Contracts\Dependent\FieldDefinition;
use Railt\Reflection\Contracts\Document;
use Railt\Reflection\Contracts\Invocations\DirectiveInvocation;
use Railt\Reflection\Contracts\Invocations\InputInvocation;
use Railt\Routing\Exceptions\InvalidActionException;

/**
 * Class DirectiveRoute
 */
class DirectiveRoute extends Route
{
    /**
     * DirectiveRoute constructor.
     * @param ContainerInterface $container
     * @param TypeDefinition $type
     * @param DirectiveInvocation $directive
     * @throws InvalidActionException
     */
    public function __construct(ContainerInterface $container, TypeDefinition $type, DirectiveInvocation $directive)
    {
        parent::__construct($container, $type);

        //
        // @route( action: "Controller@action" )
        //
        $this->exportAction($directive->getDocument(), $directive->getPassedArgument('action'));

        //
        // @route( relation: {parent: "key", child: "key"} )
        //
        $relation = $directive->getPassedArgument('relation');

        if ($relation) {
            $this->exportRelation($relation, $directive->getParent());
        }

        //
        // @route( operations: [OperationName] )
        //
        $operations = $directive->getPassedArgument('operations');

        if ($operations) {
            $this->exportOperations($directive->getPassedArgument('operations'));
        }
    }

    /**
     * @param Document $document
     * @param string $urn
     * @throws \Railt\Routing\Exceptions\InvalidActionException
     */
    private function exportAction(Document $document, string $urn): void
    {
        [$controller, $action] = \tap(\explode('@', $urn), function (array $parts) use ($urn) {
            if (\count($parts) !== 2) {
                $error = 'The action route argument must contain an urn in the format "Class@action", but "%s" given';
                throw new InvalidActionException(\sprintf($error, $urn));
            }
        });

        $instance = $this->container->make($this->loadControllerClass($document, $controller));

        $this->then(\Closure::fromCallable([$instance, $action]));
    }

    /**
     * @param Document $document
     * @param string $controller<commit_msg>Apply fixes from StyleCI (#150)<commit_after><?php
/**
 * This file is part of Railt package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
declare(strict_types=1);

namespace Railt\Routing;

use Illuminate\Support\Str;
use Railt\Container\ContainerInterface;
use Railt\Reflection\Contracts\Definitions\TypeDefinition;
use Railt\Reflection\Contracts\Dependent\FieldDefinition;
use Railt\Reflection\Contracts\Document;
use Railt\Reflection\Contracts\Invocations\DirectiveInvocation;
use Railt\Reflection\Contracts\Invocations\InputInvocation;
use Railt\Routing\Exceptions\InvalidActionException;

/**
 * Class DirectiveRoute
 */
class DirectiveRoute extends Route
{
    /**
     * DirectiveRoute constructor.
     * @param ContainerInterface $container
     * @param TypeDefinition $type
     * @param DirectiveInvocation $directive
     * @throws InvalidActionException
     */
    public function __construct(ContainerInterface $container, TypeDefinition $type, DirectiveInvocation $directive)
    {
        parent::__construct($container, $type);

        //
        // @route( action: "Controller@action" )
        //
        $this->exportAction($directive->getDocument(), $directive->getPassedArgument('action'));

        //
        // @route( relation: {parent: "key", child: "key"} )
        //
        $relation = $directive->getPassedArgument('relation');

        if ($relation) {
            $this->exportRelation($relation, $directive->getParent());
        }

        //
        // @route( operations: [OperationName] )
        //
        $operations = $directive->getPassedArgument('operations');

        if ($operations) {
            $this->exportOperations($directive->getPassedArgument('operations'));
        }
    }

    /**
     * @param Document $document
     * @param string $urn
     * @throws \Railt\Routing\Exceptions\InvalidActionException
     */
    private function exportAction(Document $document, string $urn): void
    {
        [$controller, $action] = \tap(\explode('@', $urn), function (array $parts) use ($urn): void {
            if (\count($parts) !== 2) {
                $error = 'The action route argument must contain an urn in the format "Class@action", but "%s" given';
                throw new InvalidActionException(\sprintf($error, $urn));
            }
        });

        $instance = $this->container->make($this->loadControllerClass($document, $controller));

        $this->then(\Closure::fromCallable([$instance, $action]));
    }

    /**
     * @param Document $document
     * @param string $controller
{'fraction_non_alphanumeric': 0.1315390447308567, 'fraction_numerical': 0.001326762699014405, 'mean_word_length': 3.0159817351598175, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, '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': '4714845', 'n_tokens_mistral': 1429, 'n_tokens_neox': 1399, 'n_words': 320}