Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
81.08% |
30 / 37 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
| UpdateResourceInIndex | |
81.08% |
30 / 37 |
|
66.67% |
4 / 6 |
12.98 | |
0.00% |
0 / 1 |
| __invoke | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| getIndexDocuments | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| getReport | |
66.67% |
12 / 18 |
|
0.00% |
0 / 1 |
3.33 | |||
| getDocumentBuilder | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| getSearchProxy | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getAdvancedSearchChecker | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2018-2022 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\search\tasks; |
| 24 | |
| 25 | use common_Exception; |
| 26 | use common_exception_InconsistentData; |
| 27 | use common_exception_MissingParameter; |
| 28 | use oat\generis\model\OntologyAwareTrait; |
| 29 | use oat\oatbox\action\Action; |
| 30 | use oat\oatbox\log\LoggerAwareTrait; |
| 31 | use oat\oatbox\reporting\Report; |
| 32 | use oat\tao\model\AdvancedSearch\AdvancedSearchChecker; |
| 33 | use oat\tao\model\search\index\DocumentBuilder\IndexDocumentBuilderInterface; |
| 34 | use oat\tao\model\search\SearchProxy; |
| 35 | use oat\tao\model\taskQueue\Task\TaskAwareInterface; |
| 36 | use oat\tao\model\taskQueue\Task\TaskAwareTrait; |
| 37 | use oat\taoAdvancedSearch\model\Index\Service\AdvancedSearchIndexDocumentBuilder; |
| 38 | use Zend\ServiceManager\ServiceLocatorAwareInterface; |
| 39 | use Zend\ServiceManager\ServiceLocatorAwareTrait; |
| 40 | |
| 41 | /** |
| 42 | * @author Ilya Yarkavets <ilya@taotesting.com> |
| 43 | */ |
| 44 | class UpdateResourceInIndex implements Action, ServiceLocatorAwareInterface, TaskAwareInterface |
| 45 | { |
| 46 | use ServiceLocatorAwareTrait; |
| 47 | use OntologyAwareTrait; |
| 48 | use TaskAwareTrait; |
| 49 | use LoggerAwareTrait; |
| 50 | |
| 51 | /** @var string[] */ |
| 52 | private array $resourceUris = []; |
| 53 | |
| 54 | /** |
| 55 | * @throws common_Exception |
| 56 | * @throws common_exception_InconsistentData |
| 57 | * @throws common_exception_MissingParameter |
| 58 | */ |
| 59 | public function __invoke($params): Report |
| 60 | { |
| 61 | if (empty($params[0])) { |
| 62 | throw new common_exception_MissingParameter(); |
| 63 | } |
| 64 | |
| 65 | $resourceUris = is_array($params[0]) ? $params[0] : [$params[0]]; |
| 66 | |
| 67 | return $this->getReport( |
| 68 | $this->getSearchProxy()->index( |
| 69 | $this->getIndexDocuments($resourceUris) |
| 70 | ) |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @throws common_exception_InconsistentData |
| 76 | * @throws common_Exception |
| 77 | */ |
| 78 | private function getIndexDocuments(array $resourceUris): array |
| 79 | { |
| 80 | $documentBuilder = $this->getDocumentBuilder(); |
| 81 | $documents = []; |
| 82 | |
| 83 | foreach ($resourceUris as $resourceUri) { |
| 84 | $this->resourceUris[] = $resourceUri; |
| 85 | |
| 86 | $documents[] = $documentBuilder->createDocumentFromResource($this->getResource($resourceUri)); |
| 87 | } |
| 88 | |
| 89 | return $documents; |
| 90 | } |
| 91 | |
| 92 | private function getReport($numberOfIndexed): Report |
| 93 | { |
| 94 | $expectedIndexations = count($this->resourceUris); |
| 95 | $resourceUris = implode(',', $this->resourceUris); |
| 96 | |
| 97 | if ($numberOfIndexed === $expectedIndexations) { |
| 98 | $message = sprintf('Document(s) "%s" successfully indexed', $resourceUris); |
| 99 | |
| 100 | $this->logInfo($message); |
| 101 | |
| 102 | return Report::createSuccess($message); |
| 103 | } |
| 104 | |
| 105 | if ($numberOfIndexed === 0) { |
| 106 | $message = sprintf('Expecting document(s) to be indexed (got zero) for ID(s) "%s"', $resourceUris); |
| 107 | |
| 108 | $this->logError($message); |
| 109 | |
| 110 | return Report::createError($message); |
| 111 | } |
| 112 | |
| 113 | $message = sprintf( |
| 114 | 'Expecting "%s" document(s) to be indexed (got %s) for ID(s) "%s"', |
| 115 | $expectedIndexations, |
| 116 | $numberOfIndexed, |
| 117 | $resourceUris |
| 118 | ); |
| 119 | |
| 120 | $this->logWarning($message); |
| 121 | |
| 122 | return Report::createWarning($message); |
| 123 | } |
| 124 | |
| 125 | private function getDocumentBuilder(): IndexDocumentBuilderInterface |
| 126 | { |
| 127 | if ($this->getAdvancedSearchChecker()->isEnabled()) { |
| 128 | return $this->getServiceLocator()->getContainer()->get(AdvancedSearchIndexDocumentBuilder::class); |
| 129 | } |
| 130 | |
| 131 | return $this->getServiceLocator()->getContainer()->get(IndexDocumentBuilderInterface::class); |
| 132 | } |
| 133 | |
| 134 | private function getSearchProxy(): SearchProxy |
| 135 | { |
| 136 | return $this->getServiceLocator()->getContainer()->get(SearchProxy::SERVICE_ID); |
| 137 | } |
| 138 | |
| 139 | private function getAdvancedSearchChecker(): AdvancedSearchChecker |
| 140 | { |
| 141 | return $this->getServiceLocator()->getContainer()->get(AdvancedSearchChecker::class); |
| 142 | } |
| 143 | } |