RequestValidator   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 4
dl 0
loc 51
ccs 33
cts 33
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 7 1
A assetFragmentsUsed() 0 12 4
A assertFragmentReferencesValid() 0 8 3
A assertAllVariablesExists() 0 8 3
A assertAllVariablesUsed() 0 8 3
1
<?php
2
/**
3
 * Date: 10/24/16
4
 *
5
 * @author Portey Vasil <[email protected]>
6
 */
7
8
namespace Youshido\GraphQL\Validator\RequestValidator;
9
10
11
use Youshido\GraphQL\Exception\Parser\InvalidRequestException;
12
use Youshido\GraphQL\Execution\Request;
13
14
class RequestValidator implements RequestValidatorInterface
15
{
16
17 74
    public function validate(Request $request)
18
    {
19 74
        $this->assertFragmentReferencesValid($request);
20 71
        $this->assetFragmentsUsed($request);
21 70
        $this->assertAllVariablesExists($request);
22 69
        $this->assertAllVariablesUsed($request);
23 68
    }
24
25 71
    private function assetFragmentsUsed(Request $request)
26
    {
27 71
        foreach ($request->getFragmentReferences() as $fragmentReference) {
28 9
            $request->getFragment($fragmentReference->getName())->setUsed(true);
29 71
        }
30
31 71
        foreach ($request->getFragments() as $fragment) {
32 9
            if (!$fragment->isUsed()) {
33 1
                throw new InvalidRequestException(sprintf('Fragment "%s" not used', $fragment->getName()), $fragment->getLocation());
34
            }
35 71
        }
36 70
    }
37
38 74
    private function assertFragmentReferencesValid(Request $request)
39
    {
40 74
        foreach ($request->getFragmentReferences() as $fragmentReference) {
41 12
            if (!$request->getFragment($fragmentReference->getName())) {
42 3
                throw new InvalidRequestException(sprintf('Fragment "%s" not defined in query', $fragmentReference->getName()), $fragmentReference->getLocation());
43
            }
44 72
        }
45 71
    }
46
47 70
    private function assertAllVariablesExists(Request $request)
48
    {
49 70
        foreach ($request->getVariableReferences() as $variableReference) {
50 11
            if (!$variableReference->getVariable()) {
51 1
                throw new InvalidRequestException(sprintf('Variable "%s" not exists', $variableReference->getName()), $variableReference->getLocation());
52
            }
53 69
        }
54 69
    }
55
56 69
    private function assertAllVariablesUsed(Request $request)
57
    {
58 69
        foreach ($request->getQueryVariables() as $queryVariable) {
59 10
            if (!$queryVariable->isUsed()) {
60 1
                throw new InvalidRequestException(sprintf('Variable "%s" not used', $queryVariable->getName()), $queryVariable->getLocation());
61
            }
62 69
        }
63 68
    }
64
}
65