Completed
Pull Request — 8.x-3.x (#525)
by Sebastian
02:23
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\SchemaBuilder;
7
use Drupal\graphql\Plugin\GraphQL\Traits\CacheablePluginTrait;
8
use Drupal\graphql\Plugin\GraphQL\Traits\DescribablePluginTrait;
9
use Drupal\graphql\Plugin\TypePluginInterface;
10
use Drupal\graphql\Plugin\TypePluginManager;
11
use GraphQL\Type\Definition\InterfaceType;
12
use GraphQL\Type\Definition\ObjectType;
13
use GraphQL\Type\Definition\ResolveInfo;
14
15
abstract class TypePluginBase extends PluginBase implements TypePluginInterface {
16
  use CacheablePluginTrait;
17
  use DescribablePluginTrait;
18
19
  /**
20
   * {@inheritdoc}
21
   */
22
  public static function createInstance(SchemaBuilder $builder, TypePluginManager $manager, $definition, $id) {
23
    return new ObjectType([
24
      'name' => $definition['name'],
25
      'description' => $definition['description'],
26
      'fields' => function () use ($builder, $definition) {
27
        $fields = $builder->getFields($definition['name']);
28
29
        if (!empty($definition['interfaces'])) {
30
          $inherited = array_map(function ($name) use ($builder) {
31
            return $builder->getFields($name);
32
          }, $definition['interfaces']);
33
34
          $inherited = call_user_func_array('array_merge', $inherited);
35
          return array_merge($inherited, $fields);
36
        }
37
38
        return $fields;
39
      },
40
      'interfaces' => function () use ($builder, $definition) {
41
        return array_filter(array_map(function ($name) use ($builder) {
42
          return $builder->getType($name);
43
        }, $definition['interfaces']), function ($type) {
44
          return $type instanceof InterfaceType;
45
        });
46
      },
47
      'isTypeOf' => function ($object, $context, ResolveInfo $info) use ($manager, $id) {
48
        $instance = $manager->getInstance(['id' => $id]);
49
        return $instance->applies($object, $context, $info);
50
      }
51
    ]);
52
  }
53
54
  /**
55
   * {@inheritdoc}
56
   */
57 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...
58
    $definition = $this->getPluginDefinition();
59
60
    return [
61
      'name' => $definition['name'],
62
      'description' => $this->buildDescription($definition),
63
      'interfaces' => $this->buildInterfaces($definition),
64
    ];
65
  }
66
67
  /**
68
   * @param $definition
69
   *
70
   * @return array
71
   */
72
  protected function buildInterfaces($definition) {
73
    return array_unique($definition['interfaces']);
74
  }
75
76
  /**
77
   * Checks whether this type applies to a given object.
78
   *
79
   * @param mixed $object
80
   *   The object to check against.
81
   * @param mixed $context
82
   *   The execution context.
83
   * @param \GraphQL\Type\Definition\ResolveInfo $info
84
   *   The resolve info object.
85
   *
86
   * @return null|bool
87
   *   TRUE if this type applies to the given object or FALSE if it doesn't.
88
   */
89
  public function applies($object, $context, ResolveInfo $info) {
90
    return NULL;
91
  }
92
93
}
94