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

TypePluginBase::applies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 3
rs 10
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\Plugin\GraphQL\PluggableSchemaBuilder;
7
use Drupal\graphql\Plugin\GraphQL\Traits\CacheablePluginTrait;
8
use Drupal\graphql\Plugin\GraphQL\Traits\DescribablePluginTrait;
9
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface;
10
use GraphQL\Type\Definition\ObjectType;
11
use GraphQL\Type\Definition\ResolveInfo;
12
13
abstract class TypePluginBase extends PluginBase implements TypeSystemPluginInterface {
14
  use CacheablePluginTrait;
15
  use DescribablePluginTrait;
16
17
  /**
18
   * {@inheritdoc}
19
   */
20
  public static function createInstance(PluggableSchemaBuilder $builder, $definition, $id) {
21
    return new ObjectType([
22
      'fields' => function () use ($builder, $definition) {
23
        $fields = $builder->getFieldsByType($definition['name']);
24
25
        if (!empty($definition['interfaces'])) {
26
          $inherited = array_map(function ($name) use ($builder) {
27
            return $builder->getFieldsByType($name);
28
          }, $definition['interfaces']);
29
30
          $inherited = call_user_func_array('array_merge', $inherited);
31
          return array_merge($inherited, $fields);
32
        }
33
34
        return $fields;
35
      },
36
      'interfaces' => function () use ($builder, $definition) {
37
        return array_map(function ($name) use ($builder) {
38
          return $builder->getTypeByName($name);
39
        }, $definition['interfaces']);
40
      },
41
      'isTypeOf' => function ($value, $context, ResolveInfo $info) use ($builder, $id) {
42
        $instance = $builder->getPluginInstance(GRAPHQL_TYPE_PLUGIN, $id);
43
        return $instance->applies($value, $context, $info);
44
      },
45
    ] + $definition);
46
  }
47
48
  /**
49
   * {@inheritdoc}
50
   */
51 View Code Duplication
  public function getDefinition() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
    $definition = $this->getPluginDefinition();
53
54
    return [
55
      'name' => $definition['name'],
56
      'description' => $this->buildDescription($definition),
57
      'interfaces' => $this->buildInterfaces($definition),
58
    ];
59
  }
60
61
  /**
62
   * @param $definition
63
   *
64
   * @return array
65
   */
66
  protected function buildInterfaces($definition) {
67
    return array_unique($definition['interfaces']);
68
  }
69
70
  /**
71
   * Checks whether this type applies to a given object.
72
   *
73
   * @param mixed $object
74
   *   The object to check against.
75
   * @param mixed $context
76
   *   The execution context.
77
   * @param \GraphQL\Type\Definition\ResolveInfo $info
78
   *   The resolve info object.
79
   *
80
   * @return null|bool
81
   *   TRUE if this type applies to the given object or FALSE if it doesn't.
82
   */
83
  public function applies($object, $context, ResolveInfo $info) {
84
    return NULL;
85
  }
86
87
}
88