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

ImageStyleId::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 4
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\GraphQL\Enums\Images;
4
5
use Drupal\Core\Entity\EntityTypeManagerInterface;
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
 * @GraphQLEnum(
13
 *   id = "image_style_id",
14
 *   name = "ImageStyleId",
15
 *   provider = "image"
16
 * )
17
 */
18
class ImageStyleId 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...
19
20
  /**
21
   * The entity type manager service.
22
   *
23
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
24
   */
25
  protected $entityTypeManager;
26
27
  /**
28
   * {@inheritdoc}
29
   */
30
  public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
31
    return new static(
32
      $configuration,
33
      $pluginId,
34
      $pluginDefinition,
35
      $container->get('entity_type.manager')
36
    );
37
  }
38
39
  /**
40
   * ImageStyleId constructor.
41
   *
42
   * @param array $configuration
43
   *   The plugin configuration array.
44
   * @param string $pluginId
45
   *   The plugin id.
46
   * @param array $pluginDefinition
47
   *   The plugin definition array.
48
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
49
   *   The entity type manager.
50
   */
51
  public function __construct(array $configuration, $pluginId, $pluginDefinition, EntityTypeManagerInterface $entityTypeManager) {
52
    parent::__construct($configuration, $pluginId, $pluginDefinition);
53
    $this->entityTypeManager = $entityTypeManager;
54
  }
55
56
  /**
57
   * {@inheritdoc}
58
   */
59
  public function buildEnumValues($definition) {
60
    $items = [];
61
62
    $storage = $this->entityTypeManager->getStorage('image_style');
63
    foreach ($storage->loadMultiple() as $imageStyle) {
64
      $items[strtoupper($imageStyle->id())] = [
65
        'value' => $imageStyle->id(),
66
        'description' => $imageStyle->label(),
67
      ];
68
    }
69
70
    return $items;
71
  }
72
73
}
74