Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 28 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| AbstractCreateOrReuse | |
0.00% |
0 / 28 |
|
0.00% |
0 / 5 |
132 | |
0.00% |
0 / 1 |
| getRootClass | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getUniquePredicates | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getSearchService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| searchResource | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
12 | |||
| createResource | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| hasResource | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| getResource | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| 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 | namespace oat\generis\model\resource; |
| 23 | |
| 24 | use core_kernel_classes_Resource; |
| 25 | use oat\generis\model\kernel\persistence\smoothsql\search\ComplexSearchService; |
| 26 | use oat\generis\model\kernel\persistence\smoothsql\search\TaoResultSet; |
| 27 | use oat\generis\model\resource\exception\DuplicateResourceException; |
| 28 | use oat\oatbox\service\ConfigurableService; |
| 29 | use oat\generis\model\OntologyAwareTrait; |
| 30 | |
| 31 | /** |
| 32 | * Abstract base of CreateAndReuse service |
| 33 | * |
| 34 | * @author Christophe GARCIA <christopheg@taotesting.com> |
| 35 | */ |
| 36 | abstract class AbstractCreateOrReuse extends ConfigurableService implements CreateOrReuseInterface |
| 37 | { |
| 38 | use OntologyAwareTrait; |
| 39 | |
| 40 | /** |
| 41 | * Returns the common parent all resources have |
| 42 | * |
| 43 | * @return \core_kernel_classes_Class |
| 44 | */ |
| 45 | abstract public function getRootClass(); |
| 46 | |
| 47 | /** |
| 48 | * List of keys that need to be identical between |
| 49 | * two resources to represent equivalence |
| 50 | * |
| 51 | * @return string[] |
| 52 | */ |
| 53 | abstract public function getUniquePredicates(); |
| 54 | |
| 55 | /** |
| 56 | * WILL break on non smooth implementations |
| 57 | * |
| 58 | * @return ComplexSearchService |
| 59 | */ |
| 60 | protected function getSearchService() |
| 61 | { |
| 62 | return $this->getModel()->getSearchInterface(); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * |
| 67 | * @param array $values |
| 68 | * @return TaoResultSet |
| 69 | */ |
| 70 | protected function searchResource(array $values) |
| 71 | { |
| 72 | |
| 73 | $searchService = $this->getSearchService(); |
| 74 | $gateWay = $searchService->getGateway(); |
| 75 | |
| 76 | $searchQueryBuilder = $gateWay->query(); |
| 77 | |
| 78 | $searchService->searchType($searchQueryBuilder, $this->getRootClass()->getUri(), true); |
| 79 | |
| 80 | $criterion = $searchQueryBuilder->newQuery(); |
| 81 | |
| 82 | foreach ($this->getUniquePredicates() as $field) { |
| 83 | if (array_key_exists($field, $values)) { |
| 84 | $value = $values[$field]; |
| 85 | $criterion->add($field)->equals($value); |
| 86 | } else { |
| 87 | \common_Logger::i('Predicate ' . $field . ' is not found.'); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | $searchQueryBuilder->setCriteria($criterion)->setLimit(1); |
| 92 | |
| 93 | return $gateWay->search($searchQueryBuilder); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * return a new resource |
| 98 | * @param array $values |
| 99 | * @return core_kernel_classes_Resource |
| 100 | */ |
| 101 | protected function createResource(array $values) |
| 102 | { |
| 103 | return $this->getRootClass()->createInstanceWithProperties($values); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * |
| 108 | * @param array $values |
| 109 | * @return boolean |
| 110 | * @throws DuplicateResourceException |
| 111 | */ |
| 112 | public function hasResource(array $values) |
| 113 | { |
| 114 | |
| 115 | $result = $this->searchResource($values); |
| 116 | $count = $result->count(); |
| 117 | |
| 118 | if ($count === 1) { |
| 119 | return true; |
| 120 | } elseif ($count === 0) { |
| 121 | return false; |
| 122 | } else { |
| 123 | throw new DuplicateResourceException($this->getRootClass()->getUri(), $values); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * |
| 129 | * @param array $values |
| 130 | * @return core_kernel_classes_Resource |
| 131 | * @throws DuplicateResourceException |
| 132 | */ |
| 133 | public function getResource(array $values) |
| 134 | { |
| 135 | |
| 136 | $result = $this->searchResource($values); |
| 137 | $count = $result->count(); |
| 138 | |
| 139 | if ($count === 1) { |
| 140 | return $result->current(); |
| 141 | } elseif ($count === 0) { |
| 142 | return $this->createResource($values); |
| 143 | } else { |
| 144 | throw new DuplicateResourceException($this->getRootClass()->getUri(), $values); |
| 145 | } |
| 146 | } |
| 147 | } |