You are here

private function StatusTwitter::processTerms in Heartbeat 8

1 call to StatusTwitter::processTerms()
StatusTwitter::setNodeData in modules/statusmessage/src/StatusTwitter.php

File

modules/statusmessage/src/StatusTwitter.php, line 262

Class

StatusTwitter

Namespace

Drupal\statusmessage

Code

private function processTerms($data) {
  $terms = new \stdClass();
  $terms->tags = [];
  $terms->users = [];
  $terms->username = -1;
  if ($data->user->screen_name) {
    $term = \Drupal::entityQuery('taxonomy_term')
      ->condition('name', $data->user->screen_name)
      ->condition('vid', 'twitter_user')
      ->execute();
    if (count($term) < 1) {
      $term = Term::create([
        'name' => $data->user->screen_name,
        'vid' => 'twitter_user',
        'field_count' => 1,
      ]);
      if ($term
        ->save()) {
        $terms->username = $term
          ->id();
        if (\Drupal::moduleHandler()
          ->moduleExists('heartbeat')) {
          \Drupal\heartbeat\Entity\Heartbeat::newTermUsage($term
            ->id());
        }
      }
      else {
        \Drupal::logger('StatusTwitter')
          ->warning('Could not save term with name %name', array(
          '%name' => $data->user->screen_name,
        ));
      }
    }
    else {
      $terms->username = array_values($term)[0];
      if (\Drupal::moduleHandler()
        ->moduleExists('heartbeat')) {
        \Drupal\heartbeat\Entity\Heartbeat::updateTermUsage(array_values($term)[0], 'twitter_user');
      }
    }
    $term = NULL;
    foreach ($data->entities->hashtags as $key => $h) {
      $term = \Drupal::entityQuery('taxonomy_term')
        ->condition('name', $h->text)
        ->condition('vid', 'twitter')
        ->execute();
      if (count($term) < 1) {
        $term = Term::create([
          'name' => $h->text,
          'vid' => 'twitter',
          'field_count' => 1,
        ]);
        if ($term
          ->save()) {
          $terms->tags[] = $term
            ->id();
          if (\Drupal::moduleHandler()
            ->moduleExists('heartbeat')) {
            \Drupal\heartbeat\Entity\Heartbeat::newTermUsage($term
              ->id());
          }
        }
        else {
          \Drupal::logger('StatusTwitter')
            ->warning('Could not save term with name %name', array(
            '%name' => $h->text,
          ));
        }
      }
      else {
        $terms->tags[] = array_values($term)[0];
        if (\Drupal::moduleHandler()
          ->moduleExists('heartbeat')) {
          \Drupal\heartbeat\Entity\Heartbeat::updateTermUsage(array_values($term)[0], 'twitter');
        }
      }
    }
    $term = NULL;
    foreach ($data->entities->user_mentions as $u) {
      $term = \Drupal::entityQuery('taxonomy_term')
        ->condition('name', $u->screen_name)
        ->condition('vid', 'twitter_user')
        ->execute();
      if (count($term) < 1) {
        $term = Term::create([
          'name' => $u->screen_name,
          'vid' => 'twitter_user',
        ]);
        if ($term
          ->save()) {
          $terms->users[] = $term
            ->id();
        }
        else {
          \Drupal::logger('StatusTwitter')
            ->warning('Could not save term with name %name', array(
            '%name' => $u->screen_name,
          ));
        }
      }
      else {
        $terms->users[] = array_values($term)[0];
      }
    }
  }
  return $terms;
}