Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
80.00% |
16 / 20 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| LtiConfigProvider | |
80.00% |
16 / 20 |
|
75.00% |
3 / 4 |
10.80 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| getConfigByName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasConfig | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getConfigByLtiClaimName | |
73.33% |
11 / 15 |
|
0.00% |
0 / 1 |
7.93 | |||
| 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\taoLti\models\classes\DynamicConfig; |
| 24 | |
| 25 | use oat\oatbox\session\SessionService; |
| 26 | use oat\tao\model\DynamicConfig\DynamicConfigProviderInterface; |
| 27 | use oat\taoLti\models\classes\LtiLaunchData; |
| 28 | use oat\taoLti\models\classes\TaoLtiSession; |
| 29 | use Psr\Log\LoggerInterface; |
| 30 | use Throwable; |
| 31 | |
| 32 | class LtiConfigProvider implements DynamicConfigProviderInterface |
| 33 | { |
| 34 | private DynamicConfigProviderInterface $configFallback; |
| 35 | |
| 36 | private SessionService $session; |
| 37 | private LoggerInterface $logger; |
| 38 | |
| 39 | public function __construct( |
| 40 | DynamicConfigProviderInterface $configFallback, |
| 41 | SessionService $session, |
| 42 | LoggerInterface $logger |
| 43 | ) { |
| 44 | $this->configFallback = $configFallback; |
| 45 | $this->session = $session; |
| 46 | $this->logger = $logger; |
| 47 | } |
| 48 | |
| 49 | public function getConfigByName(string $name): ?string |
| 50 | { |
| 51 | return $this->getConfigByLtiClaimName($name) ?? $this->configFallback->getConfigByName($name); |
| 52 | } |
| 53 | |
| 54 | public function hasConfig(string $name): bool |
| 55 | { |
| 56 | return $this->getConfigByName($name) !== null; |
| 57 | } |
| 58 | |
| 59 | private function getConfigByLtiClaimName(string $name): ?string |
| 60 | { |
| 61 | $currentSession = $this->session->getCurrentSession(); |
| 62 | |
| 63 | if (!$currentSession instanceof TaoLtiSession) { |
| 64 | return null; |
| 65 | } |
| 66 | |
| 67 | $ltiLaunchData = $currentSession->getLaunchData(); |
| 68 | |
| 69 | if ($name === self::LOGIN_URL_CONFIG_NAME) { |
| 70 | return $ltiLaunchData->getCustomParameter(LtiLaunchData::LTI_TAO_LOGIN_URL); |
| 71 | } |
| 72 | if ($name === self::LOGOUT_URL_CONFIG_NAME) { |
| 73 | return $ltiLaunchData->getCustomParameter(LtiLaunchData::LTI_REDIRECT_AFTER_LOGOUT_URL); |
| 74 | } |
| 75 | |
| 76 | try { |
| 77 | if ($name === self::PLATFORM_URL_CONFIG_NAME && $ltiLaunchData->hasReturnUrl()) { |
| 78 | return $ltiLaunchData->getReturnUrl(); |
| 79 | } |
| 80 | } catch (Throwable $exception) { |
| 81 | $this->logger->warning( |
| 82 | sprintf('It was not possible to recover return url claim. Exception: %s', $exception) |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | return null; |
| 87 | } |
| 88 | } |