Completed
Push — master ( c1a156...8cadde )
by Kirill
08:14
created

DirectivesSystem::resolve()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 2
dl 0
loc 17
ccs 11
cts 11
cp 1
crap 4
rs 9.7
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\SDL\Compiler\System\Provider;
11
12
use Railt\Parser\Ast\RuleInterface;
13
use Railt\Reflection\Contracts\Definition;
14
use Railt\Reflection\Contracts\Definition\DirectiveDefinition;
15
use Railt\Reflection\Contracts\Invocation\Behaviour\ProvidesDirectives;
16
use Railt\Reflection\Contracts\Invocation\DirectiveInvocation;
17
use Railt\Reflection\Invocation\Behaviour\HasDirectives;
18
use Railt\SDL\Ast\ProvidesDirectiveNodes;
19
use Railt\SDL\Compiler\System\System;
20
use Railt\SDL\Exception\TypeConflictException;
21
22
/**
23
 * Class DirectivesSystem
24
 */
25
class DirectivesSystem extends System
26
{
27
    /**
28
     * @param Definition|HasDirectives $definition
29
     * @param RuleInterface $ast
30
     */
31 119
    public function resolve(Definition $definition, RuleInterface $ast): void
32
    {
33 119
        if ($definition instanceof ProvidesDirectives && $ast instanceof ProvidesDirectiveNodes) {
34 119
            foreach ($ast->getDirectiveNodes() as $child) {
35 7
                $this->deferred(function () use ($definition, $child) {
36
                    /** @var DirectiveInvocation $directive */
37 7
                    $directive = $this->process->build($child, $definition);
38
39 7
                    $this->linker(function () use ($definition, $directive) {
40 7
                        $this->validateDefinition($directive);
41
42 7
                        $definition->withDirective($directive);
43 7
                    });
44 7
                });
45
            }
46
        }
47 119
    }
48
49
    /**
50
     * @param DirectiveInvocation $directive
51
     * @throws \Railt\SDL\Exception\CompilerException
52
     */
53 7
    private function validateDefinition(DirectiveInvocation $directive): void
54
    {
55 7
        $definition = $directive->getDefinition();
56
57 7
        if (! $definition instanceof DirectiveDefinition) {
58
            $error = \sprintf('Can not use %s as directive', $definition);
59
            throw (new TypeConflictException($error))->in($directive);
60
        }
61 7
    }
62
}
63