DisplayModeIdDeriver   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 23
c 1
b 0
f 0
dl 0
loc 66
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 2 1
A __construct() 0 2 1
A getDisplayModes() 0 16 1
A getDerivativeDefinitions() 0 12 2
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\Deriver\Enums;
4
5
use Drupal\Component\Plugin\Derivative\DeriverBase;
0 ignored issues
show
Bug introduced by
The type Drupal\Component\Plugin\Derivative\DeriverBase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Drupal\Core\Entity\EntityTypeManagerInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Entity\EntityTypeManagerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Drupal\Core\Entity\EntityViewModeInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Entity\EntityViewModeInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Plugin\Disco...ntainerDeriverInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Drupal\Core\StringTranslation\StringTranslationTrait;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\StringTransl...\StringTranslationTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Drupal\graphql\Utility\StringHelper;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Depend...tion\ContainerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
class DisplayModeIdDeriver extends DeriverBase implements ContainerDeriverInterface {
14
  use StringTranslationTrait;
15
16
  /**
17
   * Entity type manager.
18
   *
19
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
20
   */
21
  protected $entityTypeManager;
22
23
  /**
24
   * {@inheritdoc}
25
   */
26
  public static function create(ContainerInterface $container, $basePluginId) {
27
    return new static($container->get('entity_type.manager'));
28
  }
29
30
  /**
31
   * DisplayModeIdDeriver constructor.
32
   *
33
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
34
   *   The entity type manager service.
35
   */
36
  public function __construct(EntityTypeManagerInterface $entityTypeManager) {
37
    $this->entityTypeManager = $entityTypeManager;
38
  }
39
40
  /**
41
   * {@inheritdoc}
42
   */
43
  public function getDerivativeDefinitions($basePluginDefinition) {
44
    foreach ($this->getDisplayModes() as $targetType => $displayModes) {
45
      $this->derivatives[$targetType] = [
0 ignored issues
show
Bug Best Practice introduced by
The property derivatives does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
46
        'name' => StringHelper::camelCase($targetType, 'display', 'mode', 'id'),
47
        'description' => $this->t("The available display modes for '@type' entities.", [
48
          '@type' => $this->entityTypeManager->getDefinition($targetType)->getLabel(),
49
        ]),
50
        'values' => $displayModes,
51
      ] + $basePluginDefinition;
52
    }
53
54
    return parent::getDerivativeDefinitions($basePluginDefinition);
55
  }
56
57
  /**
58
   * Retrieves a list of entity view modes grouped by their target type.
59
   *
60
   * @return array
61
   *   The list of entity view modes grouped by the target entity type.
62
   */
63
  protected function getDisplayModes() {
64
    $storage = $this->entityTypeManager->getStorage('entity_view_mode');
65
    return array_reduce($storage->loadMultiple(), function ($carry, EntityViewModeInterface $current) {
66
      $target = $current->getTargetType();
67
      list(, $id) = explode('.', $current->id());
68
69
      $carry[$target][StringHelper::upperCase($id)] = [
70
        'value' => $id,
71
        'description' => $this->t("The '@label' display mode for '@type' entities.", [
72
          '@label' => $current->label(),
73
          '@type' => $this->entityTypeManager->getDefinition($target)->getLabel(),
74
        ]),
75
      ];
76
77
      return $carry;
78
    }, []);
79
  }
80
81
}
82