RequestValidator::assetFragmentsUsed()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 9
cts 9
cp 1
rs 9.8666
c 0
b 0
f 0
cc 4
nc 6
nop 1
crap 4
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