Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
40.91% |
18 / 44 |
|
25.00% |
2 / 8 |
CRAP | |
0.00% |
0 / 1 |
| ValueCollectionService | |
40.91% |
18 / 44 |
|
25.00% |
2 / 8 |
102.53 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| findAll | |
80.00% |
8 / 10 |
|
0.00% |
0 / 1 |
4.13 | |||
| delete | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| persist | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
30 | |||
| count | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
| setMaxItems | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setUserDataLanguage | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| getEventAggregator | |
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) 2020-2021 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | * @author Sergei Mikhailov <sergei.mikhailov@taotesting.com> |
| 21 | */ |
| 22 | |
| 23 | declare(strict_types=1); |
| 24 | |
| 25 | namespace oat\tao\model\Lists\Business\Service; |
| 26 | |
| 27 | use oat\oatbox\event\EventAggregator; |
| 28 | use oat\oatbox\session\SessionService; |
| 29 | use oat\oatbox\user\UserLanguageServiceInterface; |
| 30 | use oat\tao\model\Lists\Business\Contract\ValueCollectionRepositoryInterface; |
| 31 | use oat\tao\model\Lists\Business\Domain\ValueCollection; |
| 32 | use oat\tao\model\Lists\Business\Domain\ValueCollectionSearchRequest; |
| 33 | use oat\tao\model\Lists\Business\Event\ListSavedEvent; |
| 34 | use oat\tao\model\Lists\Business\Input\ValueCollectionDeleteInput; |
| 35 | use oat\tao\model\Lists\Business\Input\ValueCollectionSearchInput; |
| 36 | use oat\tao\model\Lists\DataAccess\Repository\ValueConflictException; |
| 37 | use oat\tao\model\service\InjectionAwareService; |
| 38 | use OverflowException; |
| 39 | |
| 40 | class ValueCollectionService extends InjectionAwareService |
| 41 | { |
| 42 | public const SERVICE_ID = 'tao/ValueCollectionService'; |
| 43 | |
| 44 | /** @var ValueCollectionRepositoryInterface */ |
| 45 | private $repositories; |
| 46 | |
| 47 | /** @var int */ |
| 48 | private $maxItems = 0; |
| 49 | |
| 50 | public function __construct(ValueCollectionRepositoryInterface ...$repositories) |
| 51 | { |
| 52 | parent::__construct(); |
| 53 | |
| 54 | $this->repositories = $repositories; |
| 55 | } |
| 56 | |
| 57 | public function findAll(ValueCollectionSearchInput $input): ValueCollection |
| 58 | { |
| 59 | $searchRequest = $input->getSearchRequest(); |
| 60 | |
| 61 | foreach ($this->repositories as $repository) { |
| 62 | if ( |
| 63 | $searchRequest->hasValueCollectionUri() |
| 64 | && !$repository->isApplicable($searchRequest->getValueCollectionUri()) |
| 65 | ) { |
| 66 | continue; |
| 67 | } |
| 68 | $this->setUserDataLanguage($searchRequest); |
| 69 | |
| 70 | return $repository->findAll( |
| 71 | $searchRequest |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | return new ValueCollection(); |
| 76 | } |
| 77 | |
| 78 | public function delete(ValueCollectionDeleteInput $input): void |
| 79 | { |
| 80 | foreach ($this->repositories as $repository) { |
| 81 | if ($repository->isApplicable($input->getValueCollectionUri())) { |
| 82 | $repository->delete($input->getValueCollectionUri()); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @param ValueCollection $valueCollection |
| 89 | * |
| 90 | * @return bool |
| 91 | * |
| 92 | * @throws ValueConflictException |
| 93 | * @throws OverflowException |
| 94 | */ |
| 95 | public function persist(ValueCollection $valueCollection): bool |
| 96 | { |
| 97 | if ($this->maxItems > 0 && $valueCollection->count() > $this->maxItems) { |
| 98 | throw new OverflowException("Collection exceeds the allowed number of items"); |
| 99 | } |
| 100 | |
| 101 | foreach ($this->repositories as $repository) { |
| 102 | if ($repository->isApplicable($valueCollection->getUri())) { |
| 103 | $this->getEventAggregator()->put( |
| 104 | $valueCollection->getUri(), |
| 105 | new ListSavedEvent($valueCollection->getUri()) |
| 106 | ); |
| 107 | |
| 108 | return $repository->persist($valueCollection); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | public function count(ValueCollectionSearchInput $input): int |
| 116 | { |
| 117 | $searchRequest = $input->getSearchRequest(); |
| 118 | |
| 119 | foreach ($this->repositories as $repository) { |
| 120 | if ( |
| 121 | $searchRequest->hasValueCollectionUri() |
| 122 | && !$repository->isApplicable($searchRequest->getValueCollectionUri()) |
| 123 | ) { |
| 124 | continue; |
| 125 | } |
| 126 | |
| 127 | return $repository->count( |
| 128 | $searchRequest |
| 129 | ); |
| 130 | } |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | public function setMaxItems(int $maxItems): void |
| 136 | { |
| 137 | $this->maxItems = $maxItems; |
| 138 | } |
| 139 | |
| 140 | private function setUserDataLanguage(ValueCollectionSearchRequest $searchRequest): void |
| 141 | { |
| 142 | /** @var SessionService $userSession */ |
| 143 | $userSession = $this->getServiceLocator()->get(SessionService::class); |
| 144 | $user = $userSession->getCurrentUser(); |
| 145 | |
| 146 | /** @var UserLanguageServiceInterface $userLanguageService */ |
| 147 | $userLanguageService = $this->getServiceLocator() |
| 148 | ->get(UserLanguageServiceInterface::SERVICE_ID); |
| 149 | |
| 150 | $searchRequest->setDataLanguage( |
| 151 | $userLanguageService->getDataLanguage($user) |
| 152 | ); |
| 153 | |
| 154 | $searchRequest->setDefaultLanguage($userLanguageService->getDefaultLanguage()); |
| 155 | } |
| 156 | |
| 157 | private function getEventAggregator(): EventAggregator |
| 158 | { |
| 159 | return $this->getServiceManager()->getContainer()->get(EventAggregator::SERVICE_ID); |
| 160 | } |
| 161 | } |