Completed
Pull Request — 8.x-3.x (#509)
by Sebastian
03:36 queued 01:04
created

EntityTranslations::resolveValues()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 5
nc 3
nop 3
dl 0
loc 8
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Entity;
4
5
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
6
use Drupal\Core\Entity\ContentEntityInterface;
7
use Drupal\Core\Entity\EntityRepositoryInterface;
8
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
9
use Drupal\Core\TypedData\TranslatableInterface;
10
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12
use Youshido\GraphQL\Execution\ResolveInfo;
13
14
/**
15
 * @GraphQLField(
16
 *   id = "entity_translations",
17
 *   name = "entityTranslations",
18
 *   secure = true,
19
 *   type = "[Entity]",
20
 *   parents = {"Entity"}
21
 * )
22
 */
23
class EntityTranslations extends FieldPluginBase implements ContainerFactoryPluginInterface {
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...
24
  use DependencySerializationTrait;
25
26
  /**
27
   * The entity repository service.
28
   *
29
   * @var \Drupal\Core\Entity\EntityRepositoryInterface
30
   */
31
  protected $entityRepository;
32
33
  /**
34
   * {@inheritdoc}
35
   */
36
  public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
37
    return new static(
38
      $configuration,
39
      $pluginId,
40
      $pluginDefinition,
41
      $container->get('entity.repository')
42
    );
43
  }
44
45
  /**
46
   * EntityTranslations constructor.
47
   *
48
   * @param array $configuration
49
   *   The plugin configuration array.
50
   * @param string $pluginId
51
   *   The plugin id.
52
   * @param mixed $pluginDefinition
53
   *   The plugin definition array.
54
   * @param \Drupal\Core\Entity\EntityRepositoryInterface $entityRepository
55
   *   The entity repository service.
56
   */
57
  public function __construct(
58
    array $configuration,
59
    $pluginId,
60
    $pluginDefinition,
61
    EntityRepositoryInterface $entityRepository
62
  ) {
63
    parent::__construct($configuration, $pluginId, $pluginDefinition);
64
    $this->entityRepository = $entityRepository;
65
  }
66
67
  /**
68
   * {@inheritdoc}
69
   */
70
  public function resolveValues($value, array $args, ResolveInfo $info) {
71
    if ($value instanceof ContentEntityInterface && $value instanceof TranslatableInterface && $value->isTranslatable()) {
0 ignored issues
show
Bug introduced by
The class Drupal\Core\Entity\ContentEntityInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
Bug introduced by
The class Drupal\Core\TypedData\TranslatableInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
72
      $languages = $value->getTranslationLanguages();
73
      foreach ($languages as $language) {
74
        yield $this->entityRepository->getTranslationFromContext($value, $language->getId());
75
      }
76
    }
77
  }
78
79
}
80