Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.91% |
20 / 22 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
| ControllerService | |
90.91% |
20 / 22 |
|
80.00% |
4 / 5 |
11.09 | |
0.00% |
0 / 1 |
| checkAnnotations | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| checkAbstract | |
60.00% |
3 / 5 |
|
0.00% |
0 / 1 |
3.58 | |||
| checkController | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| checkPublic | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| getAction | |
100.00% |
3 / 3 |
|
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 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | * @author Alexander Zagovorichev <zagovorichev@1pt.com> |
| 21 | */ |
| 22 | |
| 23 | namespace oat\tao\model\routing; |
| 24 | |
| 25 | use oat\oatbox\service\ConfigurableService; |
| 26 | use oat\tao\model\http\Controller; |
| 27 | use ReflectionClass; |
| 28 | use ReflectionMethod; |
| 29 | |
| 30 | class ControllerService extends ConfigurableService |
| 31 | { |
| 32 | public const SERVICE_ID = 'tao/controllerService'; |
| 33 | |
| 34 | /** |
| 35 | * @param $controllerClass |
| 36 | * @param string $action |
| 37 | * @throws RouterException |
| 38 | */ |
| 39 | private function checkAnnotations($controllerClass, $action = '') |
| 40 | { |
| 41 | /** @var RouteAnnotationService $routeAnnotationService */ |
| 42 | $routeAnnotationService = $this->getServiceLocator()->get(RouteAnnotationService::SERVICE_ID); |
| 43 | // extra layer of the security - to not launch action if denied |
| 44 | if (!$routeAnnotationService->hasAccess($controllerClass, $action)) { |
| 45 | $message = $action ? "Unable to run the action '" |
| 46 | . $action . "' in '" . $controllerClass |
| 47 | . "', blocked by route annotations." : "Class '$controllerClass' blocked by route annotation"; |
| 48 | throw new RouterException($message); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @param $controllerClass |
| 54 | * @throws RouterException |
| 55 | */ |
| 56 | private function checkAbstract($controllerClass) |
| 57 | { |
| 58 | try { |
| 59 | $abstractClass = new ReflectionClass($controllerClass); |
| 60 | } catch (\ReflectionException $e) { |
| 61 | throw new RouterException($e->getMessage()); |
| 62 | } |
| 63 | if ($abstractClass->isAbstract()) { |
| 64 | throw new RouterException('Attempt to run an action from the Abstract class "' . $controllerClass . '"'); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @param string $controllerClass |
| 70 | * @return mixed |
| 71 | * @throws RouterException |
| 72 | */ |
| 73 | public function checkController($controllerClass) |
| 74 | { |
| 75 | // abstract class can't be loaded |
| 76 | $this->checkAbstract($controllerClass); |
| 77 | |
| 78 | // check if blocked by annotations |
| 79 | $this->checkAnnotations($controllerClass); |
| 80 | |
| 81 | return $controllerClass; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @param $class |
| 86 | * @param $action |
| 87 | * @throws RouterException |
| 88 | */ |
| 89 | private function checkPublic($class, $action) |
| 90 | { |
| 91 | try { |
| 92 | // protected method |
| 93 | $reflection = new ReflectionMethod($class, $action); |
| 94 | if (!$reflection->isPublic()) { |
| 95 | throw new RouterException('The method "' . $action . '" is not public in the class "' . $class . '"'); |
| 96 | } |
| 97 | } catch (\ReflectionException $e) { |
| 98 | throw new RouterException($e->getMessage()); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @param string $controllerClass |
| 104 | * @param string $action |
| 105 | * @throws RouterException |
| 106 | * @return string |
| 107 | */ |
| 108 | public function getAction($controllerClass = '', $action = '') |
| 109 | { |
| 110 | // method needs to be public |
| 111 | $this->checkPublic($controllerClass, $action); |
| 112 | // check if blocked by annotations |
| 113 | $this->checkAnnotations($controllerClass, $action); |
| 114 | |
| 115 | return $action; |
| 116 | } |
| 117 | } |