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

QueryProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 61
rs 10
wmc 7
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getQuery() 0 9 3
A addQueryProvider() 0 4 1
A getSortedProviders() 0 12 3
1
<?php
2
3
namespace Drupal\graphql\GraphQL\QueryProvider;
4
5
use GraphQL\Server\OperationParams;
6
7
class QueryProvider implements QueryProviderInterface {
8
9
  /**
10
   * Unsorted list of query providers nested and keyed by priority.
11
   *
12
   * @var \Drupal\graphql\GraphQL\QueryProvider\QueryProviderInterface[]
13
   */
14
  protected $providers = [];
15
16
  /**
17
   * Sorted list of query providers.
18
   *
19
   * @var \Drupal\graphql\GraphQL\QueryProvider\QueryProviderInterface[]
20
   */
21
  protected $sortedProviders;
22
23
  /**
24
   * {@inheritdoc}
25
   */
26
  public function getQuery($id, OperationParams $operation) {
27
    foreach ($this->getSortedProviders() as $provider) {
28
      if ($query = $provider->getQuery($id, $operation)) {
29
        return $query;
30
      }
31
    }
32
33
    return NULL;
34
  }
35
36
  /**
37
   * Adds a query provider.
38
   *
39
   * @param \Drupal\graphql\GraphQL\QueryProvider\QueryProviderInterface $provider
40
   *   The query provider to add.
41
   * @param int $priority
42
   *   Priority of the query provider.
43
   */
44
  public function addQueryProvider(QueryProviderInterface $provider, $priority = 0) {
45
    $this->providers[$priority][] = $provider;
46
    $this->sortedProviders = NULL;
0 ignored issues
show
Documentation Bug introduced by
It seems like NULL of type null is incompatible with the declared type array<integer,object<Dru...ueryProviderInterface>> of property $sortedProviders.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
47
  }
48
49
  /**
50
   * Returns the sorted array of query providers.
51
   *
52
   * @return \Drupal\graphql\GraphQL\QueryProvider\QueryProviderInterface[]
53
   *   An array of query provider objects.
54
   */
55
  protected function getSortedProviders() {
56
    if (!isset($this->sortedProviders)) {
57
      krsort($this->providers);
58
59
      $this->sortedProviders = [];
60
      foreach ($this->providers as $providers) {
61
        $this->sortedProviders = array_merge($this->sortedProviders, $providers);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->sortedProviders, $providers) of type array is incompatible with the declared type array<integer,object<Dru...ueryProviderInterface>> of property $sortedProviders.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
62
      }
63
    }
64
65
    return $this->sortedProviders;
66
  }
67
}
68