WhileLoopAction::execute()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 8.6506
c 0
b 0
f 0
cc 7
nc 6
nop 3
1
<?php
2
declare(strict_types=1);
3
namespace Thunder\Logeek\Action;
4
5
use Thunder\Logeek\ActionInterface;
6
use Thunder\Logeek\Board;
7
8
final class WhileLoopAction implements ActionInterface
9
{
10
    private $iterations;
11
12
    public function __construct($iterations)
13
    {
14
        $this->iterations = $iterations;
15
    }
16
17
    public function execute(Board $board, string $alias, array $operation)
18
    {
19
        $iteration = 0;
20
        while(true) {
21
            $left = (string)$board->getVariable($operation['left']);
22
            $board->debug(sprintf('While Evaluate %s %s %s', $left, $operation['operator'], $operation['right']));
23
            if($operation['operator'] === 'is' && $left === (string)$operation['right']) {
24
                $board->runActorProgram($alias, $operation['program']);
25
            } elseif($operation['operator'] === 'not' && $left !== (string)$operation['right']) {
26
                $board->runActorProgram($alias, $operation['program']);
27
            } else {
28
                $board->debug('While LoopEnd');
29
                break;
30
            }
31
            if($iteration > $this->iterations) {
32
                $board->debug('Iterations exceeded!');
33
                break;
34
            }
35
            $iteration++;
36
        }
37
    }
38
39
    public function getAlias(): string
40
    {
41
        return 'while';
42
    }
43
44
    public function getArguments(): array
45
    {
46
        return ['left', 'operator', 'right', 'program'];
47
    }
48
}
49