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

DirectivesSystem   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 88.24%

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 15
cts 17
cp 0.8824
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 17 4
A validateDefinition() 0 9 2
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