Completed
Pull Request — 8.x-3.x (#432)
by Sebastian
01:59
created

Processor::getQueryCacheMetadata()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 2
nop 0
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
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
19
class Processor extends BaseProcessor {
20
  /**
21
   * The dependency injection container.
22
   *
23
   * @var \Symfony\Component\DependencyInjection\ContainerInterface
24
   */
25
  protected $container;
26
27
  /**
28
   * Constructs a Processor object.
29
   *
30
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
31
   *   The dependency injection container.
32
   * @param \Youshido\GraphQL\Schema\AbstractSchema $schema
33
   *   The GraphQL schema.
34
   * @param boolean $secure
35
   *   Indicate that this processor is executing trusted queries.
36
   */
37
  public function __construct(ContainerInterface $container, AbstractSchema $schema, $secure = FALSE) {
38
    parent::__construct($schema);
0 ignored issues
show
Documentation introduced by
$schema is of type object<Youshido\GraphQL\Schema\AbstractSchema>, but the function expects a object<Youshido\GraphQL\...cutionContextInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
39
    $this->container = $container;
40
41
    $contexts = $this->executionContext->getContainer();
42
    $contexts->set('secure', $secure);
43
    $contexts->set('metadata', new CacheableMetadata());
44
  }
45
46
  /**
47
   * {@inheritdoc}
48
   */
49
  protected function doResolve(FieldInterface $field, AstFieldInterface $ast, $parentValue = NULL) {
50
    $contexts = $this->executionContext->getContainer();
51
52
    if ($field instanceof SecureFieldInterface) {
53
      // If not resolving in a trusted environment, check if the field is secure.
54
      if ($contexts->has('secure') && !$contexts->get('secure') && !$field->isSecure()) {
55
        throw new \Exception(sprintf("Unable to resolve insecure field '%s' (%s).", $field->getName(), get_class($field)));
56
      }
57
    }
58
59
    $value = $this->doResolveValue($field, $ast, $parentValue);
60
    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...
61
      // If the current resolved value returns cache metadata, keep it.
62
      $contexts->get('metadata')->addCacheableDependency($value);
63
    }
64
65
    // If it's a value wrapper, extract the real value to return.
66
    if ($value instanceof ValueWrapperInterface) {
67
      $value = $value->getValue();
68
    }
69
70
    return $value;
71
  }
72
73
  /**
74
   * Override deferred resolving to use our own DeferredResult class.
75
   *
76
   * {@inheritdoc}
77
   */
78
  protected function deferredResolve($resolvedValue, callable $callback) {
79
    if ($resolvedValue instanceof DeferredResolverInterface) {
0 ignored issues
show
Bug introduced by
The class Youshido\GraphQL\Executi...ferredResolverInterface 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...
80
      $deferredResult = new DeferredResult($resolvedValue, function($result) use ($callback) {
81
        $contexts = $this->executionContext->getContainer();
82
83
        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...
84
          $contexts->get('metadata')->addCacheableDependency($result);
85
        }
86
87
        if ($result instanceof ValueWrapperInterface) {
88
          $result = $result->getValue();
89
        }
90
91
        return call_user_func($callback, $result);
92
      });
93
94
      // Whenever we stumble upon a deferred resolver, append it to the
95
      // queue to be resolved later.
96
      $this->deferredResults[] = $deferredResult;
0 ignored issues
show
Bug introduced by
The property deferredResults does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
97
      return $deferredResult;
98
    }
99
100
    // For simple values, invoke the callback immediately.
101
    return $callback($resolvedValue);
102
  }
103
104
  /**
105
   * Helper function to resolve a field value.
106
   *
107
   * @param \Youshido\GraphQL\Field\FieldInterface $field
108
   * @param \Youshido\GraphQL\Parser\Ast\Interfaces\FieldInterface $ast
109
   * @param null $parentValue
110
   *
111
   * @return mixed|null
112
   */
113
  protected function doResolveValue(FieldInterface $field, AstFieldInterface $ast, $parentValue = NULL) {
114
    $arguments = $this->parseArgumentsValues($field, $ast);
115
    $astFields = $ast instanceof AstQuery ? $ast->getFields() : [];
116
    $resolveInfo = $this->createResolveInfo($field, $astFields);
117
118
    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...
119
      $field->setContainer($this->container);
120
    }
121
122
    return $field->resolve($parentValue, $arguments, $resolveInfo);
123
  }
124
}
125