Passed
Push — master ( c47e08...96e565 )
by Quang
02:51
created

ASTDirector   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
dl 0
loc 48
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getBuilder() 0 9 4
A __construct() 0 7 2
A build() 0 9 2
1
<?php
2
3
namespace Digia\GraphQL\Language\ASTBuilder;
4
5
use Digia\GraphQL\Error\LanguageException;
6
use Digia\GraphQL\Language\LexerInterface;
7
8
class ASTDirector implements ASTDirectorInterface
9
{
10
    /**
11
     * @var ASTBuilderInterface[]
12
     */
13
    protected $builders;
14
15
    /**
16
     * ASTBuilder constructor.
17
     *
18
     * @param ASTBuilderInterface[] $builders
19
     */
20
    public function __construct($builders)
21
    {
22
        foreach ($builders as $builder) {
23
            $builder->setDirector($this);
24
        }
25
26
        $this->builders = $builders;
27
    }
28
29
    /**
30
     * @inheritdoc
31
     */
32
    public function build(string $kind, LexerInterface $lexer, array $params = []): ?array
33
    {
34
        $builder = $this->getBuilder($kind);
35
36
        if ($builder !== null) {
37
            return $builder->build($lexer, $params);
38
        }
39
40
        throw new LanguageException(sprintf('AST of kind "%s" not supported.', $kind));
41
    }
42
43
    /**
44
     * @param string $kind
45
     * @return ASTBuilderInterface|null
46
     */
47
    protected function getBuilder(string $kind): ?ASTBuilderInterface
48
    {
49
        foreach ($this->builders as $builder) {
50
            if ($builder instanceof ASTBuilderInterface && $builder->supportsBuilder($kind)) {
51
                return $builder;
52
            }
53
        }
54
55
        return null;
56
    }
57
}
58