izzum\statemachine\utils\Utils::getCommand PHP Method

getCommand() public static method

the Command will be configured with the 'reference' of the stateful object
public static getCommand ( string $command_name, Context $context ) : izzum\command\ICommand
$command_name string entry~,exit~ or transition command name. multiple commands can be split by a ',' in which case a composite command will be returned.
$context izzum\statemachine\Context to be able to get the entity
return izzum\command\ICommand
    public static function getCommand($command_name, Context $context)
    {
        // it's oke to have no command, as there might be 'marker' states, where
        // we just need to transition something to a next state (according to a
        // rule)
        // where useful work can be done (eg: from the 'initial' type state to
        // a 'shortcut' state for special cases.
        if ($command_name === '' || $command_name === null) {
            // return a command without side effects
            return new NullCommand();
        }
        $output = new Composite();
        // a command string can be made up of multiple commands seperated by a
        // comma
        $all_commands = explode(',', $command_name);
        // get the correct object to inject in the command(s)
        $entity = $context->getEntity();
        foreach ($all_commands as $single_command) {
            if (!class_exists($single_command)) {
                $e = new Exception(sprintf("failed command creation, class does not exist: (%s) for Context (%s)", $single_command, $context->toString()), Exception::COMMAND_CREATION_FAILURE);
                throw $e;
            }
            try {
                $command = new $single_command($entity);
                $output->add($command);
            } catch (\Exception $e) {
                $e = new Exception(sprintf("command (%s) objects to construction for Context (%s). message: '%s'", $single_command, $context->toString(), $e->getMessage()), Exception::COMMAND_CREATION_FAILURE);
                throw $e;
            }
        }
        return $output;
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * @test
  */
 public function shouldGetExceptionForNonExistingCommand()
 {
     $command_name = 'bogus';
     $context = new Context(new Identifier('1', 'test'));
     try {
         $command = Utils::getCommand($command_name, $context);
         $this->fail('should not come here, command does not exist');
     } catch (Exception $e) {
         $this->assertEquals(Exception::COMMAND_CREATION_FAILURE, $e->getCode());
         $this->assertContains('class does not exist', $e->getMessage());
         //echo $e->getMessage() . PHP_EOL;
     }
 }
All Usage Examples Of izzum\statemachine\utils\Utils::getCommand