Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 27 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| AbstractApiRoute | |
0.00% |
0 / 27 |
|
0.00% |
0 / 3 |
156 | |
0.00% |
0 / 1 |
| resolve | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getController | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
| getAction | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
42 | |||
| 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 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\tao\model\routing; |
| 23 | |
| 24 | use Psr\Http\Message\ServerRequestInterface; |
| 25 | |
| 26 | /** |
| 27 | * Class AbstractApiRoute |
| 28 | * Route for RestApi controllers |
| 29 | * @package oat\tao\model\routing |
| 30 | * @author Aleh Hutnikau, <hutnikau@1pt.com> |
| 31 | */ |
| 32 | abstract class AbstractApiRoute extends AbstractRoute |
| 33 | { |
| 34 | /** |
| 35 | * @param ServerRequestInterface $request |
| 36 | * @return null|string |
| 37 | * @throws \ResolverException |
| 38 | */ |
| 39 | public function resolve(ServerRequestInterface $request) |
| 40 | { |
| 41 | $relativeUrl = \tao_helpers_Request::getRelativeUrl($request->getRequestTarget()); |
| 42 | try { |
| 43 | return $this->getController($relativeUrl) . '@' . $this->getAction($request->getMethod()); |
| 44 | } catch (RouterException $e) { |
| 45 | return null; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @param $relativeUrl |
| 51 | * @return string |
| 52 | * @throws RouterException |
| 53 | */ |
| 54 | protected function getController($relativeUrl) |
| 55 | { |
| 56 | $parts = explode('/', $relativeUrl); |
| 57 | $prefix = $this->getControllerPrefix(); |
| 58 | if (strpos($relativeUrl, $this->getId()) !== 0) { |
| 59 | throw new RouterException('Path does not match'); |
| 60 | } |
| 61 | |
| 62 | if (!isset($parts[2])) { |
| 63 | throw new RouterException('Missed controller name in uri: ' . $relativeUrl); |
| 64 | } |
| 65 | |
| 66 | if (!class_exists($prefix . ucfirst($parts[2]))) { |
| 67 | throw new RouterException('Controller ' . $parts[2] . ' does not exists'); |
| 68 | } |
| 69 | |
| 70 | return $prefix . ucfirst($parts[2]); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @param $method |
| 75 | * @return string |
| 76 | * @throws RouterException |
| 77 | */ |
| 78 | protected function getAction($method) |
| 79 | { |
| 80 | switch ($method) { |
| 81 | case 'GET': |
| 82 | $action = 'get'; |
| 83 | break; |
| 84 | case 'PUT': |
| 85 | $action = 'put'; |
| 86 | break; |
| 87 | case 'POST': |
| 88 | $action = 'post'; |
| 89 | break; |
| 90 | case 'DELETE': |
| 91 | $action = 'delete'; |
| 92 | break; |
| 93 | default: |
| 94 | throw new RouterException('Method `' . $method . ' is not supported'); |
| 95 | } |
| 96 | |
| 97 | return $action; |
| 98 | } |
| 99 | } |