CommandsRenderer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 37
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A render() 0 20 4
1
<?php declare(strict_types=1);
2
3
namespace Bigwhoop\Trumpet\Presentation\SlideRendering;
4
5
use Bigwhoop\Trumpet\Commands\CommandExecutionContext;
6
use Bigwhoop\Trumpet\Commands\CommandHandler;
7
use Bigwhoop\Trumpet\Commands\CommandParams;
8
use Bigwhoop\Trumpet\Config\Slide;
9
10
final class CommandsRenderer implements SlideRenderer
11
{
12
    const COMMAND_PREFIX = '!';
13
14
    /** @var CommandHandler */
15
    private $commandHandler;
16
17
    /** @var CommandExecutionContext */
18
    private $executionContext;
19
20 1
    public function __construct(CommandHandler $handler, CommandExecutionContext $executionContext)
21
    {
22 1
        $this->commandHandler = $handler;
23 1
        $this->executionContext = $executionContext;
24 1
    }
25
    
26 1
    public function render(Slide $slide): string
27
    {
28 1
        $lines = explode("\n", str_replace("\r", '', $slide->content));
29
30 1
        $out = [];
31 1
        foreach ($lines as $line) {
32 1
            if (0 === strpos($line, self::COMMAND_PREFIX)) {
33 1
                $command = substr($line, strlen(self::COMMAND_PREFIX));
34 1
                list($commandName, $commandParams) = explode(' ', $command, 2);
35
36 1
                if ($this->commandHandler->hasCommand($commandName)) {
37 1
                    $line = $this->commandHandler->execute($commandName, new CommandParams($commandParams), $this->executionContext);
38
                }
39
            }
40
41 1
            $out[] = $line;
42
        }
43
44 1
        return implode("\n", $out);
45
    }
46
}
47