Completed
Pull Request — 8.x-3.x (#484)
by Sebastian
03:39
created

EntityCreated::buildArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Entity;
4
5
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
6
use Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface;
7
use Youshido\GraphQL\Execution\ResolveInfo;
8
use DateTime;
9
10
/**
11
 * Get the entities created date if available.
12
 *
13
 * @GraphQLField(
14
 *   id = "entity_created",
15
 *   secure = true,
16
 *   name = "entityCreated",
17
 *   type = "String",
18
 *   parents = {"Entity"}
19
 * )
20
 */
21 View Code Duplication
class EntityCreated extends FieldPluginBase {
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...
Duplication introduced by
This class 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...
22
23
  /**
24
   * {@inheritdoc}
25
   */
26
  protected function buildArguments(PluggableSchemaBuilderInterface $schemaBuilder) {
27
    $arguments = parent::buildArguments($schemaBuilder);
28
29
    // Set the default value of the format argument programmatically.
30
    /** @var \Youshido\GraphQL\Field\AbstractInputField $format */
31
    $format = $arguments['format'];
32
    $format->getConfig()->set('defaultValue', DateTime::ISO8601);
33
34
    return $arguments;
35
  }
36
37
  /**
38
   * {@inheritdoc}
39
   */
40
  protected function resolveValues($value, array $args, ResolveInfo $info) {
41
    // `getCreatedTime` is on NodeInterface which feels weird, since there
42
    // is a generic `EntityInterface`. Checking for method existence for now.
43
    if (method_exists($value, 'getCreatedTime')) {
44
      $datetime = new DateTime();
45
      $datetime->setTimestamp($value->getCreatedTime());
46
      yield $datetime->format($args['format']);
47
    }
48
  }
49
50
}
51