metadata = $metadata; $this->config = $config; $this->entityManager = $entityManager; $this->serviceFactory = $serviceFactory; $this->notificatorFactory = $notificatorFactory; $this->user = $user; } protected function checkHasStream($entityType) { if (!array_key_exists($entityType, $this->hasStreamCache)) { $this->hasStreamCache[$entityType] = $this->metadata->get("scopes.{$entityType}.stream"); } return $this->hasStreamCache[$entityType]; } protected function getNotificator($entityType) { if (empty($this->notifatorsHash[$entityType])) { $notificator = $this->notificatorFactory->create($entityType); $this->notifatorsHash[$entityType] = $notificator; } return $this->notifatorsHash[$entityType]; } public function afterSave(Entity $entity, array $options = []) { if (!empty($options['silent']) || !empty($options['noNotifications'])) { return; } $entityType = $entity->getEntityType(); if (!$this->checkHasStream($entityType) || $entity->hasLinkMultipleField('assignedUsers')) { if (in_array($entityType, $this->config->get('assignmentNotificationsEntityList', []))) { $notificator = $this->getNotificator($entityType); $notificator->process($entity, $options); } } } public function beforeRemove(Entity $entity, array $options = []) { if (!empty($options['silent']) || !empty($options['noNotifications'])) { return; } $entityType = $entity->getEntityType(); if ($this->checkHasStream($entityType)) { $followersData = $this->getStreamService()->getEntityFollowers($entity); foreach ($followersData['idList'] as $userId) { if ($userId === $this->user->id) { continue; } $notification = $this->entityManager->getEntity('Notification'); $notification->set(array( 'userId' => $userId, 'type' => 'EntityRemoved', 'data' => array( 'entityType' => $entity->getEntityType(), 'entityId' => $entity->id, 'entityName' => $entity->get('name'), 'userId' => $this->user->id, 'userName' => $this->user->get('name'), ) )); $this->entityManager->saveEntity($notification); } } } public function afterRemove(Entity $entity) { $query = $this->entityManager->getQueryBuilder() ->delete() ->from('Notification') ->where([ 'OR' => [ [ 'relatedId' => $entity->id, 'relatedType' => $entity->getEntityType(), ], [ 'relatedParentId' => $entity->id, 'relatedParentType' => $entity->getEntityType(), ], ], ]) ->build(); $this->entityManager->getQueryExecutor()->execute($query); } protected function getStreamService() { if (empty($this->streamService)) { $this->streamService = $this->serviceFactory->create('Stream'); } return $this->streamService; } }