addDependency('serviceFactory'); } public function afterSave(Entity $entity, array $options = []) { parent::afterSave($entity, $options); $this->handleAfterSaveContacts($entity, $options); } protected function handleAfterSaveContacts(Entity $entity, array $options = []) { if ($entity->isAttributeChanged('contactId')) { $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->getInjection('serviceFactory')->create('Stream')->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->getInjection('serviceFactory')->create('Stream')->followEntity($entity, $portalUser->id, true); } if (!in_array($contactId, $contactIdList) && !$this->isRelated($entity, 'contacts', $contactId)) { $this->relate($entity, 'contacts', $contactId); } } } }