Completed
Pull Request — 8.x-3.x (#509)
by Sebastian
06:18
created

ExternalRequest::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 4
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Routing\ExternalUrl;
4
5
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
6
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
7
use Drupal\Core\Url;
8
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
9
use GuzzleHttp\ClientInterface;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
use Youshido\GraphQL\Execution\ResolveInfo;
12
13
/**
14
 * Issue an external request and retrieve the response object.
15
 *
16
 * @GraphQLField(
17
 *   id = "external_url_request",
18
 *   name = "request",
19
 *   type = "ExternalResponse",
20
 *   parents = {"ExternalUrl"}
21
 * )
22
 */
23 View Code Duplication
class ExternalRequest 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...
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...
24
  use DependencySerializationTrait;
25
26
  /**
27
   * @var \GuzzleHttp\ClientInterface
28
   */
29
  protected $httpClient;
30
31
  /**
32
   * {@inheritdoc}
33
   */
34
  public static function create(
35
    ContainerInterface $container,
36
    array $configuration,
37
    $plugin_id,
38
    $plugin_definition
39
  ) {
40
    return new static(
41
      $configuration,
42
      $plugin_id,
43
      $plugin_definition,
44
      $container->get('http_client')
45
    );
46
  }
47
48
  /**
49
   * ExternalRequest constructor.
50
   *
51
   * @param array $configuration
52
   *   The plugin configuration array.
53
   * @param string $pluginId
54
   *   The plugin id.
55
   * @param mixed $pluginDefinition
56
   *   The plugin definition array.
57
   * @param \GuzzleHttp\ClientInterface $httpClient
58
   *   The http client service.
59
   */
60
  public function __construct(
61
    array $configuration,
62
    $pluginId,
63
    $pluginDefinition,
64
    ClientInterface $httpClient
65
  ) {
66
    parent::__construct($configuration, $pluginId, $pluginDefinition);
67
    $this->httpClient = $httpClient;
68
  }
69
70
71
  /**
72
   * {@inheritdoc}
73
   */
74
  protected function resolveValues($value, array $args, ResolveInfo $info) {
75
    if ($value instanceof Url) {
0 ignored issues
show
Bug introduced by
The class Drupal\Core\Url 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...
76
      yield $this->httpClient->request('GET', $value->toString());
77
    }
78
  }
79
80
}
81