handleAfterSaveContacts($entity, $options); } protected function getStreamService() { $this->streamService = $this->streamService ?? $this->serviceFactory->create('Stream'); return $this->streamService; } protected function handleAfterSaveContacts(Entity $entity, array $options = []) { if (!$entity->isAttributeChanged('contactId')) return; $contactId = $entity->get('contactId'); $contactIdList = $entity->get('contactsIds') ?? []; $fetchedContactId = $entity->getFetched('contactId'); if ($fetchedContactId) { $previousPortalUser = $this->getEntityManager()->getRepository('User')->select(['id'])->where([ 'contactId' => $fetchedContactId, 'type' => 'portal', ])->findOne(); if ($previousPortalUser) { $this->getStreamService()->unfollowEntity($entity, $previousPortalUser->id); } } if (!$contactId) { if ($fetchedContactId) { $this->unrelate($entity, 'contacts', $fetchedContactId); } return; } $portalUser = $this->getEntityManager()->getRepository('User')->select(['id'])->where([ 'contactId' => $contactId, 'type' => 'portal', 'isActive' => true, ])->findOne(); if ($portalUser) { $this->getStreamService()->followEntity($entity, $portalUser->id, true); } if (!in_array($contactId, $contactIdList) && !$this->isRelated($entity, 'contacts', $contactId)) { $this->relate($entity, 'contacts', $contactId); } } }