Passed
Push — master ( d735d3...29fa29 )
by Yannick
18:02 queued 07:53
created

LoggingExecutor   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 45
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A executeQuery() 0 36 3
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\GraphQL;
8
9
use ApiPlatform\GraphQl\ExecutorInterface;
10
use GraphQL\GraphQL;
11
use GraphQL\Type\Schema;
12
use GraphQL\Executor\ExecutionResult;
13
use Psr\Log\LoggerInterface;
14
15
class LoggingExecutor implements ExecutorInterface
16
{
17
    private LoggerInterface $logger;
18
19
    public function __construct(LoggerInterface $logger)
20
    {
21
        $this->logger = $logger;
22
    }
23
24
    public function executeQuery(
25
        Schema $schema,
26
               $source,
27
               $rootValue = null,
28
               $context = null,
29
        array $variableValues = null,
30
        string $operationName = null,
31
        callable $fieldResolver = null,
32
        array $validationRules = null
33
    ): ExecutionResult {
34
        $result = GraphQL::executeQuery(
35
            $schema,
36
            $source,
37
            $rootValue,
38
            $context,
39
            $variableValues,
40
            $operationName,
41
            $fieldResolver,
42
            $validationRules
43
        );
44
45
        if (!empty($result->errors)) {
46
            foreach ($result->errors as $error) {
47
                $msg = '[GraphQL Error] '.$error->getMessage();
48
                error_log($msg);
49
50
                $this->logger->error($msg, [
51
                    'debugMessage' => $error->getPrevious()?->getMessage() ?? null,
52
                    'path' => $error->getPath(),
53
                    'locations' => $error->getLocations(),
54
                    'extensions' => $error->getExtensions(),
55
                ]);
56
            }
57
        }
58
59
        return $result;
60
    }
61
}
62