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.

df_tools_slack_entity_insert()   B
last analyzed

Complexity

Conditions 9
Paths 14

Size

Total Lines 44
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 26
nc 14
nop 1
dl 0
loc 44
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
use Drupal\Core\Url;
4
use Drupal\Core\Entity\EntityInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Entity\EntityInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
5
use Drupal\taxonomy\Entity\Term;
6
7
/**
8
 * Implements hook_entity_insert().
9
 */
10
function df_tools_slack_entity_insert(EntityInterface $entity) {
11
  if ($entity->getEntityTypeId() == 'node') {
12
    if ($entity->hasField('field_send_to_slack') && $entity->isPublished()) {
13
      // node info
14
      $entity_id = $entity->id();
15
      $entity_title = $entity->getTitle();
16
      $entity_url = Url::fromRoute('entity.node.canonical', ['node' => $entity_id], ['absolute' => TRUE])->toString();
17
      $summary = $entity->get('body')->summary;
18
      // $author = strip_tags($entity->getAuthorName());
19
20
      if ($entity->hasField('field_tags')) {
21
        $target_id = $entity->get('field_tags')->target_id;
22
        if ($target_id !== null) {
23
          $term = Term::load($target_id);
24
          $term_name = $term->getName();
25
          // todo: select slack channel by taxonomy term, e.g. office location
26
        }
27
      }
28
29
      // site name
30
      $config = \Drupal::config('system.site');
31
      $sitename = $config->get('name');
32
33
      // slack info
34
      $config = \Drupal::config('slack.settings');
35
      $channel = $config->get('slack_channel');
36
      $username = $config->get('slack_username');
37
38
      // build message
39
      $output = [];
40
      $output[] = "A new content has been published on $sitename: ";
41
      $output[] = "<$entity_url | $entity_title>";
42
      if (isset($term) && !empty($term)) {
43
        $output[] = " tag: _$term_name _";
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $term_name does not seem to be defined for all execution paths leading up to this point.
Loading history...
44
      }
45
      $output[] = "$summary";
46
      // $output[] = "_Comment author_: $author";
47
48
      // send slack message
49
      if ($entity->get('field_send_to_slack')->value == 1) {
50
        \Drupal::service('slack.slack_service')
51
          ->sendMessage(implode("\n", $output), $channel, $username);
52
53
        \Drupal::logger('publish_to_slack')->info('Message sent.');
54
      }
55
56
    }
57
  }
58
59
}
60