Completed
Pull Request — 8.x-3.x (#525)
by Philipp
03:19
created

RequestController::handleBatchRequest()   C

Complexity

Conditions 7
Paths 4

Size

Total Lines 84
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 46
nc 4
nop 2
dl 0
loc 84
rs 6.5389
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Drupal\graphql\Controller;
4
5
use Drupal\Core\Cache\CacheableJsonResponse;
6
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
7
use Drupal\graphql\GraphQL\Execution\QueryProcessor;
8
use Symfony\Component\DependencyInjection\ContainerInterface;
9
10
/**
11
 * Handles GraphQL requests.
12
 */
13
class RequestController implements ContainerInjectionInterface {
14
15
  /**
16
   * The query processor.
17
   *
18
   * @var \Drupal\graphql\GraphQL\Execution\QueryProcessor
19
   */
20
  protected $processor;
21
22
  /**
23
   * The service configuration parameters.
24
   *
25
   * @var array
26
   */
27
  protected $parameters;
28
29
  /**
30
   * {@inheritdoc}
31
   */
32
  public static function create(ContainerInterface $container) {
33
    return new static(
34
      $container->get('graphql.query_processor'),
35
      $container->getParameter('graphql.config')
36
    );
37
  }
38
39
  /**
40
   * RequestController constructor.
41
   *
42
   * @param \Drupal\graphql\GraphQL\Execution\QueryProcessor $processor
43
   *   The query processor.
44
   * @param array $parameters
45
   *   The service configuration parameters.
46
   */
47
  public function __construct(QueryProcessor $processor, array $parameters) {
48
    $this->processor = $processor;
49
    $this->parameters = $parameters;
50
  }
51
52
  /**
53
   * Handles graphql requests.
54
   *
55
   * @param string $schema
56
   *   The name of the schema.
57
   * @param \GraphQL\Server\OperationParams|\GraphQL\Server\OperationParams[] $operations
58
   *   The graphql operation(s) to execute.
59
   *
60
   * @return \Drupal\Core\Cache\CacheableJsonResponse
61
   *   The JSON formatted response.
62
   */
63
  public function handleRequest($schema, $operations) {
64
    $debug = !empty($this->parameters['development']);
65
66
    if (is_array($operations)) {
67
      return $this->handleBatch($schema, $operations, $debug);
68
    }
69
70
    return $this->handleSingle($schema, $operations, $debug);
71
  }
72
73
  /**
74
   * @param $schema
75
   * @param $operations
76
   * @param bool $debug
77
   *
78
   * @return \Drupal\Core\Cache\CacheableJsonResponse
79
   */
80
  protected function handleSingle($schema, $operations, $debug = FALSE) {
81
    $result = $this->processor->processQuery($schema, $operations, NULL, $debug);
82
    $response = new CacheableJsonResponse($result);
83
    $response->addCacheableDependency($result);
84
    return $response;
85
  }
86
87
  /**
88
   * @param $schema
89
   * @param $operations
90
   * @param bool $debug
91
   *
92
   * @return \Drupal\Core\Cache\CacheableJsonResponse
93
   */
94
  protected function handleBatch($schema, $operations, $debug = FALSE) {
95
    $result = $this->processor->processQuery($schema, $operations, NULL, $debug);
96
    $response = new CacheableJsonResponse($result);
97
98
    // In case of a batch request, the result is an array.
99
    $dependencies = is_array($operations) ? $result : [$result];
100
    foreach ($dependencies as $dependency) {
0 ignored issues
show
Bug introduced by
The expression $dependencies of type object<Exception>|object...n>|object<Throwable>"}> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
101
      $response->addCacheableDependency($dependency);
102
    }
103
104
    return $response;
105
  }
106
107
}
108