Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 55 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| tao_actions_RestController | |
0.00% |
0 / 55 |
|
0.00% |
0 / 3 |
600 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| getClassFromRequest | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
156 | |||
| createSubClass | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
90 | |||
| 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) 2016 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | use oat\generis\model\OntologyAwareTrait; |
| 23 | use oat\oatbox\service\ServiceManager; |
| 24 | use oat\tao\model\action\RestControllerInterface; |
| 25 | use oat\tao\model\routing\AnnotationReader\security; |
| 26 | |
| 27 | abstract class tao_actions_RestController extends \tao_actions_CommonModule implements RestControllerInterface |
| 28 | { |
| 29 | use OntologyAwareTrait; |
| 30 | use \tao_actions_RestTrait; |
| 31 | |
| 32 | public const CLASS_URI_PARAM = 'class-uri'; |
| 33 | public const CLASS_LABEL_PARAM = 'class-label'; |
| 34 | public const CLASS_COMMENT_PARAM = 'class-comment'; |
| 35 | public const PARENT_CLASS_URI_PARAM = 'parent-class-uri'; |
| 36 | |
| 37 | /** |
| 38 | * Check response encoding requested |
| 39 | * |
| 40 | * tao_actions_RestModule constructor. |
| 41 | * @security("hide"); |
| 42 | */ |
| 43 | public function __construct() |
| 44 | { |
| 45 | if ($this->hasHeader("Accept")) { |
| 46 | try { |
| 47 | $this->responseEncoding = tao_helpers_Http::acceptHeader( |
| 48 | $this->getAcceptableMimeTypes(), |
| 49 | $this->getHeader("Accept") |
| 50 | ) ?? $this->responseEncoding; |
| 51 | } catch (common_exception_ClientException $e) { |
| 52 | header('Content-Type: ' . $this->responseEncoding); |
| 53 | $this->setServiceLocator(ServiceManager::getServiceManager()); |
| 54 | $this->returnFailure($e); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | header('Content-Type: ' . $this->responseEncoding); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @OA\Schema( |
| 63 | * schema="tao.GenerisClass.Search", |
| 64 | * type="object", |
| 65 | * @OA\Property( |
| 66 | * property="class-uri", |
| 67 | * type="string", |
| 68 | * description="Target class uri" |
| 69 | * ), |
| 70 | * @OA\Property( |
| 71 | * property="class-label", |
| 72 | * type="string", |
| 73 | * description="Target class label. If label is not unique first match will be used" |
| 74 | * ) |
| 75 | * ) |
| 76 | * |
| 77 | * Get class instance from request parameters |
| 78 | * If more than one class with given label exists the first open will be picked up. |
| 79 | * @param core_kernel_classes_Class $rootClass |
| 80 | * @return core_kernel_classes_Class|null |
| 81 | * @throws common_exception_RestApi |
| 82 | */ |
| 83 | protected function getClassFromRequest(\core_kernel_classes_Class $rootClass) |
| 84 | { |
| 85 | $class = null; |
| 86 | if ($this->hasRequestParameter(self::CLASS_URI_PARAM) && $this->hasRequestParameter(self::CLASS_LABEL_PARAM)) { |
| 87 | throw new \common_exception_RestApi( |
| 88 | self::CLASS_URI_PARAM . ' and ' . self::CLASS_LABEL_PARAM |
| 89 | . ' parameters do not supposed to be used simultaneously.' |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | if ( |
| 94 | !$this->hasRequestParameter(self::CLASS_URI_PARAM) |
| 95 | && !$this->hasRequestParameter(self::CLASS_LABEL_PARAM) |
| 96 | ) { |
| 97 | $class = $rootClass; |
| 98 | } |
| 99 | |
| 100 | if ($this->hasRequestParameter(self::CLASS_URI_PARAM)) { |
| 101 | $classUriParam = $this->getRequestParameter(self::CLASS_URI_PARAM); |
| 102 | if (!$classUriParam) { |
| 103 | throw new \common_exception_RestApi( |
| 104 | self::CLASS_URI_PARAM . ' is not valid.' |
| 105 | ); |
| 106 | } |
| 107 | $class = $this->getClass($classUriParam); |
| 108 | } |
| 109 | if ($this->hasRequestParameter(self::CLASS_LABEL_PARAM)) { |
| 110 | $label = $this->getRequestParameter(self::CLASS_LABEL_PARAM); |
| 111 | foreach ($rootClass->getSubClasses(true) as $subClass) { |
| 112 | if ($subClass->getLabel() === $label) { |
| 113 | $class = $subClass; |
| 114 | break; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | if ($class === null || !$class->exists()) { |
| 119 | throw new \common_exception_RestApi( |
| 120 | 'Class does not exist. Please use valid ' . self::CLASS_URI_PARAM . ' or ' . self::CLASS_LABEL_PARAM |
| 121 | ); |
| 122 | } |
| 123 | return $class; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * @OA\Schema( |
| 128 | * schema="tao.GenerisClass.New", |
| 129 | * type="object", |
| 130 | * @OA\Property( |
| 131 | * property="class-label", |
| 132 | * type="string", |
| 133 | * description="Class label" |
| 134 | * ), |
| 135 | * @OA\Property( |
| 136 | * property="class-comment", |
| 137 | * type="string", |
| 138 | * description="Class comment" |
| 139 | * ), |
| 140 | * @OA\Property( |
| 141 | * property="parent-class-uri", |
| 142 | * type="string", |
| 143 | * description="Parent class uri, root class by default" |
| 144 | * ) |
| 145 | * ) |
| 146 | * |
| 147 | * Create sub class of given root class. |
| 148 | * |
| 149 | * @param core_kernel_classes_Class $rootClass |
| 150 | * @throws \common_Exception |
| 151 | * @throws \common_exception_InconsistentData |
| 152 | * @throws \common_exception_ClassAlreadyExists |
| 153 | * @return \core_kernel_classes_Class |
| 154 | */ |
| 155 | protected function createSubClass(\core_kernel_classes_Class $rootClass) |
| 156 | { |
| 157 | if (!$this->hasRequestParameter(static::CLASS_LABEL_PARAM)) { |
| 158 | throw new \common_exception_RestApi('Missed required parameter: ' . static::CLASS_LABEL_PARAM); |
| 159 | } |
| 160 | $label = $this->getRequestParameter(static::CLASS_LABEL_PARAM); |
| 161 | |
| 162 | if ($this->hasRequestParameter(static::PARENT_CLASS_URI_PARAM)) { |
| 163 | $parentClass = $this->getClass($this->getRequestParameter(static::PARENT_CLASS_URI_PARAM)); |
| 164 | if ($parentClass->getUri() !== $rootClass->getUri() && !$parentClass->isSubClassOf($rootClass)) { |
| 165 | throw new \common_Exception(__('Class uri provided is not a valid class.')); |
| 166 | } |
| 167 | $rootClass = $parentClass; |
| 168 | } |
| 169 | |
| 170 | $comment = $this->hasRequestParameter(static::CLASS_COMMENT_PARAM) |
| 171 | ? $this->getRequestParameter(static::CLASS_COMMENT_PARAM) |
| 172 | : ''; |
| 173 | |
| 174 | $class = null; |
| 175 | |
| 176 | /** @var \core_kernel_classes_Class $subClass */ |
| 177 | foreach ($rootClass->getSubClasses() as $subClass) { |
| 178 | if ($subClass->getLabel() === $label) { |
| 179 | throw new \common_exception_ClassAlreadyExists($subClass); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | if (!$class) { |
| 184 | $class = $rootClass->createSubClass($label, $comment); |
| 185 | } |
| 186 | |
| 187 | return $class; |
| 188 | } |
| 189 | } |