Code Duplication    Length = 28-30 lines in 2 locations

modules/graphql_core/src/Plugin/GraphQL/Fields/Entity/EntityChanged.php 1 location

@@ 25-52 (lines=28) @@
22
 *   }
23
 * )
24
 */
25
class EntityChanged extends FieldPluginBase {
26
27
  /**
28
   * {@inheritdoc}
29
   */
30
  protected function buildArguments(PluggableSchemaBuilderInterface $schemaBuilder) {
31
    $arguments = parent::buildArguments($schemaBuilder);
32
33
    // Set the default value of the format argument programmatically.
34
    /** @var \Youshido\GraphQL\Field\AbstractInputField $format */
35
    $format = $arguments['format'];
36
    $format->getConfig()->set('defaultValue', DateTime::ISO8601);
37
38
    return $arguments;
39
  }
40
41
  /**
42
   * {@inheritdoc}
43
   */
44
  protected function resolveValues($value, array $args, ResolveInfo $info) {
45
    if ($value instanceof EntityChangedInterface) {
46
      $datetime = new DateTime();
47
      $datetime->setTimestamp($value->getChangedTime());
48
      yield $datetime->format($args['format']);
49
    }
50
  }
51
52
}
53

modules/graphql_core/src/Plugin/GraphQL/Fields/Entity/EntityCreated.php 1 location

@@ 21-50 (lines=30) @@
18
 *   parents = {"Entity"}
19
 * )
20
 */
21
class EntityCreated extends FieldPluginBase {
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