Completed
Pull Request — 8.x-3.x (#404)
by Sebastian
04:12 queued 01:48
created

ConfigurationRoutes   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 46
loc 46
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 3 3 1
A alterRoutes() 19 19 3

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace Drupal\graphql\Routing;
4
5
use Drupal\Core\Routing\RouteSubscriberBase;
6
use Drupal\graphql\Plugin\GraphQL\SchemaPluginManager;
7
use Symfony\Component\Routing\Route;
8
use Symfony\Component\Routing\RouteCollection;
9
10
/**
11
 * Registers routes for all configurable schemas.
12
 */
13 View Code Duplication
class ConfigurationRoutes extends RouteSubscriberBase {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
14
15
  /**
16
   * The graphql schema plugin manager.
17
   *
18
   * @var \Drupal\graphql\Plugin\GraphQL\SchemaPluginManager
19
   */
20
  protected $schemaManager;
21
22
  /**
23
   * Constructs a QueryRoutes object.
24
   *
25
   * @param \Drupal\graphql\Plugin\GraphQL\SchemaPluginManager $schemaManager
26
   *   The graphql schema plugin manager.
27
   */
28
  public function __construct(SchemaPluginManager $schemaManager) {
29
    $this->schemaManager = $schemaManager;
30
  }
31
32
  /**
33
   * Alters existing routes for a specific collection.
34
   *
35
   * @param \Symfony\Component\Routing\RouteCollection $collection
36
   *   The route collection for adding routes.
37
   */
38
  protected function alterRoutes(RouteCollection $collection) {
39
    $routes = new RouteCollection();
40
41
    foreach ($this->schemaManager->getDefinitions() as $key => $definition) {
42
      if (empty($definition['uses_plugins'])) {
43
        continue;
44
      }
45
46
      $routes->add("graphql.configuration.$key", new Route("{$definition['path']}/configure", [
47
        'schema' => $key,
48
        '_controller' => '\Drupal\graphql\Controller\ConfigurationController::configurationOverview',
49
        '_title' => 'Configuration',
50
      ], [
51
        '_admin_route' => 'TRUE',
52
      ]));
53
    }
54
55
    $collection->addCollection($routes);
56
  }
57
58
}
59