Passed
Pull Request — master (#145)
by Christoffer
02:48 queued 25s
created

ExecutableDefinitionASTBuilder::build()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 8.2222
c 0
b 0
f 0
cc 7
eloc 11
nc 7
nop 2
1
<?php
2
3
namespace Digia\GraphQL\Language\ASTBuilder;
4
5
use Digia\GraphQL\Language\KeywordEnum;
6
use Digia\GraphQL\Language\LexerInterface;
7
use Digia\GraphQL\Language\TokenKindEnum;
8
9
class ExecutableDefinitionASTBuilder extends AbstractASTBuilder
10
{
11
    /**
12
     * @param LexerInterface $lexer
13
     * @return bool
14
     */
15
    public function supportsBuilder(string $kind): bool
16
    {
17
        return $kind === ASTKindEnum::EXECUTABLE_DEFINITION;
18
    }
19
20
    /**
21
     * @inheritdoc
22
     */
23
    public function build(LexerInterface $lexer, array $params): ?array
24
    {
25
        if ($this->peek($lexer, TokenKindEnum::NAME)) {
26
            // valid names are: query, mutation, subscription and fragment
27
            switch ($lexer->getToken()->getValue()) {
28
                case KeywordEnum::QUERY:
29
                case KeywordEnum::MUTATION:
30
                case KeywordEnum::SUBSCRIPTION:
31
                    return $this->buildAST(ASTKindEnum::OPERATION_DEFINITION, $lexer);
32
                case KeywordEnum::FRAGMENT:
33
                    return $this->buildAST(ASTKindEnum::FRAGMENT_DEFINITION, $lexer);
34
            }
35
        } elseif ($this->peek($lexer, TokenKindEnum::BRACE_L)) {
36
            // Anonymous query
37
            return $this->buildAST(ASTKindEnum::OPERATION_DEFINITION, $lexer);
38
        }
39
40
        throw $this->unexpected($lexer);
41
    }
42
}
43