Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 37 |
|
0.00% |
0 / 15 |
CRAP | |
0.00% |
0 / 1 |
| ListService | |
0.00% |
0 / 37 |
|
0.00% |
0 / 15 |
342 | |
0.00% |
0 / 1 |
| isEditable | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getListElement | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| getListElements | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| getMaxItems | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setMaxItems | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| removeList | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| createListElement | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| isRemote | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| createUri | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getValueService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getParentPropertyListCachedRepository | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getRemoteListClassSpecification | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getEditableListClassSpecification | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getListDeleter | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getContainer | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 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-2021 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\taoBackOffice\model\lists; |
| 24 | |
| 25 | use Throwable; |
| 26 | use tao_models_classes_ListService; |
| 27 | use Psr\Container\ContainerInterface; |
| 28 | use core_kernel_classes_Class as RdfClass; |
| 29 | use oat\generis\model\kernel\uri\UriProvider; |
| 30 | use oat\tao\model\Lists\Business\Domain\Value; |
| 31 | use oat\taoBackOffice\model\lists\Service\ListDeleter; |
| 32 | use oat\tao\model\Lists\Business\Domain\ValueCollection; |
| 33 | use oat\tao\model\Specification\ClassSpecificationInterface; |
| 34 | use oat\tao\model\Lists\Business\Service\ValueCollectionService; |
| 35 | use oat\taoBackOffice\model\lists\Contract\ListDeleterInterface; |
| 36 | use oat\tao\model\Lists\Business\Input\ValueCollectionSearchInput; |
| 37 | use oat\tao\model\Lists\Business\Domain\ValueCollectionSearchRequest; |
| 38 | use oat\tao\model\Lists\Business\Specification\RemoteListClassSpecification; |
| 39 | use oat\tao\model\Lists\Business\Specification\EditableListClassSpecification; |
| 40 | use oat\tao\model\Lists\DataAccess\Repository\ParentPropertyListCachedRepository; |
| 41 | |
| 42 | class ListService extends tao_models_classes_ListService |
| 43 | { |
| 44 | /** @var int */ |
| 45 | private $maxItems = 1000; |
| 46 | |
| 47 | /** |
| 48 | * Whenever or not a list is editable |
| 49 | * The Language list should not be editable. |
| 50 | * |
| 51 | * @return bool |
| 52 | */ |
| 53 | public function isEditable(RdfClass $listClass) |
| 54 | { |
| 55 | return $this->getEditableListClassSpecification()->isSatisfiedBy($listClass); |
| 56 | } |
| 57 | |
| 58 | public function getListElement(RdfClass $listClass, $uri) |
| 59 | { |
| 60 | $request = new ValueCollectionSearchRequest(); |
| 61 | $request->setValueCollectionUri($listClass->getUri())->setUris($uri); |
| 62 | |
| 63 | $result = $this->getValueService()->findAll( |
| 64 | new ValueCollectionSearchInput($request) |
| 65 | ); |
| 66 | |
| 67 | return $result->count() === 0 |
| 68 | ? null |
| 69 | : iterator_to_array($result->getIterator())[0]; |
| 70 | } |
| 71 | |
| 72 | public function getListElements(RdfClass $listClass, $sort = true, $limit = 0) |
| 73 | { |
| 74 | $request = new ValueCollectionSearchRequest(); |
| 75 | $request->setValueCollectionUri($listClass->getUri()); |
| 76 | |
| 77 | if ($limit) { |
| 78 | $request->setLimit($limit); |
| 79 | } |
| 80 | |
| 81 | $result = $this->getValueService()->findAll( |
| 82 | new ValueCollectionSearchInput($request) |
| 83 | ); |
| 84 | |
| 85 | return $result->getIterator(); |
| 86 | } |
| 87 | |
| 88 | public function getMaxItems(): int |
| 89 | { |
| 90 | return $this->maxItems; |
| 91 | } |
| 92 | |
| 93 | public function setMaxItems(int $value): self |
| 94 | { |
| 95 | $this->maxItems = $value; |
| 96 | return $this; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @deprecated Use \oat\taoBackOffice\model\lists\Service\ListDeleter::delete() |
| 101 | * |
| 102 | * @return bool |
| 103 | */ |
| 104 | public function removeList(RdfClass $listClass) |
| 105 | { |
| 106 | try { |
| 107 | $this->getListDeleter()->delete($listClass); |
| 108 | |
| 109 | return true; |
| 110 | } catch (Throwable $exception) { |
| 111 | return false; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * @param string $label |
| 117 | * |
| 118 | * @return void |
| 119 | */ |
| 120 | public function createListElement(RdfClass $listClass, $label = '') |
| 121 | { |
| 122 | $valueCollection = new ValueCollection( |
| 123 | $listClass->getUri(), |
| 124 | new Value(null, $this->createUri(), $label) |
| 125 | ); |
| 126 | |
| 127 | $this->getValueService()->persist($valueCollection); |
| 128 | } |
| 129 | |
| 130 | public function isRemote(RdfClass $listClass): bool |
| 131 | { |
| 132 | return $this->getRemoteListClassSpecification()->isSatisfiedBy($listClass); |
| 133 | } |
| 134 | |
| 135 | private function createUri(): string |
| 136 | { |
| 137 | return $this->getServiceLocator()->get(UriProvider::class)->provide(); |
| 138 | } |
| 139 | |
| 140 | private function getValueService(): ValueCollectionService |
| 141 | { |
| 142 | return $this->getServiceLocator()->get(ValueCollectionService::class); |
| 143 | } |
| 144 | |
| 145 | private function getParentPropertyListCachedRepository(): ParentPropertyListCachedRepository |
| 146 | { |
| 147 | return $this->getServiceLocator()->get(ParentPropertyListCachedRepository::class); |
| 148 | } |
| 149 | |
| 150 | private function getRemoteListClassSpecification(): ClassSpecificationInterface |
| 151 | { |
| 152 | return $this->getContainer()->get(RemoteListClassSpecification::class); |
| 153 | } |
| 154 | |
| 155 | private function getEditableListClassSpecification(): ClassSpecificationInterface |
| 156 | { |
| 157 | return $this->getContainer()->get(EditableListClassSpecification::class); |
| 158 | } |
| 159 | |
| 160 | private function getListDeleter(): ListDeleterInterface |
| 161 | { |
| 162 | return $this->getContainer()->get(ListDeleter::class); |
| 163 | } |
| 164 | |
| 165 | private function getContainer(): ContainerInterface |
| 166 | { |
| 167 | return $this->getServiceLocator()->getContainer(); |
| 168 | } |
| 169 | } |