You are here

public function HeartbeatEventSubscriber::flag_entity_unflagged in Heartbeat 8

This method is called whenever the flag.entity_unflagged event is dispatched.

Parameters

GetResponseEvent $event:

File

src/EventSubscriber/HeartbeatEventSubscriber.php, line 181

Class

HeartbeatEventSubscriber
Class HeartbeatEventSubscriber.

Namespace

Drupal\heartbeat\EventSubscriber

Code

public function flag_entity_unflagged(Event $event) {
  $friendStatus = FRIEND;
  $flagging = array_values($event
    ->getFlaggings())[0];
  if ($flagging
    ->getFlagId() === 'friendship') {
    $entity = $this->flagService
      ->getFlagById($flagging
      ->getFlagId());
    $user = $flagging
      ->getOwner();
    if ($entity
      ->id() && $user
      ->isAuthenticated()) {
      $heartbeatTypeService = \Drupal::service('heartbeat.heartbeattype');
      $tokenService = \Drupal::service('token');
      foreach ($heartbeatTypeService
        ->getTypes() as $type) {
        $heartbeatTypeEntity = $heartbeatTypeService
          ->load($type);
        if ($heartbeatTypeEntity
          ->getMainEntity() === "flagging") {
          $arguments = json_decode($heartbeatTypeEntity
            ->getArguments());
          $user2 = User::load($flagging
            ->getFlaggableId());
          $targetUserFriendships = $this->flagService
            ->getFlagFlaggings($entity, $user2);
          foreach ($targetUserFriendships as $friendship) {
            if ($friendship
              ->getFlaggableId() === $user
              ->id()) {
              $friendStatus = NOT_FRIEND;
              break;
            }
          }
          $friendStatus = $friendStatus == NOT_FRIEND ? NOT_FRIEND : PENDING;
          foreach ($arguments as $key => $argument) {
            $variables[$key] = $argument;
          }
          Heartbeat::updateFriendship($user
            ->id(), $user2
            ->id(), time(), $friendStatus);
        }
      }
    }
  }
  $friendships = Database::getConnection()
    ->select("heartbeat_friendship", "hf")
    ->fields('hf', array(
    'status',
    'uid',
    'uid_target',
  ))
    ->execute();
  $friendData = $friendships
    ->fetchAll();
  $friendConfig = \Drupal::configFactory()
    ->getEditable('heartbeat_friendship.settings');
  $friendConfig
    ->set('data', json_encode($friendData))
    ->save();
}