Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 43 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| tao_models_classes_CrudService | |
0.00% |
0 / 43 |
|
0.00% |
0 / 8 |
462 | |
0.00% |
0 / 1 |
| isInScope | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getClassService | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getRootClass | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| getAll | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| delete | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| deleteAll | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| create | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| update | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
30 | |||
| 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) 2013-2014 (original work) Open Assessment Technologies SA |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | use oat\generis\model\OntologyAwareTrait; |
| 23 | |
| 24 | /** |
| 25 | * |
| 26 | * Crud services implements basic CRUD services, orginally intended for |
| 27 | * REST controllers/ HTTP exception handlers. |
| 28 | * Consequently the signatures and behaviors is closer to REST and throwing |
| 29 | * HTTP like exceptions. |
| 30 | * |
| 31 | * @author Patrick Plichart, patrick@taotesting.com |
| 32 | * |
| 33 | */ |
| 34 | abstract class tao_models_classes_CrudService extends tao_models_classes_Service |
| 35 | { |
| 36 | use OntologyAwareTrait; |
| 37 | |
| 38 | /** |
| 39 | * |
| 40 | * @author Patrick Plichart, patrick@taotesting.com |
| 41 | * @param string $uri |
| 42 | * @throws common_exception_InvalidArgumentType |
| 43 | * @return boolean |
| 44 | */ |
| 45 | public function isInScope($uri) |
| 46 | { |
| 47 | if (!(common_Utils::isUri($uri))) { |
| 48 | throw new common_exception_InvalidArgumentType(); |
| 49 | } |
| 50 | $resource = $this->getClass($uri); |
| 51 | return $resource->hasType($this->getRootClass()); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * |
| 56 | * @author Patrick Plichart, patrick@taotesting.com |
| 57 | * return tao_models_classes_ClassService |
| 58 | */ |
| 59 | abstract protected function getClassService(); |
| 60 | |
| 61 | /** |
| 62 | * |
| 63 | * @author Patrick Plichart, patrick@taotesting.com |
| 64 | * return core_kernel_classes_Class |
| 65 | */ |
| 66 | protected function getRootClass() |
| 67 | { |
| 68 | return $this->getClassService()->getRootClass(); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * |
| 73 | * @param string uri |
| 74 | * @throws common_Exception_NoContent |
| 75 | * @throws common_exception_InvalidArgumentType |
| 76 | * @return object |
| 77 | */ |
| 78 | public function get($uri) |
| 79 | { |
| 80 | if (!common_Utils::isUri($uri)) { |
| 81 | throw new common_exception_InvalidArgumentType(); |
| 82 | } |
| 83 | if (!($this->isInScope($uri))) { |
| 84 | throw new common_exception_PreConditionFailure("The URI must be a valid resource under the root Class"); |
| 85 | } |
| 86 | $resource = $this->getResource($uri); |
| 87 | $formater = new core_kernel_classes_ResourceFormatter(); |
| 88 | return $formater->getResourceDescription($resource, false); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * |
| 93 | * @author Patrick Plichart, patrick@taotesting.com |
| 94 | * @return stdClass |
| 95 | */ |
| 96 | public function getAll() |
| 97 | { |
| 98 | $formater = new core_kernel_classes_ResourceFormatter(); |
| 99 | $resources = []; |
| 100 | foreach ($this->getRootClass()->getInstances(true) as $resource) { |
| 101 | $resources[] = $formater->getResourceDescription($resource, false); |
| 102 | } |
| 103 | return $resources; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * |
| 108 | * @author Patrick Plichart, patrick@taotesting.com |
| 109 | * @param string $uri |
| 110 | * @throws common_exception_InvalidArgumentType |
| 111 | * @throws common_exception_PreConditionFailure |
| 112 | * @throws common_exception_NoContent |
| 113 | */ |
| 114 | public function delete($uri) |
| 115 | { |
| 116 | if (!common_Utils::isUri($uri)) { |
| 117 | throw new common_exception_InvalidArgumentType(); |
| 118 | } |
| 119 | if (!($this->isInScope($uri))) { |
| 120 | throw new common_exception_PreConditionFailure("The URI must be a valid resource under the root Class"); |
| 121 | } |
| 122 | $resource = $this->getResource($uri); |
| 123 | // if the resource does not exist, indicate a not found exception |
| 124 | if (count($resource->getRdfTriples()->sequence) == 0) { |
| 125 | throw new common_exception_NoContent(); |
| 126 | } |
| 127 | $resource->delete(); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * |
| 132 | * @author Patrick Plichart, patrick@taotesting.com |
| 133 | */ |
| 134 | public function deleteAll() |
| 135 | { |
| 136 | foreach ($this->getRootClass()->getInstances(true) as $resource) { |
| 137 | $resource->delete(); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * |
| 143 | * @author Patrick Plichart, patrick@taotesting.com |
| 144 | * @param string $label |
| 145 | * @param string $type |
| 146 | * @param array $propertiesValues |
| 147 | * @return core_kernel_classes_Resource |
| 148 | */ |
| 149 | public function create($label = "", $type = null, $propertiesValues = []) |
| 150 | { |
| 151 | $type = (isset($type)) ? $this->getClass($type) : $this->getRootClass(); |
| 152 | |
| 153 | $resource = $this->getClassService()->createInstance($type, $label); |
| 154 | $resource->setPropertiesValues($propertiesValues); |
| 155 | return $resource; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * |
| 160 | * @author Patrick Plichart, patrick@taotesting.com |
| 161 | * @param string $uri |
| 162 | * @param array $propertiesValues |
| 163 | * @throws common_exception_InvalidArgumentType |
| 164 | * @throws common_exception_PreConditionFailure |
| 165 | * @throws common_exception_NoContent |
| 166 | * @return core_kernel_classes_Resource |
| 167 | */ |
| 168 | public function update($uri, $propertiesValues = []) |
| 169 | { |
| 170 | if (!common_Utils::isUri($uri)) { |
| 171 | throw new common_exception_InvalidArgumentType(); |
| 172 | } |
| 173 | if (!($this->isInScope($uri))) { |
| 174 | throw new common_exception_PreConditionFailure("The URI must be a valid resource under the root Class"); |
| 175 | } |
| 176 | $resource = $this->getResource($uri); |
| 177 | // if the resource does not exist, indicate a not found exception |
| 178 | if (count($resource->getRdfTriples()->sequence) == 0) { |
| 179 | throw new common_exception_NoContent(); |
| 180 | } |
| 181 | foreach ($propertiesValues as $uri => $parameterValue) { |
| 182 | $resource->editPropertyValues($this->getProperty($uri), $parameterValue); |
| 183 | } |
| 184 | return $resource; |
| 185 | } |
| 186 | } |