Completed
Pull Request — 8.x-3.x (#949)
by
unknown
01:25
created

GqlQueryMapQueryProvider::discoverQueryMaps()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 22

Duplication

Lines 9
Ratio 40.91 %

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 0
dl 9
loc 22
rs 9.2568
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql\GraphQL\QueryProvider;
4
5
use Drupal\Component\FileSystem\RegexDirectoryIterator;
6
use Drupal\Core\Cache\CacheBackendInterface;
7
use Drupal\Core\Config\ConfigFactoryInterface;
8
use GraphQL\Server\OperationParams;
9
10
class GqlQueryMapQueryProvider implements QueryProviderInterface {
11
12
  /**
13
   * The cache backend for storing query map file paths.
14
   *
15
   * @var \Drupal\Core\Cache\CacheBackendInterface
16
   */
17
  protected $cacheBackend;
18
19
  /**
20
   * The paths to use for finding query maps.
21
   *
22
   * @var string[]
23
   */
24
  protected $lookupPaths;
25
26
  /**
27
   * QueryProvider constructor.
28
   *
29
   * @param \Drupal\Core\Cache\CacheBackendInterface $cacheBackend
30
   *   The cache backend for storing query map file paths.
31
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
32
   *   The config factory.
33
   */
34
  public function __construct(CacheBackendInterface $cacheBackend, ConfigFactoryInterface $configFactory) {
35
    $this->lookupPaths = $configFactory->get('graphql.query_map_json.config')->get('lookup_paths') ?: [];
36
    $this->cacheBackend = $cacheBackend;
37
  }
38
39
  /**
40
   * {@inheritdoc}
41
   */
42
  public function getQuery($id, OperationParams $operation) {
43
    // Check that the id is properly formatted.
44
    if (empty($id)) {
45
      return NULL;
46
    }
47
48 View Code Duplication
    if (!(($cache = $this->cacheBackend->get('graphql_query_map_gql_versions')) && ($versions = $cache->data) !== NULL)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
      $this->cacheBackend->set('graphql_query_map_gql_versions', $versions = $this->discoverQueryMaps());
50
    }
51
52
    if (isset($versions) && isset($versions[$id]) && file_exists($versions[$id])) {
53
      return file_get_contents($versions[$id]);
54
    }
55
56
    return NULL;
57
  }
58
59
  /**
60
   * Discovers the available query maps within the configured lookup paths.
61
   *
62
   * @return array
63
   *   An associative array of query maps with the query map versions as keys.
64
   */
65
  protected function discoverQueryMaps() {
66
    $maps = [];
67
    foreach ($this->lookupPaths as $path) {
68 View Code Duplication
      if (is_dir($path)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
        $iterator = new RegexDirectoryIterator($path, '/\.gql/i');
70
71
        /** @var \SplFileInfo $file */
72
        foreach ($iterator as $file) {
73
          $filename = $file->getBasename('.gql');
74
          $maps[$filename] = $file->getPathname();
75
        }
76
      }
77
78
      if (is_file($path)) {
79
        $file = new \SplFileInfo($path);
80
        $filename = $file->getBasename('.gql');
81
        $maps[$filename] = $file->getPathname();
82
      }
83
    }
84
85
    return $maps;
86
  }
87
}
88