RouteSubscriber::routes()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 0
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\entity_browser;
4
5
use Drupal\Core\Entity\EntityTypeManagerInterface;
6
use Drupal\Core\Entity\Query\QueryFactory;
7
use Symfony\Component\Routing\RouteCollection;
8
9
/**
10
 * Generates routes for entity browsers.
11
 */
12
class RouteSubscriber {
13
14
  /**
15
   * The entity browser storage.
16
   *
17
   * @var \Drupal\Core\Entity\EntityStorageInterface
18
   */
19
  protected $browserStorage;
20
21
  /**
22
   * Display plugin manager.
23
   *
24
   * @var \Drupal\entity_browser\DisplayManager
25
   */
26
  protected $displayManager;
27
28
  /**
29
   * Entity browser query.
30
   *
31
   * @var \Drupal\Core\Entity\Query\QueryInterface
32
   */
33
  protected $browserQuery;
34
35
  /**
36
   * Constructs a \Drupal\views\EventSubscriber\RouteSubscriber instance.
37
   *
38
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
39
   *   The entity manager.
40
   * @param \Drupal\entity_browser\DisplayManager $display_manager
41
   *   The display manager.
42
   * @param \Drupal\Core\Entity\Query\QueryFactory $entity_query
43
   *   The entity query factory.
44
   */
45
  public function __construct(EntityTypeManagerInterface $entity_type_manager, DisplayManager $display_manager, QueryFactory $entity_query) {
46
    $this->browserStorage = $entity_type_manager->getStorage('entity_browser');
47
    $this->displayManager = $display_manager;
48
    $this->browserQuery = $entity_query->get('entity_browser');
49
  }
50
51
  /**
52
   * Returns a set of route objects.
53
   *
54
   * @return \Symfony\Component\Routing\RouteCollection
55
   *   A route collection.
56
   */
57
  public function routes() {
58
    $collection = new RouteCollection();
59
    // Return $collection;.
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
60
    foreach ($this->getBrowserIDsWithRoute() as $id) {
61
      /** @var $browser \Drupal\entity_browser\EntityBrowserInterface */
62
      $browser = $this->browserStorage->load($id);
63
      if ($route = $browser->route()) {
64
        $collection->add('entity_browser.' . $browser->id(), $route);
65
      }
66
    }
67
68
    return $collection;
69
  }
70
71
  /**
72
   * Gets entity browser IDs that use routes.
73
   *
74
   * @return array
75
   *   Array of browser IDs.
76
   */
77
  protected function getBrowserIDsWithRoute() {
78
    // Get all display plugins which provides the type.
79
    $display_plugins = $this->displayManager->getDefinitions();
80
    $ids = [];
81
    foreach ($display_plugins as $id => $definition) {
82
      if (!empty($definition['uses_route'])) {
83
        $ids[$id] = $id;
84
      }
85
    }
86
87
    return $this->browserQuery
88
      ->condition('status', TRUE)
89
      ->condition("display", $ids, 'IN')
90
      ->execute();
91
  }
92
93
}
94