<commit_before><?php

namespace Honeybee\Infrastructure\ProcessManager\StateMachine;

use Honeybee\Common\Error\RuntimeError;
use Honeybee\Infrastructure\ProcessManager\ProcessStateInterface;
use Honeybee\Model\Aggregate\AggregateRootTypeMap;
use Honeybee\Model\Task\ModifyAggregateRoot\AddEmbeddedEntity\AddEmbeddedEntityCommand;
use JmesPath\AstRuntime;
use Workflux\State\State;
use Workflux\StatefulSubjectInterface;

abstract class AggregateRootCommandStateNode extends State
{
    protected $aggregate_root_type_map;

    abstract protected function createCommand(ProcessStateInterface $process_state);

    public function __construct(
        $name,
        $type = self::TYPE_ACTIVE,
        array $options = [],
        AggregateRootTypeMap $aggregate_root_type_map
    ) {
        parent::__construct($name, $type, $options);

        $this->aggregate_root_type_map = $aggregate_root_type_map;

        $this->needs('aggregate_root_type')->needs('command')->needs('payload_path');
    }

    public function onEntry(StatefulSubjectInterface $process_state)
    {
        if (!$process_state instanceof ProcessStateInterface) {
            throw new RuntimeError('Only ' . ProcessStateInterface::CLASS . ' subjects supported by ' . static::CLASS);
        }

        parent::onEntry($process_state);

        $command = $this->createCommand($process_state);
        $execution_context = $process_state->getExecutionContext();
        $execution_context->setParameter('command', $command);
    }

    protected function getCommandImplementor()
    {
        $command_class = $this->options->get('command');
        if (!class_exists($command_class)) {
            throw new RuntimeError('Unable to load configured command class: ' . $command_class);
        }

        return $command_class;
    }

    protected function getCommandPayload(ProcessStateInterface $process_state)
    {
        $command_class = $this->getCommandImplementor();
        $payload = $process_state->getPayload();
        $payload_path = $this->options->get('payload_path');
        $jmes_path_runtime = new AstRuntime();

        $command_payload = $jmes_path_runtime($payload_path, $payload);

        return $command_payload;
    }

    protected function getAggregateRootType()
    {
        $ar_type_prefix = $this->options->get('aggregate_root_type');
        if (!$this->aggregate_root_type_map->hasKey($ar_type_prefix)) {
            throw new RuntimeError('Unable to resolve given ar-prefix: ' . $ar_type_prefix);
        }

        return $this->aggregate_root_type_map->getItem($ar_type_prefix);
    }

    protected function needs($option_key)
    {
        if (!$this->options->has($option_key)) {
            throw new RuntimeError(sprintf('Missing require option "%s"', $option_key));
        }

        return $this;
    }

    protected function requiresVariable($variable_name, ProcessStateInterface $process_state)<commit_msg>validation of the payload config now JIT<commit_after><?php

namespace Honeybee\Infrastructure\ProcessManager\StateMachine;

use Honeybee\Common\Error\RuntimeError;
use Honeybee\Infrastructure\ProcessManager\ProcessStateInterface;
use Honeybee\Model\Aggregate\AggregateRootTypeMap;
use Honeybee\Model\Task\ModifyAggregateRoot\AddEmbeddedEntity\AddEmbeddedEntityCommand;
use JmesPath\AstRuntime;
use Workflux\State\State;
use Workflux\StatefulSubjectInterface;

abstract class AggregateRootCommandStateNode extends State
{
    protected $aggregate_root_type_map;

    abstract protected function createCommand(ProcessStateInterface $process_state);

    public function __construct(
        $name,
        $type = self::TYPE_ACTIVE,
        array $options = [],
        AggregateRootTypeMap $aggregate_root_type_map
    ) {
        parent::__construct($name, $type, $options);

        $this->aggregate_root_type_map = $aggregate_root_type_map;

        $this->needs('aggregate_root_type')->needs('command');
    }

    public function onEntry(StatefulSubjectInterface $process_state)
    {
        if (!$process_state instanceof ProcessStateInterface) {
            throw new RuntimeError('Only ' . ProcessStateInterface::CLASS . ' subjects supported by ' . static::CLASS);
        }

        parent::onEntry($process_state);

        $command = $this->createCommand($process_state);
        $execution_context = $process_state->getExecutionContext();
        $execution_context->setParameter('command', $command);
    }

    protected function getCommandImplementor()
    {
        $command_class = $this->options->get('command');
        if (!class_exists($command_class)) {
            throw new RuntimeError('Unable to load configured command class: ' . $command_class);
        }

        return $command_class;
    }

    protected function getCommandPayload(ProcessStateInterface $process_state)
    {
        $this->needs('payload_path');

        $command_class = $this->getCommandImplementor();
        $payload = $process_state->getPayload();
        $payload_path = $this->options->get('payload_path');
        $jmes_path_runtime = new AstRuntime();

        $command_payload = $jmes_path_runtime($payload_path, $payload);

        return $command_payload;
    }

    protected function getAggregateRootType()
    {
        $ar_type_prefix = $this->options->get('aggregate_root_type');
        if (!$this->aggregate_root_type_map->hasKey($ar_type_prefix)) {
            throw new RuntimeError('Unable to resolve given ar-prefix: ' . $ar_type_prefix);
        }

        return $this->aggregate_root_type_map->getItem($ar_type_prefix);
    }

    protected function needs($option_key)
    {
        if (!$this->options->has($option_key)) {
            throw new RuntimeError(sprintf('Missing require option "%s"', $option_key));
        }

        return $this;
    }

    protected function requiresVariable($variable_name, ProcessStateInterface $process_state)
{'fraction_non_alphanumeric': 0.10841944773843808, 'fraction_numerical': 0.0, 'mean_word_length': 3.667193675889328, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 45, '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': '4673322', 'n_tokens_mistral': 1664, 'n_tokens_neox': 1609, 'n_words': 286}