Completed
Pull Request — 8.x-3.x (#448)
by Sebastian
03:12
created

QueryProvider::addQueryProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql\QueryProvider;
4
5
class QueryProvider implements QueryProviderInterface {
6
7
  /**
8
   * Unsorted list of query providers nested and keyed by priority.
9
   *
10
   * @var \Drupal\graphql\QueryProvider\QueryProviderInterface[]
11
   */
12
  protected $providers = [];
13
14
  /**
15
   * Sorted list of query providers.
16
   *
17
   * @var \Drupal\graphql\QueryProvider\QueryProviderInterface[]
18
   */
19
  protected $sortedProviders;
20
21
  /**
22
   * {@inheritdoc}
23
   */
24
  public function getQuery(array $params) {
25
    foreach ($this->getSortedProviders() as $provider) {
26
      if ($query = $provider->getQuery($params)) {
27
        return $query;
28
      }
29
    }
30
31
    return NULL;
32
  }
33
34
  /**
35
   * Adds a query provider.
36
   *
37
   * @param \Drupal\graphql\QueryProvider\QueryProviderInterface $provider
38
   *   The query provider to add.
39
   * @param int $priority
40
   *   Priority of the query provider.
41
   */
42
  public function addQueryProvider(QueryProviderInterface $provider, $priority = 0) {
43
    $this->providers[$priority][] = $provider;
44
    $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...
45
  }
46
47
  /**
48
   * Returns the sorted array of query providers.
49
   *
50
   * @return \Drupal\graphql\QueryProvider\QueryProviderInterface[]
51
   *   An array of query provider objects.
52
   */
53
  protected function getSortedProviders() {
54
    if (!isset($this->sortedProviders)) {
55
      krsort($this->providers);
56
57
      $this->sortedProviders = [];
58
      foreach ($this->providers as $providers) {
59
        $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...
60
      }
61
    }
62
63
    return $this->sortedProviders;
64
  }
65
}
66