* @package oat\tao\model\search\tasks */ class UpdateResourceInIndex implements Action, ServiceLocatorAwareInterface, TaskAwareInterface { use ServiceLocatorAwareTrait; use OntologyAwareTrait; use TaskAwareTrait; public function __invoke($params): Report { if (empty($params) || empty($params[0])) { throw new \common_exception_MissingParameter(); } $createdResource = $this->getResource($params[0]); /** @var IndexService $indexService */ $indexService = $this->getServiceLocator()->get(IndexService::SERVICE_ID); /** @var IndexDocumentBuilder $documentBuilder */ $documentBuilder = $indexService->getDocumentBuilder(); $documentBuilder->setServiceLocator($this->getServiceLocator()); $indexDocument = $documentBuilder->createDocumentFromResource($createdResource); /** @var Search $searchService */ $searchService = $this->getServiceLocator()->get(Search::SERVICE_ID); $numberOfIndexed = $searchService->index([$indexDocument]); if ($numberOfIndexed === 0) { $type = Report::TYPE_ERROR; $message = "Zero documents were added/updated in index."; } elseif ($numberOfIndexed === 1) { $type = Report::TYPE_SUCCESS; $message = "Document in index was successfully updated."; } else { $type = Report::TYPE_WARNING; $message = "The number or indexed documents is different than the expected total"; } return new Report($type, $message); } }