Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.12% |
16 / 17 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| UpdateClassInIndex | |
94.12% |
16 / 17 |
|
66.67% |
2 / 3 |
7.01 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __invoke | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
4 | |||
| getIndexIteratorFactory | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| 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-2021 (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_Error; |
| 26 | use common_exception_MissingParameter; |
| 27 | use oat\oatbox\reporting\Report; |
| 28 | use oat\generis\model\OntologyAwareTrait; |
| 29 | use oat\oatbox\action\Action; |
| 30 | use oat\oatbox\log\LoggerAwareTrait; |
| 31 | use oat\tao\model\search\index\IndexIteratorFactory; |
| 32 | use oat\tao\model\search\SearchProxy; |
| 33 | use oat\tao\model\taskQueue\Task\TaskAwareInterface; |
| 34 | use oat\tao\model\taskQueue\Task\TaskAwareTrait; |
| 35 | use Zend\ServiceManager\ServiceLocatorAwareInterface; |
| 36 | use Zend\ServiceManager\ServiceLocatorAwareTrait; |
| 37 | |
| 38 | class UpdateClassInIndex implements Action, ServiceLocatorAwareInterface, TaskAwareInterface |
| 39 | { |
| 40 | use ServiceLocatorAwareTrait; |
| 41 | use OntologyAwareTrait; |
| 42 | use TaskAwareTrait; |
| 43 | use LoggerAwareTrait; |
| 44 | |
| 45 | /** @var IndexIteratorFactory */ |
| 46 | private $indexIteratorFactory; |
| 47 | |
| 48 | public function __construct(IndexIteratorFactory $indexIteratorFactory = null) |
| 49 | { |
| 50 | $this->indexIteratorFactory = $indexIteratorFactory; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @throws common_exception_MissingParameter |
| 55 | * @throws common_exception_Error |
| 56 | */ |
| 57 | public function __invoke($params): Report |
| 58 | { |
| 59 | if (empty($params) || empty($params[0])) { |
| 60 | throw new common_exception_MissingParameter(); |
| 61 | } |
| 62 | |
| 63 | /** @var SearchProxy $searchService */ |
| 64 | $searchService = $this->getServiceLocator()->get(SearchProxy::SERVICE_ID); |
| 65 | $numberOfIndexedResources = $searchService->index( |
| 66 | $this->getIndexIteratorFactory()->create($params) |
| 67 | ); |
| 68 | |
| 69 | $this->logInfo($numberOfIndexedResources . ' resources have been indexed by ' . static::class); |
| 70 | |
| 71 | $type = Report::TYPE_SUCCESS; |
| 72 | $message = "Documents in index were successfully updated."; |
| 73 | |
| 74 | if ($numberOfIndexedResources < 1) { |
| 75 | $type = Report::TYPE_INFO; |
| 76 | $message = "Zero documents were added/updated in index."; |
| 77 | } |
| 78 | |
| 79 | return new Report($type, $message); |
| 80 | } |
| 81 | |
| 82 | private function getIndexIteratorFactory(): IndexIteratorFactory |
| 83 | { |
| 84 | if (null === $this->indexIteratorFactory) { |
| 85 | $this->indexIteratorFactory = new IndexIteratorFactory($this->getServiceLocator()); |
| 86 | } |
| 87 | |
| 88 | return $this->indexIteratorFactory; |
| 89 | } |
| 90 | } |