Completed
Push — 8.x-3.x ( e67c0e...2e01b0 )
by Philipp
02:27
created

TypePluginBase::getDefinition()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 2
nop 1
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql\Plugin\GraphQL\Types;
4
5
use Drupal\Component\Plugin\PluginBase;
6
use Drupal\graphql\GraphQL\Type\InterfaceType;
7
use Drupal\graphql\GraphQL\Type\ObjectType;
8
use Drupal\graphql\Plugin\GraphQL\Interfaces\InterfacePluginBase;
9
use Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface;
10
use Drupal\graphql\Plugin\GraphQL\Traits\CacheablePluginTrait;
11
use Drupal\graphql\Plugin\GraphQL\Traits\FieldablePluginTrait;
12
use Drupal\graphql\Plugin\GraphQL\Traits\NamedPluginTrait;
13
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface;
14
15
/**
16
 * Base class for GraphQL type plugins.
17
 */
18
abstract class TypePluginBase extends PluginBase implements TypeSystemPluginInterface {
19
  use CacheablePluginTrait;
20
  use NamedPluginTrait;
21
  use FieldablePluginTrait;
22
23
  /**
24
   * The type instance.
25
   *
26
   * @var \Drupal\graphql\GraphQL\Type\ObjectType
27
   */
28
  protected $definition;
29
30
  /**
31
   * {@inheritdoc}
32
   */
33
  public function getDefinition(PluggableSchemaBuilderInterface $schemaBuilder) {
34
    if (!isset($this->definition)) {
35
      $interfaces = $this->buildInterfaces($schemaBuilder);
36
37
      $this->definition = new ObjectType($this, [
38
        'name' => $this->buildName(),
39
        'description' => $this->buildDescription(),
40
        'interfaces' => $interfaces,
41
      ]);
42
43
      $this->definition->addFields($this->buildFields($schemaBuilder));
44
45
      foreach ($interfaces as $interface) {
46
        if ($interface instanceof InterfaceType) {
47
          $interface->registerType($this->definition);
48
        }
49
      }
50
    }
51
52
    return $this->definition;
53
  }
54
55
  /**
56
   * Build the list of interfaces.
57
   *
58
   * @param \Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface $schemaBuilder
59
   *   Instance of the schema manager to resolve dependencies.
60
   *
61
   * @return \Youshido\GraphQL\Type\AbstractInterfaceTypeInterface[]
62
   *   The list of interfaces.
63
   */
64
  protected function buildInterfaces(PluggableSchemaBuilderInterface $schemaBuilder) {
65
    $definition = $this->getPluginDefinition();
66
    if ($definition['interfaces']) {
67
      return array_map(function (TypeSystemPluginInterface $interface) use ($schemaBuilder) {
68
        return $interface->getDefinition($schemaBuilder);
69
      }, array_filter($schemaBuilder->find(function ($interface) use ($definition) {
70
        return in_array($interface['name'], $definition['interfaces']);
71
      }, [GRAPHQL_INTERFACE_PLUGIN]), function ($interface) {
72
        return $interface instanceof InterfacePluginBase;
73
      }));
74
    }
75
76
    return [];
77
  }
78
79
  /**
80
   * Checks whether this type applies to a given object.
81
   *
82
   * @param mixed $object
83
   *   The object to check against.
84
   *
85
   * @return bool
86
   *   TRUE if this type applies to the given object, FALSE otherwise.
87
   */
88
  public function applies($object) {
89
    return FALSE;
90
  }
91
92
}
93