Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
21 / 21 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| SearchQueryFactory | |
100.00% |
21 / 21 |
|
100.00% |
2 / 2 |
7 | |
100.00% |
1 / 1 |
| create | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
4 | |||
| checkRequiredParams | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| 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) 2020-2022 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\search; |
| 24 | |
| 25 | use oat\oatbox\service\ConfigurableService; |
| 26 | use Psr\Http\Message\ServerRequestInterface; |
| 27 | |
| 28 | class SearchQueryFactory extends ConfigurableService |
| 29 | { |
| 30 | /** |
| 31 | * @throws CreateSearchQueryException |
| 32 | */ |
| 33 | public function create(ServerRequestInterface $request): SearchQuery |
| 34 | { |
| 35 | $params = $request->getQueryParams(); |
| 36 | |
| 37 | $this->checkRequiredParams($params); |
| 38 | |
| 39 | $rows = isset($params['rows']) ? (int)$params['rows'] : null; |
| 40 | $page = isset($params['page']) ? (int)$params['page'] : null; |
| 41 | $startRow = is_null($rows) ? 0 : $rows * ($page - 1); |
| 42 | |
| 43 | $query = new SearchQuery( |
| 44 | $params['params']['query'], |
| 45 | $params['params']['rootNode'], |
| 46 | $params['params']['parentNode'], |
| 47 | $startRow, |
| 48 | $rows, |
| 49 | $page, |
| 50 | $params['sortby'] ?? null, |
| 51 | $params['sortorder'] ?? null |
| 52 | ); |
| 53 | |
| 54 | $query->setStructure($params['params']['structure']); |
| 55 | |
| 56 | return $query; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @throws CreateSearchQueryException |
| 61 | */ |
| 62 | private function checkRequiredParams(array $params): void |
| 63 | { |
| 64 | if (!isset($params['params']['rootNode'])) { |
| 65 | throw new CreateSearchQueryException('Root node is missing from request'); |
| 66 | } |
| 67 | |
| 68 | if (!isset($params['params']['query'])) { |
| 69 | throw new CreateSearchQueryException('User input is missing'); |
| 70 | } |
| 71 | } |
| 72 | } |