Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
88.89% |
8 / 9 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ResourceLanguageRetriever | |
88.89% |
8 / 9 |
|
66.67% |
2 / 3 |
5.03 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setRetriever | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| retrieve | |
100.00% |
5 / 5 |
|
100.00% |
1 / 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) 2024 (original work) Open Assessment Technologies SA. |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\Translation\Service; |
| 24 | |
| 25 | use core_kernel_classes_Resource; |
| 26 | use InvalidArgumentException; |
| 27 | use oat\oatbox\user\UserLanguageServiceInterface; |
| 28 | |
| 29 | class ResourceLanguageRetriever |
| 30 | { |
| 31 | private UserLanguageServiceInterface $userLanguageService; |
| 32 | |
| 33 | private array $retrievers = []; |
| 34 | |
| 35 | public function __construct(UserLanguageServiceInterface $userLanguageService) |
| 36 | { |
| 37 | $this->userLanguageService = $userLanguageService; |
| 38 | } |
| 39 | |
| 40 | public function setRetriever(string $resourceType, callable $retriever): void |
| 41 | { |
| 42 | if (array_key_exists($resourceType, $this->retrievers)) { |
| 43 | throw new InvalidArgumentException(sprintf('Retriever for resource type %s already set.', $resourceType)); |
| 44 | } |
| 45 | |
| 46 | $this->retrievers[$resourceType] = $retriever; |
| 47 | } |
| 48 | |
| 49 | public function retrieve(core_kernel_classes_Resource $resource): string |
| 50 | { |
| 51 | $resourceType = $resource->getRootId(); |
| 52 | |
| 53 | $language = isset($this->retrievers[$resourceType]) |
| 54 | ? $this->retrievers[$resourceType]($resource) |
| 55 | : null; |
| 56 | |
| 57 | return $language ?? $this->userLanguageService->getDefaultLanguage(); |
| 58 | } |
| 59 | } |