Completed
Pull Request — 8.x-3.x (#495)
by Sebastian
06:33
created

LanguageId::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 4
dl 0
loc 4
rs 10
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
   * LanguageId constructor.
30
   *
31
   * @param array $configuration
32
   *   The plugin configuration array.
33
   * @param string $pluginId
34
   *   The plugin id.
35
   * @param array $pluginDefinition
36
   *   The plugin definition array.
37
   * @param \Drupal\Core\Language\LanguageManagerInterface $languageManager
38
   *   The language manager service.
39
   */
40
  public function __construct(array $configuration, $pluginId, $pluginDefinition, LanguageManagerInterface $languageManager) {
41
    parent::__construct($configuration, $pluginId, $pluginDefinition);
42
    $this->languageManager = $languageManager;
43
  }
44
45
  /**
46
   * {@inheritdoc}
47
   */
48
  public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
49
    return new static(
50
      $configuration,
51
      $pluginId,
52
      $pluginDefinition,
53
      $container->get('language_manager')
54
    );
55
  }
56
57
  /**
58
   * {@inheritdoc}
59
   */
60
  public function buildValues(PluggableSchemaBuilderInterface $schemaBuilder) {
61
    $values = [];
62
63
    foreach ($this->languageManager->getLanguages() as $language) {
64
      $values[] = [
65
        'name' => str_replace('-', '_', $language->getId()),
66
        'value' => $language->getId(),
67
        'description' => $language->getName(),
68
      ];
69
    }
70
71
    return $values;
72
  }
73
74
}
75