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

JsonQueryMapQueryProvider::getQuery()   B

Complexity

Conditions 9
Paths 7

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 11
c 0
b 0
f 0
nc 7
nop 2
dl 0
loc 21
rs 7.041
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 JsonQueryMapQueryProvider 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
    list($version, $id) = explode(':', $id);
44
45
    // Check that the id is properly formatted.
46
    if (empty($version) || empty($id)) {
47
      return NULL;
48
    }
49
50
    if (!(($cache = $this->cacheBackend->get('graphql_query_map_json_versions')) && ($versions = $cache->data) !== NULL)) {
51
      $this->cacheBackend->set('graphql_query_map_json_versions', $versions = $this->discoverQueryMaps());
52
    }
53
54
    if (isset($versions) && isset($versions[$version]) && file_exists($versions[$version])) {
55
      $contents = json_decode(file_get_contents($versions[$version]), TRUE);
56
      if ($query = array_search($id, $contents)) {
57
        return $query;
58
      }
59
    }
60
61
    return NULL;
62
  }
63
64
  /**
65
   * Discovers the available query maps within the configured lookup paths.
66
   *
67
   * @return array
68
   *   An associative array of query maps with the query map versions as keys.
69
   */
70
  protected function discoverQueryMaps() {
71
    $maps = [];
72
    foreach ($this->lookupPaths as $path) {
73
      if (is_dir($path)) {
74
        $iterator = new RegexDirectoryIterator($path, '/\.json/i');
75
76
        /** @var \SplFileInfo $file */
77
        foreach ($iterator as $file) {
78
          $hash = sha1(file_get_contents($file->getPathname()));
79
          $maps[$hash] = $file->getPathname();
80
        }
81
      }
82
83
      if (is_file($path)) {
84
        $file = new \SplFileInfo($path);
85
        $hash = sha1(file_get_contents($file->getPathname()));
86
        $maps[$hash] = $file->getPathname();
87
      }
88
    }
89
90
    return $maps;
91
  }
92
}
93