Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 14 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| TaskLanguageLoader | |
0.00% |
0 / 14 |
|
0.00% |
0 / 2 |
12 | |
0.00% |
0 / 1 |
| loadTranslations | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| getTaskExtension | |
0.00% |
0 / 6 |
|
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) 2022 Open Assessment Technologies SA |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | declare(strict_types=1); |
| 23 | |
| 24 | namespace oat\tao\model\taskQueue\Task; |
| 25 | |
| 26 | use common_ext_Extension; |
| 27 | use common_ext_ExtensionException; |
| 28 | use common_ext_ExtensionsManager; |
| 29 | use oat\oatbox\log\LoggerService; |
| 30 | use oat\oatbox\service\ConfigurableService; |
| 31 | use oat\oatbox\user\UserLanguageServiceInterface; |
| 32 | use ReflectionClass; |
| 33 | use tao_helpers_I18n; |
| 34 | use Throwable; |
| 35 | |
| 36 | class TaskLanguageLoader extends ConfigurableService implements TaskLanguageLoaderInterface |
| 37 | { |
| 38 | public function loadTranslations(TaskInterface $task): void |
| 39 | { |
| 40 | try { |
| 41 | tao_helpers_I18n::init( |
| 42 | $extension = $this->getTaskExtension($task), |
| 43 | $this->getServiceLocator()->get(UserLanguageServiceInterface::SERVICE_ID)->getDefaultLanguage() |
| 44 | ); |
| 45 | |
| 46 | $message = sprintf('Translations loaded for extension "%s"', $extension->getId()); |
| 47 | } catch (Throwable $e) { |
| 48 | $message = sprintf('Unable to load translations for task "%s".', $task->getId()); |
| 49 | } |
| 50 | |
| 51 | $this->getServiceLocator()->get(LoggerService::SERVICE_ID)->notice($message); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @throws common_ext_ExtensionException |
| 56 | */ |
| 57 | private function getTaskExtension(TaskInterface $task): common_ext_Extension |
| 58 | { |
| 59 | $reflectionClass = new ReflectionClass($task); |
| 60 | $namespace = $reflectionClass->getNamespaceName(); |
| 61 | $extensionId = explode('\\', $namespace)[1] ?? null; |
| 62 | |
| 63 | return $this->getServiceLocator() |
| 64 | ->get(common_ext_ExtensionsManager::SERVICE_ID) |
| 65 | ->getExtensionById($extensionId); |
| 66 | } |
| 67 | } |