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

InternalRequest::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 4
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Routing\InternalUrl;
4
5
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
6
use Drupal\Core\Url;
7
use Drupal\graphql\GraphQL\Buffers\SubRequestBuffer;
8
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
use Symfony\Component\HttpFoundation\RequestStack;
11
use Symfony\Component\HttpKernel\HttpKernelInterface;
12
use Youshido\GraphQL\Execution\ResolveInfo;
13
14
/**
15
 * Issue an internal request and retrieve the response object.
16
 *
17
 * @GraphQLField(
18
 *   id = "internal_url_request",
19
 *   secure = true,
20
 *   name = "request",
21
 *   type = "InternalResponse",
22
 *   parents = {"InternalUrl"}
23
 * )
24
 */
25
class InternalRequest 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...
26
27
  /**
28
   * The http kernel service.
29
   *
30
   * @var \Symfony\Component\HttpKernel\HttpKernelInterface
31
   */
32
  protected $httpKernel;
33
34
  /**
35
   * The request stack.
36
   *
37
   * @var \Symfony\Component\HttpFoundation\RequestStack
38
   */
39
  protected $requestStack;
40
41
  /**
42
   * The subrequest buffer service.
43
   *
44
   * @var \Drupal\graphql\GraphQL\Buffers\SubRequestBuffer
45
   */
46
  protected $subRequestBuffer;
47
48
  /**
49
   * {@inheritdoc}
50
   */
51
  public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
52
    return new static(
53
      $configuration,
54
      $pluginId,
55
      $pluginDefinition,
56
      $container->get('graphql.buffer.subrequest'),
57
      $container->get('http_kernel'),
58
      $container->get('request_stack')
59
    );
60
  }
61
62
  /**
63
   * InternalRequest constructor.
64
   *
65
   * @param array $configuration
66
   *   The plugin configuration array.
67
   * @param string $pluginId
68
   *   The plugin id.
69
   * @param mixed $pluginDefinition
70
   *   The plugin definition array.
71
   * @param \Drupal\graphql\GraphQL\Buffers\SubRequestBuffer $subRequestBuffer
72
   *   The sub-request buffer service.
73
   * @param \Symfony\Component\HttpKernel\HttpKernelInterface $httpKernel
74
   *   The http kernel.
75
   * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
76
   *   The request stack.
77
   */
78
  public function __construct(
79
    array $configuration,
80
    $pluginId,
81
    $pluginDefinition,
82
    SubRequestBuffer $subRequestBuffer,
83
    HttpKernelInterface $httpKernel,
84
    RequestStack $requestStack
85
  ) {
86
    parent::__construct($configuration, $pluginId, $pluginDefinition);
87
    $this->subRequestBuffer = $subRequestBuffer;
88
    $this->httpKernel = $httpKernel;
89
    $this->requestStack = $requestStack;
90
  }
91
92
  /**
93
   * {@inheritdoc}
94
   */
95
  protected function resolveValues($value, array $args, ResolveInfo $info) {
96
    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...
97
      $resolve = $this->subRequestBuffer->add($value, function () {
98
        $request = $this->requestStack->getCurrentRequest()->duplicate();
99
        $request->attributes->set('_controller', $request->get('_graphql_controller'));
100
        $request->attributes->remove('_graphql_subrequest');
101
        $request->attributes->remove('_graphql_controller');
102
103
        $response = $this->httpKernel->handle($request, HttpKernelInterface::SUB_REQUEST);
104
105
        // TODO:
106
        // Remove the request stack manipulation once the core issue described at
107
        // https://www.drupal.org/node/2613044 is resolved.
108
        while ($this->requestStack->getCurrentRequest() === $request) {
109
          $this->requestStack->pop();
110
        }
111
112
        return $response;
113
      });
114
115
      return function ($value, array $args, ResolveInfo $info) use ($resolve) {
0 ignored issues
show
Bug Best Practice introduced by
The return type of return function ($value,... (yield $resolve()); }; (Closure) is incompatible with the return type of the parent method Drupal\graphql\Plugin\Gr...uginBase::resolveValues of type Generator.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $info is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
116
        yield $resolve();
117
      };
118
    }
119
  }
120
121
}