Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 22 | class SchemaLoader { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The cache contexts manager service. |
||
| 26 | * |
||
| 27 | * @var \Drupal\Core\Cache\Context\CacheContextsManager |
||
| 28 | */ |
||
| 29 | protected $contextsManager; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The schema plugin manager service. |
||
| 33 | * |
||
| 34 | * @var \Drupal\graphql\Plugin\GraphQL\SchemaPluginManager |
||
| 35 | */ |
||
| 36 | protected $schemaManager; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The schema cache backend. |
||
| 40 | * |
||
| 41 | * @var \Drupal\Core\Cache\CacheBackendInterface |
||
| 42 | */ |
||
| 43 | protected $schemaCache; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The cache metadata cache. |
||
| 47 | * |
||
| 48 | * @var \Drupal\Core\Cache\CacheBackendInterface |
||
| 49 | */ |
||
| 50 | protected $metadataCache; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The service configuration. |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected $config; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The request stack service. |
||
| 61 | * |
||
| 62 | * @var \Symfony\Component\HttpFoundation\RequestStack |
||
| 63 | */ |
||
| 64 | protected $requestStack; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Static cache of loaded schemas. |
||
| 68 | * |
||
| 69 | * @var \Youshido\GraphQL\Schema\AbstractSchema[] |
||
| 70 | */ |
||
| 71 | protected $schemas = []; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Static cache of loaded cache metadata. |
||
| 75 | * |
||
| 76 | * @var \Drupal\Core\Cache\RefinableCacheableDependencyInterface[] |
||
| 77 | */ |
||
| 78 | protected $metadata = []; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * SchemaLoader constructor. |
||
| 82 | * |
||
| 83 | * @param \Drupal\Core\Cache\Context\CacheContextsManager $contextsManager |
||
| 84 | * The cache contexts manager service. |
||
| 85 | * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack |
||
| 86 | * The request stack service. |
||
| 87 | * @param \Drupal\graphql\Plugin\GraphQL\SchemaPluginManager $schemaManager |
||
| 88 | * The schema plugin manager service. |
||
| 89 | * @param \Drupal\Core\Cache\CacheBackendInterface $schemaCache |
||
| 90 | * The schema cache backend. |
||
| 91 | * @param \Drupal\Core\Cache\CacheBackendInterface $metadataCache |
||
| 92 | * The metadata cache backend. |
||
| 93 | * @param array $config |
||
| 94 | * The configuration provided through the services.yml. |
||
| 95 | */ |
||
| 96 | public function __construct( |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Loads and caches the generated schema. |
||
| 114 | * |
||
| 115 | * @param string $name |
||
| 116 | * The name of the schema to load. |
||
| 117 | * |
||
| 118 | * @return \Youshido\GraphQL\Schema\AbstractSchema |
||
| 119 | * The generated GraphQL schema. |
||
| 120 | */ |
||
| 121 | public function getSchema($name) { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Retrieves the schema's cache metadata. |
||
| 156 | * |
||
| 157 | * @param string $name |
||
| 158 | * The name of the schema. |
||
| 159 | * @return \Drupal\Core\Cache\RefinableCacheableDependencyInterface |
||
| 160 | * The cache metadata for the schema. |
||
| 161 | */ |
||
| 162 | public function getCacheMetadata($name) { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Collects schema cache metadata from all types registered with the schema. |
||
| 195 | * |
||
| 196 | * The cache metadata is statically cached. This means that the schema may not |
||
| 197 | * be modified after this method has been called. |
||
| 198 | * |
||
| 199 | * @param \Youshido\GraphQL\Schema\AbstractSchema $schema |
||
| 200 | * The schema to extract the cache metadata from. |
||
| 201 | * |
||
| 202 | * @return \Drupal\Core\Cache\CacheableMetadata |
||
| 203 | * The cache metadata collected from the schema's types. |
||
| 204 | */ |
||
| 205 | protected function extractCacheMetadata(AbstractSchema $schema) { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Maps a max age value to an "expire" value for the Cache API. |
||
| 228 | * |
||
| 229 | * @param int $maxAge |
||
| 230 | * A max age value. |
||
| 231 | * |
||
| 232 | * @return int |
||
| 233 | * A corresponding "expire" value. |
||
| 234 | * |
||
| 235 | * @see \Drupal\Core\Cache\CacheBackendInterface::set() |
||
| 236 | */ |
||
| 237 | View Code Duplication | protected function maxAgeToExpire($maxAge) { |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Generates a cache identifier for the passed cache contexts. |
||
| 247 | * |
||
| 248 | * @param string $name |
||
| 249 | * The name of the schema. |
||
| 250 | * @param \Drupal\Core\Cache\CacheableDependencyInterface $metadata |
||
| 251 | * Optional array of cache context tokens. |
||
| 252 | * |
||
| 253 | * @return string The generated cache identifier. |
||
| 254 | * The generated cache identifier. |
||
| 255 | */ |
||
| 256 | protected function getCacheIdentifier($name, CacheableDependencyInterface $metadata) { |
||
| 261 | |||
| 262 | } |
||
| 263 |