thunderer/Logeek

View on GitHub
src/Action/WhileLoopAction.php

Summary

Maintainability
A
1 hr
Test Coverage
<?php
declare(strict_types=1);
namespace Thunder\Logeek\Action;

use Thunder\Logeek\ActionInterface;
use Thunder\Logeek\Board;

final class WhileLoopAction implements ActionInterface
{
    private $iterations;

    public function __construct($iterations)
    {
        $this->iterations = $iterations;
    }

    public function execute(Board $board, string $alias, array $operation)
    {
        $iteration = 0;
        while(true) {
            $left = (string)$board->getVariable($operation['left']);
            $board->debug(sprintf('While Evaluate %s %s %s', $left, $operation['operator'], $operation['right']));
            if($operation['operator'] === 'is' && $left === (string)$operation['right']) {
                $board->runActorProgram($alias, $operation['program']);
            } elseif($operation['operator'] === 'not' && $left !== (string)$operation['right']) {
                $board->runActorProgram($alias, $operation['program']);
            } else {
                $board->debug('While LoopEnd');
                break;
            }
            if($iteration > $this->iterations) {
                $board->debug('Iterations exceeded!');
                break;
            }
            $iteration++;
        }
    }

    public function getAlias(): string
    {
        return 'while';
    }

    public function getArguments(): array
    {
        return ['left', 'operator', 'right', 'program'];
    }
}