Completed
Pull Request — 8.x-1.x (#127)
by Janez
07:01 queued 24s
created

EntityBrowserFormController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 90
Duplicated Lines 11.11 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 1
dl 10
loc 90
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A create() 10 10 1
A getFormObject() 0 3 1
A title() 0 4 1
A loadBrowser() 0 6 1

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
/**
4
 * @file
5
 * Contains \Drupal\entity_browser\Controllers\EntityBrowserFormController.
6
 */
7
8
namespace Drupal\entity_browser\Controllers;
9
10
use Drupal\Component\Utility\Xss;
11
use Drupal\Core\Controller\ControllerResolverInterface;
12
use Drupal\Core\Controller\HtmlFormController;
13
use Drupal\Core\DependencyInjection\ClassResolverInterface;
14
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
15
use Drupal\Core\Entity\EntityManagerInterface;
16
use Drupal\Core\Form\FormBuilderInterface;
17
use Drupal\Core\Routing\RouteMatchInterface;
18
use Symfony\Component\DependencyInjection\ContainerInterface;
19
use Symfony\Component\HttpFoundation\Request;
20
21
/**
22
 * Standalone entity browser page.
23
 */
24
class EntityBrowserFormController extends HtmlFormController implements ContainerInjectionInterface {
25
26
  /**
27
   * Current route match service.
28
   *
29
   * @var \Drupal\Core\Routing\RouteMatchInterface
30
   */
31
  protected $currentRouteMatch;
32
33
  /**
34
   * The browser storage.
35
   *
36
   * @var \Drupal\Core\Entity\EntityStorageInterface
37
   */
38
  protected $browserStorage;
39
40
  /**
41
   * Current request.
42
   *
43
   * @var \Symfony\Component\HttpFoundation\Request
44
   */
45
  protected $request;
46
47
  /**
48
   * Constructs Entity browser form controller.
49
   *
50
   * @param \Drupal\Core\Controller\ControllerResolverInterface $controller_resolver
51
   *   The controller resolver.
52
   * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
53
   *   The form builder.
54
   * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
55
   *   The class resolver.
56
   * @param RouteMatchInterface $route_match
57
   *   Current route match service.
58
   * @param EntityManagerInterface $entity_manager
59
   *   Entity manager service.
60
   * @param \Symfony\Component\HttpFoundation\Request $request
61
   *   Current request.
62
   */
63
  public function __construct(ControllerResolverInterface $controller_resolver, FormBuilderInterface $form_builder, ClassResolverInterface $class_resolver, RouteMatchInterface $route_match, EntityManagerInterface $entity_manager, Request $request) {
64
    parent::__construct($controller_resolver, $form_builder, $class_resolver);
65
    $this->currentRouteMatch = $route_match;
66
    $this->browserStorage = $entity_manager->getStorage('entity_browser');
67
    $this->request = $request;
68
    $this->formBuilder = $form_builder;
69
  }
70
71
  /**
72
   * {@inheritdoc}
73
   */
74 View Code Duplication
  public static function create(ContainerInterface $container) {
0 ignored issues
show
Duplication introduced by
This method 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...
75
    return new static(
76
      $container->get('controller_resolver'),
77
      $container->get('form_builder'),
78
      $container->get('class_resolver'),
79
      $container->get('current_route_match'),
80
      $container->get('entity.manager'),
81
      $container->get('request_stack')->getCurrentRequest()
82
    );
83
  }
84
85
  /**
86
   * {@inheritdoc}
87
   */
88
  protected function getFormObject(RouteMatchInterface $route_match, $form_arg) {
89
    return $this->loadBrowser()->getFormObject();
90
  }
91
92
  /**
93
   * Standalone entity browser title callback.
94
   */
95
  public function title() {
96
    $browser = $this->loadBrowser();
97
    return Xss::filter($browser->label());
98
  }
99
100
  /**
101
   * Loads entity browser object for this page.
102
   *
103
   * @return \Drupal\entity_browser\EntityBrowserInterface
104
   *   Loads the entity browser object
105
   */
106
  protected function loadBrowser() {
107
    /** @var $route \Symfony\Component\Routing\Route */
108
    $route = $this->currentRouteMatch->getRouteObject();
109
    /** @var $browser \Drupal\entity_browser\EntityBrowserInterface */
110
    return $this->browserStorage->load($route->getDefault('entity_browser_id'));
111
  }
112
113
}
114