OutputRenderer   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 16
c 1
b 0
f 0
dl 0
loc 39
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addRenderer() 0 4 2
A render() 0 7 2
A __construct() 0 4 1
A renderRole() 0 11 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\Schema\Renderer\ConsoleRenderer;
6
7
use Cycle\Schema\Renderer\SchemaRenderer;
8
9
class OutputRenderer implements SchemaRenderer
10
{
11
    /** @var Renderer[] */
12
    private array $renderers = [];
13
    private Formatter $formatter;
14
15
    public function __construct(Formatter $formatter, array $renderers = [])
16
    {
17
        $this->formatter = $formatter;
18
        $this->addRenderer(...$renderers);
19
    }
20
21
    final public function addRenderer(Renderer ...$renderers): void
22
    {
23
        foreach ($renderers as $renderer) {
24
            $this->renderers[] = $renderer;
25
        }
26
    }
27
28
    final public function render(array $schema): string
29
    {
30
        $result = '';
31
        foreach ($schema as $role => $roleSchema) {
32
            $result .= $this->renderRole($roleSchema, $role) . $this->formatter::ROLE_BLOCK_SEPARATOR;
33
        }
34
        return $result;
35
    }
36
37
    private function renderRole(array $schema, string $role): string
38
    {
39
        $rows = [];
40
41
        foreach ($this->renderers as $renderer) {
42
            if ($row = $renderer->render($this->formatter, $schema, $role)) {
43
                $rows[] = $row;
44
            }
45
        }
46
47
        return \implode($this->formatter::LINE_SEPARATOR, $rows);
48
    }
49
}
50