GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ArticleNodeEndpointBlock   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 66
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A create() 0 6 1
A build() 0 25 1
1
<?php
2
3
namespace Drupal\df_tools_article\Plugin\Block;
4
5
use Drupal\Core\Block\BlockBase;
6
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
7
use Symfony\Component\DependencyInjection\ContainerInterface;
8
use GuzzleHttp\Client;
9
10
/**
11
 * Provides a 'ArticleNodeEndpointBlock' block.
12
 *
13
 * @Block(
14
 *  id = "article_node_endpoint_block",
15
 *  admin_label = @Translation("Article node endpoint block"),
16
 *  category = "Lists",
17
 * )
18
 */
19
class ArticleNodeEndpointBlock extends BlockBase implements ContainerFactoryPluginInterface {
20
21
  /**
22
   * GuzzleHttp\Client definition.
23
   *
24
   * @var \GuzzleHttp\Client
25
   */
26
  protected $httpClient;
27
28
  /**
29
   * Constructs a new ArticleNodeEndpointBlock instance.
30
   *
31
   * @param array $configuration
32
   *   A configuration array containing information about the plugin instance.
33
   * @param string $plugin_id
34
   *   The plugin ID for the plugin instance.
35
   * @param mixed $plugin_definition
36
   *   The plugin implementation definition.
37
   * @param \GuzzleHttp\Client $http_client
38
   *   The HTTP client service.
39
   */
40
  public function __construct(array $configuration, $plugin_id, $plugin_definition, Client $http_client) {
41
    parent::__construct($configuration, $plugin_id, $plugin_definition);
42
    $this->httpClient = $http_client;
43
  }
44
45
  /**
46
   * {@inheritdoc}
47
   */
48
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
49
    return new static(
50
      $configuration,
51
      $plugin_id,
52
      $plugin_definition,
53
      $container->get('http_client')
0 ignored issues
show
Bug introduced by
It seems like $container->get('http_client') can also be of type null; however, parameter $http_client of Drupal\df_tools_article\...intBlock::__construct() does only seem to accept GuzzleHttp\Client, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
      /** @scrutinizer ignore-type */ $container->get('http_client')
Loading history...
54
    );
55
  }
56
57
  /**
58
   * {@inheritdoc}
59
   */
60
  public function build() {
61
    $build = [];
62
    global $base_url;
63
64
    header('Content-Type: application/json');
65
    $json_output = (string) $this->httpClient->get($base_url . '/api/node/article')->getBody();
66
    $json_pretty = json_encode(json_decode($json_output), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
67
    $json_indented_by_2 = preg_replace('/^(  +?)\\1(?=[^ ])/m', '$1', $json_pretty);
68
69
    $build['article_node_endpoint_block'] = [
70
      '#markup' => '<div id="api-demo">
71
        <button class="btn btn-primary button button--primary coh-style-link-button 
72
        coh-style-link-button-color open-apiModal">Expand API Response</button>
73
        <div class="apiResponse">
74
          <pre><code class="language-json">' . $json_indented_by_2 . '</code></pre>
75
        </div>
76
        <div class="apiResponseModal">
77
          <pre><code class="language-json">' . $json_indented_by_2 . '</code></pre>
78
        </div>
79
      </div>',
80
      '#attached' => ['library' => ['df_tools_article/main']],
81
      '#allowed_tags' => ['button', 'code', 'div', 'pre'],
82
    ];
83
84
    return $build;
85
  }
86
87
}
88