Completed
Push — master ( e13501...8c19ea )
by Alexandr
05:24
created

RequestValidator   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 3
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\Execution\Request;
12
use Youshido\GraphQL\Parser\Exception\InvalidRequestException;
13
14
class RequestValidator implements RequestValidatorInterface
15
{
16
17 34
    public function validate(Request $request)
18
    {
19 34
        $this->assertFragmentReferencesValid($request);
20 32
        $this->assetFragmentsUsed($request);
21 31
        $this->assertAllVariablesExists($request);
22 30
        $this->assertAllVariablesUsed($request);
23 29
    }
24
25 32
    private function assetFragmentsUsed(Request $request)
26
    {
27 32
        foreach ($request->getFragmentReferences() as $fragmentReference) {
28 3
            $request->getFragment($fragmentReference->getName())->setUsed(true);
29 32
        }
30
31 32
        foreach ($request->getFragments() as $fragment) {
32 3
            if (!$fragment->isUsed()) {
33 1
                throw new InvalidRequestException(sprintf('Fragment "%s" not used', $fragment->getName()));
34
            }
35 32
        }
36 31
    }
37
38 34
    private function assertFragmentReferencesValid(Request $request)
39
    {
40 34
        foreach ($request->getFragmentReferences() as $fragmentReference) {
41 5
            if (!$request->getFragment($fragmentReference->getName())) {
42 2
                throw new InvalidRequestException(sprintf('Fragment "%s" not defined in query', $fragmentReference->getName()));
43
            }
44 33
        }
45 32
    }
46
47 31
    private function assertAllVariablesExists(Request $request)
48
    {
49 31
        foreach ($request->getVariableReferences() as $variableReference) {
50 5
            if (!$variableReference->getVariable()) {
51 1
                throw new InvalidRequestException(sprintf('Variable "%s" not exists', $variableReference->getName()));
52
            }
53 30
        }
54 30
    }
55
56 30
    private function assertAllVariablesUsed(Request $request)
57
    {
58 30
        foreach ($request->getQueryVariables() as $queryVariable) {
59 4
            if (!$queryVariable->isUsed()) {
60 1
                throw new InvalidRequestException(sprintf('Variable "%s" not used', $queryVariable->getName()));
61
            }
62 30
        }
63 29
    }
64
}
65