Completed
Pull Request — 8.x-3.x (#470)
by Sebastian
06:11 queued 03:37
created

Processor   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 113
rs 10
c 0
b 0
f 0
wmc 19
lcom 1
cbo 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
C doResolve() 0 23 8
C deferredResolve() 0 31 7
A doResolveValue() 0 11 3
1
<?php
2
3
namespace Drupal\graphql\GraphQL\Execution;
4
5
use Drupal\Core\Cache\CacheableDependencyInterface;
6
use Drupal\Core\Cache\CacheableMetadata;
7
use Drupal\graphql\GraphQL\SecureFieldInterface;
8
use Drupal\graphql\GraphQL\ValueWrapperInterface;
9
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
use Youshido\GraphQL\Execution\DeferredResolverInterface;
12
use Youshido\GraphQL\Execution\DeferredResult;
13
use Youshido\GraphQL\Execution\Processor as BaseProcessor;
14
use Youshido\GraphQL\Field\FieldInterface;
15
use Youshido\GraphQL\Parser\Ast\Interfaces\FieldInterface as AstFieldInterface;
16
use Youshido\GraphQL\Parser\Ast\Query as AstQuery;
17
use Youshido\GraphQL\Schema\AbstractSchema;
18
use Youshido\GraphQL\Type\Enum\AbstractEnumType;
19
use Youshido\GraphQL\Type\Scalar\AbstractScalarType;
20
21
class Processor extends BaseProcessor {
22
23
  /**
24
   * The dependency injection container.
25
   *
26
   * @var \Symfony\Component\DependencyInjection\ContainerInterface
27
   */
28
  protected $container;
29
30
  /**
31
   * Constructs a Processor object.
32
   *
33
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
34
   *   The dependency injection container.
35
   * @param \Youshido\GraphQL\Schema\AbstractSchema $schema
36
   *   The GraphQL schema.
37
   * @param boolean $secure
38
   *   Indicate that this processor is executing trusted queries.
39
   */
40
  public function __construct(ContainerInterface $container, AbstractSchema $schema, $secure = FALSE) {
41
    parent::__construct($schema);
42
    $this->container = $container;
43
44
    $contexts = $this->executionContext->getContainer();
45
    $contexts->set('secure', $secure);
46
    $contexts->set('metadata', new CacheableMetadata());
47
  }
48
49
  /**
50
   * {@inheritdoc}
51
   */
52
  protected function doResolve(FieldInterface $field, AstFieldInterface $ast, $parentValue = NULL) {
53
    $contexts = $this->executionContext->getContainer();
54
55
    if ($field instanceof SecureFieldInterface) {
56
      // If not resolving in a trusted environment, check if the field is secure.
57
      if ($contexts->has('secure') && !$contexts->get('secure') && !$field->isSecure()) {
58
        throw new \Exception(sprintf("Unable to resolve insecure field '%s' (%s).", $field->getName(), get_class($field)));
59
      }
60
    }
61
62
    $value = $this->doResolveValue($field, $ast, $parentValue);
63
    if ($value instanceof CacheableDependencyInterface && $contexts->has('metadata')) {
0 ignored issues
show
Bug introduced by
The class Drupal\Core\Cache\CacheableDependencyInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
64
      // If the current resolved value returns cache metadata, keep it.
65
      $contexts->get('metadata')->addCacheableDependency($value);
66
    }
67
68
    // If it's a value wrapper, extract the real value to return.
69
    if ($value instanceof ValueWrapperInterface) {
70
      $value = $value->getValue();
71
    }
72
73
    return $value;
74
  }
75
76
  /**
77
   * Override deferred resolving to use our own DeferredResult class.
78
   *
79
   * {@inheritdoc}
80
   */
81
  protected function deferredResolve($resolvedValue, FieldInterface $field, callable $callback) {
82
    if ($resolvedValue instanceof DeferredResolverInterface) {
83
      $deferredResult = new DeferredResult($resolvedValue, function($result) use ($callback, $field) {
84
        $contexts = $this->executionContext->getContainer();
85
86
        if ($result instanceof CacheableDependencyInterface && $contexts->has('metadata')) {
0 ignored issues
show
Bug introduced by
The class Drupal\Core\Cache\CacheableDependencyInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
87
          $contexts->get('metadata')->addCacheableDependency($result);
88
        }
89
90
        if ($result instanceof ValueWrapperInterface) {
91
          $result = $result->getValue();
92
        }
93
94
        return $this->deferredResolve($result, $field, $callback);
95
      });
96
97
      // Whenever we stumble upon a deferred resolver, add it to the queue
98
      // to be resolved later.
99
      $type = $field->getType()->getNamedType();
100
      if ($type instanceof AbstractScalarType || $type instanceof AbstractEnumType) {
101
        array_push($this->deferredResultsLeaf, $deferredResult);
102
      } else {
103
        array_push($this->deferredResultsComplex, $deferredResult);
104
      }
105
106
      return $deferredResult;
107
    }
108
109
    // For simple values, invoke the callback immediately.
110
    return $callback($resolvedValue);
111
  }
112
113
  /**
114
   * Helper function to resolve a field value.
115
   *
116
   * @param \Youshido\GraphQL\Field\FieldInterface $field
117
   * @param \Youshido\GraphQL\Parser\Ast\Interfaces\FieldInterface $ast
118
   * @param null $parentValue
119
   *
120
   * @return mixed|null
121
   */
122
  protected function doResolveValue(FieldInterface $field, AstFieldInterface $ast, $parentValue = NULL) {
123
    $arguments = $this->parseArgumentsValues($field, $ast);
124
    $astFields = $ast instanceof AstQuery ? $ast->getFields() : [];
125
    $resolveInfo = $this->createResolveInfo($field, $astFields);
126
127
    if ($field instanceof ContainerAwareInterface) {
0 ignored issues
show
Bug introduced by
The class Symfony\Component\Depend...ContainerAwareInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
128
      $field->setContainer($this->container);
129
    }
130
131
    return $field->resolve($parentValue, $arguments, $resolveInfo);
132
  }
133
}
134