metadata = $metadata; $this->config = $config; $this->entityManager = $entityManager; $this->streamService = $streamService; $this->notificatorFactory = $notificatorFactory; $this->user = $user; } public function afterSave(Entity $entity, array $options): void { $entityType = $entity->getEntityType(); assert($entity instanceof CoreEntity); /** * No need to process assignment notifications for entity types that have Stream enabled. * Users are notified via Stream notifications. */ if ($this->checkHasStream($entityType) && !$entity->hasLinkMultipleField('assignedUsers')) { return; } $assignmentNotificationsEntityList = $this->config->get('assignmentNotificationsEntityList') ?? []; if (!in_array($entityType, $assignmentNotificationsEntityList)) { return; } $notificator = $this->getNotificator($entityType); if (!$notificator instanceof AssignmentNotificator) { // For backward compatiblity. $notificator->process($entity, $options); return; } $params = AssignmentNotificatorParams::create()->withRawOptions($options); $notificator->process($entity, $params); } public function beforeRemove(Entity $entity): void { $entityType = $entity->getEntityType(); if (!$this->checkHasStream($entityType)) { return; } $followersData = $this->streamService->getEntityFollowers($entity); $userIdList = $followersData['idList']; foreach ($userIdList as $userId) { if ($userId === $this->user->getId()) { continue; } $this->entityManager->createEntity(Notification::ENTITY_TYPE, [ 'userId' => $userId, 'type' => Notification::TYPE_ENTITY_REMOVED, 'data' => [ 'entityType' => $entity->getEntityType(), 'entityId' => $entity->getId(), 'entityName' => $entity->get('name'), 'userId' => $this->user->getId(), 'userName' => $this->user->get('name'), ], ]); } } public function afterRemove(Entity $entity): void { $query = $this->entityManager ->getQueryBuilder() ->delete() ->from(Notification::ENTITY_TYPE) ->where([ 'OR' => [ [ 'relatedId' => $entity->getId(), 'relatedType' => $entity->getEntityType(), ], [ 'relatedParentId' => $entity->getId(), 'relatedParentType' => $entity->getEntityType(), ], ], ]) ->build(); $this->entityManager->getQueryExecutor()->execute($query); } private function checkHasStream(string $entityType): bool { if (!array_key_exists($entityType, $this->hasStreamCache)) { $this->hasStreamCache[$entityType] = (bool) $this->metadata->get(['scopes', $entityType, 'stream']); } return $this->hasStreamCache[$entityType]; } /** * @return AssignmentNotificator */ private function getNotificator(string $entityType): object { if (empty($this->notifatorsHash[$entityType])) { $notificator = $this->notificatorFactory->create($entityType); $this->notifatorsHash[$entityType] = $notificator; } return $this->notifatorsHash[$entityType]; } }