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;
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()) {
0 ignored issues
show
Bug introduced by
The method hasField() does not exist on Drupal\Core\Entity\EntityInterface. It seems like you code against a sub-type of Drupal\Core\Entity\EntityInterface such as Drupal\Core\Entity\FieldableEntityInterface or Drupal\access_unpublished\AccessTokenInterface or Drupal\feeds\FeedInterface or Drupal\Core\Entity\EditorialContentEntityBase or Drupal\media\MediaInterface or Drupal\simple_oauth\Entity\Oauth2TokenInterface or Drupal\block_content\BlockContentInterface or Drupal\user\UserInterface or Drupal\cohesion_elements\ComponentContentInterface or Drupal\menu_link_content\MenuLinkContentInterface or Drupal\taxonomy\TermInterface or Drupal\entity_test\Entity\EntityTestConstraints or Drupal\tmgmt_local\LocalTaskInterface or Drupal\file\FileInterface or Drupal\moderation_note\ModerationNoteInterface or Drupal\storage\Entity\StorageInterface or Drupal\content_kanban\Entity\KanbanLogInterface or Drupal\workspaces\WorkspaceInterface or Drupal\entity_test\Entity\EntityTestMulChanged or Drupal\node\NodeInterface or Drupal\tmgmt_local\LocalTaskItemInterface or Drupal\comment\CommentInterface or Drupal\entity_host_relat...ityTestHostRelationship or Drupal\Core\Entity\EditorialContentEntityBase or Drupal\media\MediaInterface or Drupal\block_content\BlockContentInterface or Drupal\cohesion_elements\ComponentContentInterface or Drupal\menu_link_content\MenuLinkContentInterface or Drupal\Core\Entity\RevisionableContentEntityBase or Drupal\taxonomy\TermInterface or Drupal\storage\Entity\StorageInterface or Drupal\entity_test\Entit...vChangedWithRevisionLog or Drupal\node\NodeInterface or Drupal\Core\Entity\ContentEntityInterface or Drupal\Core\Entity\ContentEntityBase or Drupal\Core\Entity\EditorialContentEntityBase or Drupal\path_alias\PathAliasInterface or Drupal\entity_test\Entity\EntityTestMulRevPub or Drupal\entity_test\Entity\EntityTestRevPub or Drupal\media\MediaInterface or Drupal\block_content\BlockContentInterface or Drupal\cohesion_elements\ComponentContentInterface or Drupal\menu_link_content\MenuLinkContentInterface or Drupal\taxonomy\TermInterface or Drupal\moderation_note\ModerationNoteInterface or Drupal\storage\Entity\StorageInterface or Drupal\entity_test_revlo...stMulWithRevisionLogPub or Drupal\node\NodeInterface or Drupal\comment\CommentInterface or Drupal\Core\Entity\ContentEntityInterface or Drupal\Core\Entity\ContentEntityInterface or Drupal\views_ui\ViewUI or Drupal\views_ui\ViewUI. ( Ignorable by Annotation )

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

12
    if ($entity->/** @scrutinizer ignore-call */ hasField('field_send_to_slack') && $entity->isPublished()) {
Loading history...
Bug introduced by
The method isPublished() does not exist on Drupal\Core\Entity\EntityInterface. It seems like you code against a sub-type of Drupal\Core\Entity\EntityInterface such as Drupal\Core\Entity\EntityPublishedInterface or Drupal\path_alias\PathAliasInterface or Drupal\media\MediaInterface or Drupal\block_content\BlockContentInterface or Drupal\cohesion_elements\ComponentContentInterface or Drupal\menu_link_content\MenuLinkContentInterface or Drupal\taxonomy\TermInterface or Drupal\moderation_note\ModerationNoteInterface or Drupal\storage\Entity\StorageInterface or Drupal\content_kanban\Entity\KanbanLogInterface or Drupal\node\NodeInterface or Drupal\comment\CommentInterface or Drupal\Core\Entity\EditorialContentEntityBase or Drupal\moderation_note\Entity\ModerationNote or Drupal\path_alias\Entity\PathAlias or Drupal\content_kanban\Entity\KanbanLog or Drupal\comment\Entity\Comment or Drupal\entity_test\Entity\EntityTestRevPub or Drupal\entity_test\Entity\EntityTestMulRevPub or Drupal\entity_test_revlo...stMulWithRevisionLogPub or Drupal\Core\Entity\EditorialContentEntityBase or Drupal\media\MediaInterface or Drupal\block_content\BlockContentInterface or Drupal\cohesion_elements\ComponentContentInterface or Drupal\menu_link_content\MenuLinkContentInterface or Drupal\taxonomy\TermInterface or Drupal\moderation_note\ModerationNoteInterface or Drupal\storage\Entity\StorageInterface or Drupal\content_kanban\Entity\KanbanLogInterface or Drupal\node\NodeInterface or Drupal\comment\CommentInterface or Drupal\Core\Entity\EditorialContentEntityBase or Drupal\media\MediaInterface or Drupal\block_content\BlockContentInterface or Drupal\cohesion_elements\ComponentContentInterface or Drupal\menu_link_content\MenuLinkContentInterface or Drupal\taxonomy\TermInterface or Drupal\storage\Entity\StorageInterface or Drupal\node\NodeInterface or Drupal\entity_test_revlo...stMulWithRevisionLogPub or Drupal\path_alias\PathAliasInterface or Drupal\media\MediaInterface or Drupal\block_content\BlockContentInterface or Drupal\cohesion_elements\ComponentContentInterface or Drupal\menu_link_content\MenuLinkContentInterface or Drupal\taxonomy\TermInterface or Drupal\moderation_note\ModerationNoteInterface or Drupal\storage\Entity\StorageInterface or Drupal\content_kanban\Entity\KanbanLogInterface or Drupal\node\NodeInterface or Drupal\comment\CommentInterface or Drupal\Core\Entity\EditorialContentEntityBase or Drupal\moderation_note\Entity\ModerationNote or Drupal\path_alias\Entity\PathAlias or Drupal\content_kanban\Entity\KanbanLog or Drupal\comment\Entity\Comment or Drupal\entity_test\Entity\EntityTestRevPub or Drupal\entity_test\Entity\EntityTestMulRevPub or Drupal\entity_test_revlo...stMulWithRevisionLogPub or Drupal\Core\Entity\EditorialContentEntityBase or Drupal\moderation_note\Entity\ModerationNote or Drupal\path_alias\Entity\PathAlias or Drupal\content_kanban\Entity\KanbanLog or Drupal\comment\Entity\Comment or Drupal\entity_test\Entity\EntityTestRevPub or Drupal\entity_test\Entity\EntityTestMulRevPub or Drupal\entity_test_revlo...stMulWithRevisionLogPub or Drupal\path_alias\PathAliasInterface or Drupal\media\MediaInterface or Drupal\block_content\BlockContentInterface or Drupal\cohesion_elements\ComponentContentInterface or Drupal\menu_link_content\MenuLinkContentInterface or Drupal\taxonomy\TermInterface or Drupal\moderation_note\ModerationNoteInterface or Drupal\storage\Entity\StorageInterface or Drupal\content_kanban\Entity\KanbanLogInterface or Drupal\node\NodeInterface or Drupal\comment\CommentInterface or Drupal\Core\Entity\EditorialContentEntityBase or Drupal\moderation_note\Entity\ModerationNote or Drupal\path_alias\Entity\PathAlias or Drupal\content_kanban\Entity\KanbanLog or Drupal\comment\Entity\Comment or Drupal\entity_test\Entity\EntityTestRevPub or Drupal\entity_test\Entity\EntityTestMulRevPub or Drupal\entity_test_revlo...stMulWithRevisionLogPub or Drupal\path_alias\PathAliasInterface or Drupal\media\MediaInterface or Drupal\block_content\BlockContentInterface or Drupal\cohesion_elements\ComponentContentInterface or Drupal\menu_link_content\MenuLinkContentInterface or Drupal\taxonomy\TermInterface or Drupal\moderation_note\ModerationNoteInterface or Drupal\storage\Entity\StorageInterface or Drupal\content_kanban\Entity\KanbanLogInterface or Drupal\node\NodeInterface or Drupal\comment\CommentInterface or Drupal\Core\Entity\EditorialContentEntityBase or Drupal\moderation_note\Entity\ModerationNote or Drupal\path_alias\Entity\PathAlias or Drupal\content_kanban\Entity\KanbanLog or Drupal\comment\Entity\Comment or Drupal\entity_test\Entity\EntityTestRevPub or Drupal\entity_test\Entity\EntityTestMulRevPub or Drupal\entity_test_revlo...stMulWithRevisionLogPub or Drupal\views_ui\ViewUI or Drupal\views_ui\ViewUI. ( Ignorable by Annotation )

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

12
    if ($entity->hasField('field_send_to_slack') && $entity->/** @scrutinizer ignore-call */ isPublished()) {
Loading history...
13
      // node info
14
      $entity_id = $entity->id();
15
      $entity_title = $entity->getTitle();
0 ignored issues
show
Bug introduced by
The method getTitle() does not exist on Drupal\Core\Entity\EntityInterface. It seems like you code against a sub-type of Drupal\Core\Entity\EntityInterface such as Drupal\cohesion_elements\ComponentContentInterface or Drupal\menu_link_content\MenuLinkContentInterface or Drupal\node\NodeInterface or Drupal\shortcut\ShortcutInterface or Drupal\shortcut\Entity\Shortcut or Drupal\node\Entity\Node or Drupal\cohesion_elements\Entity\ComponentContent or Drupal\menu_link_content\Entity\MenuLinkContent or Drupal\cohesion_elements\ComponentContentInterface or Drupal\menu_link_content\MenuLinkContentInterface or Drupal\node\NodeInterface or Drupal\node\Entity\Node or Drupal\cohesion_elements\Entity\ComponentContent or Drupal\menu_link_content\Entity\MenuLinkContent or Drupal\cohesion_elements\ComponentContentInterface or Drupal\menu_link_content\MenuLinkContentInterface or Drupal\node\NodeInterface or Drupal\node\Entity\Node or Drupal\cohesion_elements\Entity\ComponentContent or Drupal\menu_link_content\Entity\MenuLinkContent or Drupal\cohesion_elements\ComponentContentInterface or Drupal\menu_link_content\MenuLinkContentInterface or Drupal\node\NodeInterface or Drupal\shortcut\ShortcutInterface or Drupal\shortcut\Entity\Shortcut or Drupal\node\Entity\Node or Drupal\cohesion_elements\Entity\ComponentContent or Drupal\menu_link_content\Entity\MenuLinkContent or Drupal\shortcut\Entity\Shortcut or Drupal\node\Entity\Node or Drupal\cohesion_elements\Entity\ComponentContent or Drupal\menu_link_content\Entity\MenuLinkContent or Drupal\cohesion_elements\ComponentContentInterface or Drupal\menu_link_content\MenuLinkContentInterface or Drupal\node\NodeInterface or Drupal\node\Entity\Node or Drupal\cohesion_elements\Entity\ComponentContent or Drupal\menu_link_content\Entity\MenuLinkContent or Drupal\cohesion_elements\ComponentContentInterface or Drupal\menu_link_content\MenuLinkContentInterface or Drupal\node\NodeInterface or Drupal\shortcut\ShortcutInterface or Drupal\shortcut\Entity\Shortcut or Drupal\node\Entity\Node or Drupal\cohesion_elements\Entity\ComponentContent or Drupal\menu_link_content\Entity\MenuLinkContent or Drupal\cohesion_elements\ComponentContentInterface or Drupal\menu_link_content\MenuLinkContentInterface or Drupal\node\NodeInterface or Drupal\shortcut\ShortcutInterface or Drupal\shortcut\Entity\Shortcut or Drupal\node\Entity\Node or Drupal\cohesion_elements\Entity\ComponentContent or Drupal\menu_link_content\Entity\MenuLinkContent or Drupal\views_ui\ViewUI or Drupal\views_ui\ViewUI. ( Ignorable by Annotation )

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

15
      /** @scrutinizer ignore-call */ 
16
      $entity_title = $entity->getTitle();
Loading history...
16
      $entity_url = Url::fromRoute('entity.node.canonical', ['node' => $entity_id], ['absolute' => TRUE])->toString();
17
      $summary = $entity->get('body')->summary;
0 ignored issues
show
Bug introduced by
The method get() does not exist on Drupal\Core\Entity\EntityInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Drupal\Core\Entity\EntityChangedInterface or Drupal\Core\Entity\RevisionableInterface or Drupal\Core\Entity\EntityBase or Drupal\Core\Entity\EntityPublishedInterface or Drupal\Core\Entity\TranslatableInterface or Drupal\Core\Entity\SynchronizableInterface or Drupal\Tests\Core\Entity...rageTestEntityInterface or Drupal\Core\Entity\EntityDescriptionInterface or Drupal\Core\Entity\Entit...uginCollectionInterface or Drupal\Core\Entity\RevisionLogInterface or Drupal\Core\Entity\Trans...leRevisionableInterface or Drupal\Tests\Core\Entity\RevisionableEntity or Drupal\Tests\Core\Entity...lueStore\EntityBaseTest or Drupal\Tests\content_moderation\Unit\TestEntity or Drupal\Tests\Core\Entity\UrlTestEntity or Drupal\Tests\Core\Entity\TestEntity or Drupal\Tests\Core\Entity\Fruit or Drupal\Tests\Core\Entity\EntityBaseTest or Drupal\Tests\Core\Entity\RevisionableEntity or Drupal\Tests\Core\Entity\Blackcurrant or Drupal\Tests\Core\Entity\Apple or Drupal\Tests\Core\Entity\RoyalGala or Drupal\Core\Entity\Trans...leRevisionableInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

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

17
      $summary = $entity->/** @scrutinizer ignore-call */ get('body')->summary;
Loading history...
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