Completed
Pull Request — 8.x-3.x (#525)
by Philipp
02:08
created

LanguageId::buildEnumValues()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\GraphQL\Enums\Languages;
4
5
use Drupal\Core\Language\LanguageManagerInterface;
6
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
7
use Drupal\graphql\Plugin\GraphQL\Enums\EnumPluginBase;
8
use Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
11
/**
12
 * Generates an enumeration of numbers.
13
 *
14
 * @GraphQLEnum(
15
 *   id = "language_id",
16
 *   name = "LanguageId"
17
 * )
18
 */
19
class LanguageId extends EnumPluginBase implements ContainerFactoryPluginInterface {
0 ignored issues
show
Bug introduced by
There is one abstract method getPluginDefinition in this class; you could implement it, or declare this class as abstract.
Loading history...
20
21
  /**
22
   * The language manager.
23
   *
24
   * @var \Drupal\Core\Language\LanguageManagerInterface
25
   */
26
  protected $languageManager;
27
28
  /**
29
   * {@inheritdoc}
30
   */
31
  public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
32
    return new static(
33
      $configuration,
34
      $pluginId,
35
      $pluginDefinition,
36
      $container->get('language_manager')
37
    );
38
  }
39
40
  /**
41
   * LanguageId constructor.
42
   *
43
   * @param array $configuration
44
   *   The plugin configuration array.
45
   * @param string $pluginId
46
   *   The plugin id.
47
   * @param array $pluginDefinition
48
   *   The plugin definition array.
49
   * @param \Drupal\Core\Language\LanguageManagerInterface $languageManager
50
   *   The language manager service.
51
   */
52
  public function __construct(array $configuration, $pluginId, $pluginDefinition, LanguageManagerInterface $languageManager) {
53
    parent::__construct($configuration, $pluginId, $pluginDefinition);
54
    $this->languageManager = $languageManager;
55
  }
56
57
  /**
58
   * {@inheritdoc}
59
   */
60
  public function buildEnumValues($definition) {
61
    $values = parent::buildEnumValues($definition);
62
63
    foreach ($this->languageManager->getLanguages() as $language) {
64
      $name = str_replace('-', '_', $language->getId());
65
      $values[strtoupper($name)] = [
66
        'value' => $language->getId(),
67
        'description' => $language->getName(),
68
      ];
69
    }
70
71
    return $values;
72
  }
73
}
74