Completed
Pull Request — 8.x-3.x (#401)
by Sebastian
02:30
created

SchemaPluginBase::constructCacheMetadata()   C

Complexity

Conditions 8
Paths 9

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 15
nc 9
nop 3
dl 0
loc 27
rs 5.3846
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql\Plugin\GraphQL\Schemas;
4
5
use Drupal\Core\Cache\Cache;
6
use Drupal\Core\Cache\CacheableMetadata;
7
use Drupal\graphql\GraphQL\Utility\TypeCollector;
8
use Drupal\graphql\Plugin\GraphQL\SchemaPluginInterface;
9
use Drupal\graphql\Plugin\GraphQL\Traits\PluginTrait;
10
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface;
11
use Youshido\GraphQL\Config\Schema\SchemaConfig;
12
use Youshido\GraphQL\Schema\AbstractSchema;
13
use Youshido\GraphQL\Type\InputObject\AbstractInputObjectType;
14
use Youshido\GraphQL\Type\InterfaceType\AbstractInterfaceType;
15
use Youshido\GraphQL\Type\Object\AbstractObjectType;
16
17
abstract class SchemaPluginBase extends AbstractSchema implements SchemaPluginInterface {
18
19
  use PluginTrait;
20
21
  /**
22
   * The response cache metadata object.
23
   *
24
   * @var \Drupal\Core\Cache\CacheableMetadata
25
   */
26
  protected $responseMetadata;
27
28
  /**
29
   * The schema cache metadata object.
30
   *
31
   * @var \Drupal\Core\Cache\CacheableMetadata
32
   */
33
  protected $schemaMetadata;
34
35
  /**
36
   * {@inheritdoc}
37
   */
38
  public function __construct($configuration, $pluginId, $pluginDefinition) {
39
    $this->constructPlugin($configuration, $pluginId, $pluginDefinition);
40
    $this->constructSchema($configuration, $pluginId, $pluginDefinition);
41
    $this->constructCacheMetadata($configuration, $pluginId, $pluginDefinition);
42
  }
43
44
  /**
45
   * Constructs the schema configuration.
46
   *
47
   * @param array $configuration
48
   *   The plugin configuration array.
49
   * @param string $pluginId
50
   *   The plugin id.
51
   * @param array $pluginDefinition
52
   *   The plugin definition array.
53
   */
54
  abstract protected function constructSchema($configuration, $pluginId, $pluginDefinition);
55
56
  /**
57
   * Constructs the schema cache metadata.
58
   *
59
   * @param array $configuration
60
   *   The plugin configuration array.
61
   * @param string $pluginId
62
   *   The plugin id.
63
   * @param array $pluginDefinition
64
   *   The plugin definition array.
65
   */
66
  protected function constructCacheMetadata($configuration, $pluginId, $pluginDefinition) {
67
    // Build the schema and response metadata objects based on the provided
68
    // schema config and all included types/fields/etc.
69
    $this->responseMetadata = new CacheableMetadata();
70
    $this->responseMetadata->setCacheMaxAge(Cache::PERMANENT);
71
    $this->schemaMetadata = new CacheableMetadata();
72
    $this->schemaMetadata->setCacheMaxAge(Cache::PERMANENT);
73
74
    foreach (TypeCollector::collectTypes($this) as $type) {
75
      if ($type instanceof TypeSystemPluginInterface) {
76
        $this->schemaMetadata->addCacheableDependency($type->getSchemaCacheMetadata());
77
        $this->responseMetadata->addCacheableDependency($type->getResponseCacheMetadata());
78
      }
79
80
      if ($type instanceof AbstractObjectType || $type instanceof AbstractInputObjectType || $type instanceof AbstractInterfaceType) {
81
        foreach ($type->getFields() as $field) {
82
          if ($field instanceof TypeSystemPluginInterface) {
83
            $this->schemaMetadata->addCacheableDependency($field->getSchemaCacheMetadata());
84
            $this->responseMetadata->addCacheableDependency($field->getResponseCacheMetadata());
85
          }
86
        }
87
      }
88
    }
89
90
    // Merge the schema cache metadata into the response cache metadata.
91
    $this->responseMetadata->addCacheableDependency($this->schemaMetadata);
92
  }
93
94
  /**
95
   * Collects schema cache metadata from all types registered with the schema.
96
   *
97
   * The cache metadata is statically cached. This means that the schema may not
98
   * be modified after this method has been called.
99
   *
100
   * @return \Drupal\Core\Cache\CacheableMetadata
101
   *   The cache metadata collected from the schema's types.
102
   */
103
  public function getSchemaCacheMetadata() {
104
    return $this->schemaMetadata;
105
  }
106
107
  /**
108
   * Collects result cache metadata from all types registered with the schema.
109
   *
110
   * The cache metadata is statically cached. This means that the schema may not
111
   * be modified after this method has been called.
112
   *
113
   * @return \Drupal\Core\Cache\CacheableMetadata
114
   *   The cache metadata collected from the schema's types.
115
   */
116
  public function getResponseCacheMetadata() {
117
    return $this->responseMetadata;
118
  }
119
120
  /**
121
   * {@inheritdoc}
122
   */
123
  public function build(SchemaConfig $config) {
124
    // Not needed anymore.
125
  }
126
127
}
128