RouteController::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql_twig\Controller;
4
5
use Drupal\Core\Controller\ControllerBase;
6
use Drupal\Core\Template\TwigEnvironment;
7
use Drupal\graphql\GraphQL\Execution\QueryProcessor;
8
use GraphQL\Server\OperationParams;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
11
/**
12
 * Controller for configuration-generated GraphQL-Twig routes.
13
 */
14
class RouteController extends ControllerBase {
15
16
  /**
17
   * @var \Drupal\graphql\GraphQL\Execution\QueryProcessor
18
   */
19
  protected $queryProcessor;
20
21
  /**
22
   * @var \Drupal\Core\Template\TwigEnvironment
23
   */
24
  protected $twig;
25
26
  /**
27
   * @inheritdoc
28
   */
29
  public static function create(ContainerInterface $container) {
30
    return new static(
31
      $container->get('graphql.query_processor'),
32
      $container->get('twig')
33
    );
34
  }
35
36
  /**
37
   * RouteController constructor.
38
   *
39
   * @param \Drupal\graphql\GraphQL\Execution\QueryProcessor $processor
40
   *   A GraphQL query processor.
41
   * @param \Drupal\Core\Template\TwigEnvironment $twig
42
   *   A Twig environment.
43
   */
44
  public function __construct(QueryProcessor $processor, TwigEnvironment $twig) {
45
    $this->queryProcessor = $processor;
46
    $this->twig = $twig;
47
  }
48
49
  /**
50
   * Generic page callback.
51
   *
52
   * Accepts a theme hook and an array of theme variables.
53
   *
54
   * @param $_graphql_theme_hook
55
   *   The theme hook.
56
   * @param $_graphql_arguments
57
   *   Query arguments
58
   *
59
   * @return array
60
   *   The render array build.
61
   */
62
  public function page($_graphql_theme_hook, $_graphql_arguments) {
63
    return [
64
      '#theme' => $_graphql_theme_hook,
65
      '#graphql_arguments' => $_graphql_arguments,
66
    ];
67
  }
68
69
  /**
70
   * Build a page title from a twig template and a GraphQL query.
71
   *
72
   * @param $_graphql_title
73
   * @param $_graphql_title_query
74
   * @param $_graphql_arguments
75
   *
76
   * @return \Drupal\Component\Render\MarkupInterface|\Drupal\Core\StringTranslation\TranslatableMarkup|string
77
   * @throws \Drupal\Component\Plugin\Exception\PluginException
78
   */
79
  public function title($_graphql_title, $_graphql_title_query, $_graphql_arguments) {
80
    if (!$_graphql_title) {
81
      return FALSE;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return FALSE; (false) is incompatible with the return type documented by Drupal\graphql_twig\Cont...\RouteController::title of type Drupal\Component\Render\...anslatableMarkup|string.

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...
82
    }
83
    if ($_graphql_title_query) {
84
      $result = $this->queryProcessor->processQuery('default:default',
85
        OperationParams::create([
86
          'query' => $_graphql_title_query,
87
          'variables' => $_graphql_arguments,
88
        ])
89
      );
90
91
      if ($result->errors) {
92
        $_graphql_title = reset($result->errors)->getMessage();
93
      }
94
      else {
95
        $_graphql_title = $this->twig->renderInline($_graphql_title, $result->data);
96
      }
97
    }
98
    else {
99
      $_graphql_title = $this->t($_graphql_title);
100
    }
101
    return $_graphql_title;
102
  }
103
104
}
105