Issues (645)

QueryProvider/JsonQueryMapQueryProvider.php (3 issues)

Labels
Severity
1
<?php
2
3
namespace Drupal\graphql\GraphQL\QueryProvider;
4
5
use Drupal\Component\FileSystem\RegexDirectoryIterator;
0 ignored issues
show
The type Drupal\Component\FileSystem\RegexDirectoryIterator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Drupal\Core\Cache\CacheBackendInterface;
0 ignored issues
show
The type Drupal\Core\Cache\CacheBackendInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Drupal\Core\Config\ConfigFactoryInterface;
0 ignored issues
show
The type Drupal\Core\Config\ConfigFactoryInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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