Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Pull Request — master (#21)
by Jérémiah
08:55 queued 02:22
created

Executor::addValidatorRule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the OverblogGraphQLBundle package.
5
 *
6
 * (c) Overblog <http://github.com/overblog/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Overblog\GraphQLBundle\Request;
13
14
use GraphQL\Error;
15
use GraphQL\Executor\ExecutionResult;
16
use GraphQL\Executor\Executor as GraphQLExecutor;
17
use GraphQL\GraphQL;
18
use GraphQL\Language\Parser as GraphQLParser;
19
use GraphQL\Language\Source;
20
use GraphQL\Schema;
21
use GraphQL\Validator\DocumentValidator;
22
use Overblog\GraphQLBundle\Error\ErrorHandler;
23
use Overblog\GraphQLBundle\Event\Events;
24
use Overblog\GraphQLBundle\Event\ExecutorContextEvent;
25
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
26
27
class Executor
28
{
29
    private $schema;
30
31
    /**
32
     * @var EventDispatcherInterface|null
33
     */
34
    private $dispatcher;
35
36 27
    /** @var bool */
37 1
    private $throwException;
38 27
39 27
    /** @var ErrorHandler|null */
40 27
    private $errorHandler;
41 27
42 27
    /** @var callable[] */
43
    private $validationRules;
44
45
    public function __construct(Schema $schema, EventDispatcherInterface $dispatcher = null, $throwException = false, ErrorHandler $errorHandler = null)
46
    {
47
        $this->schema = $schema;
48
        $this->dispatcher = $dispatcher;
49 22
        $this->throwException = (bool) $throwException;
50
        $this->errorHandler = $errorHandler;
51 22
        $this->validationRules = DocumentValidator::allRules();
52
    }
53 22
54
    public function addValidatorRule(callable $validatorRule)
55
    {
56 27
        $this->validationRules[] = $validatorRule;
57
    }
58 27
59 27
    /**
60
     * @param bool $throwException
61 27
     *
62 27
     * @return $this
63 27
     */
64 27
    public function setThrowException($throwException)
65 27
    {
66 27
        $this->throwException = (bool) $throwException;
67 27
68
        return $this;
69 27
    }
70
71 27
    public function execute(array $data, array $context = [])
72
    {
73
        if (null !== $this->dispatcher) {
74
            $event = new ExecutorContextEvent($context);
75
            $this->dispatcher->dispatch(Events::EXECUTOR_CONTEXT, $event);
76
            $context = $event->getExecutorContext();
77
        }
78
79
        $executionResult = $this->executeAndReturnResult(
80
            $this->schema,
81
            isset($data['query']) ? $data['query'] : null,
82
            $context,
83
            $data['variables'],
84
            $data['operationName']
85
        );
86
87
        if (null !== $this->errorHandler) {
88
            $this->errorHandler->handleErrors($executionResult, $this->throwException);
89
        }
90
91
        return $executionResult;
92
    }
93
94
    private function executeAndReturnResult(Schema $schema, $requestString, $rootValue = null, $variableValues = null, $operationName = null)
95
    {
96
        try {
97
            $source = new Source($requestString ?: '', 'GraphQL request');
98
            $documentAST = GraphQLParser::parse($source);
99
            $validationErrors = DocumentValidator::validate($schema, $documentAST, $this->validationRules);
100
101
            if (!empty($validationErrors)) {
102
                return new ExecutionResult(null, $validationErrors);
103
            }
104
105
            return GraphQLExecutor::execute($schema, $documentAST, $rootValue, $variableValues, $operationName);
106
        } catch (Error $e) {
107
            return new ExecutionResult(null, [$e]);
108
        }
109
    }
110
}
111