Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 31 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| ConfigurableLtiProviderRepository | |
0.00% |
0 / 31 |
|
0.00% |
0 / 8 |
380 | |
0.00% |
0 / 1 |
| count | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| findAll | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| searchByLabel | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| getProviders | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| searchById | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| searchByOauthKey | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| searchByIssuer | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
30 | |||
| getLtiProviderFactory | |
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) 2019-2020 (original work) Open Assessment Technologies SA |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\taoLti\models\classes\LtiProvider; |
| 24 | |
| 25 | use InvalidArgumentException; |
| 26 | use oat\oatbox\service\ConfigurableService; |
| 27 | |
| 28 | /** |
| 29 | * Service methods to manage the LTI provider business objects. |
| 30 | */ |
| 31 | class ConfigurableLtiProviderRepository extends ConfigurableService implements LtiProviderRepositoryInterface |
| 32 | { |
| 33 | public const OPTION_LTI_PROVIDER_LIST = 'OPTION_LTI_PROVIDER_LIST'; |
| 34 | |
| 35 | public const LTI_VERSION = 'ltiVersion'; |
| 36 | public const LTI_TOOL_CLIENT_ID = 'toolClientId'; |
| 37 | public const LTI_TOOL_IDENTIFIER = 'toolIdentifier'; |
| 38 | public const LTI_TOOL_NAME = 'toolName'; |
| 39 | public const LTI_TOOL_DEPLOYMENT_IDS = 'toolDeploymentIds'; |
| 40 | public const LTI_TOOL_AUDIENCE = 'toolAudience'; |
| 41 | public const LTI_TOOL_OIDC_LOGIN_INITATION_URL = 'toolOidcLoginInitiationUrl'; |
| 42 | public const LTI_TOOL_LAUNCH_URL = 'toolLaunchUrl'; |
| 43 | public const LTI_TOOL_JWKS_URL = 'toolJwksUrl'; |
| 44 | public const LTI_TOOL_PUBLIC_KEY = 'toolPublicKey'; |
| 45 | |
| 46 | /** |
| 47 | * @var LtiProvider[] |
| 48 | */ |
| 49 | private $providers; |
| 50 | |
| 51 | public function count() |
| 52 | { |
| 53 | return count($this->getProviders()); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @inheritdoc |
| 58 | */ |
| 59 | public function findAll(): array |
| 60 | { |
| 61 | return $this->getProviders(); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @inheritdoc |
| 66 | */ |
| 67 | public function searchByLabel(string $label): array |
| 68 | { |
| 69 | return array_filter( |
| 70 | $this->getProviders(), |
| 71 | static function (LtiProvider $provider) use ($label) { |
| 72 | return stripos($provider->getLabel(), $label) !== false; |
| 73 | } |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Get providers from configuration. |
| 79 | * |
| 80 | * @return LtiProvider[] |
| 81 | */ |
| 82 | private function getProviders(): array |
| 83 | { |
| 84 | |
| 85 | if ($this->providers === null) { |
| 86 | $providerList = $this->getOption(self::OPTION_LTI_PROVIDER_LIST); |
| 87 | if ($providerList === null) { |
| 88 | throw new InvalidArgumentException('LTI provider list is not valid.'); |
| 89 | } |
| 90 | |
| 91 | $this->providers = []; |
| 92 | |
| 93 | foreach ($providerList as $provider) { |
| 94 | $this->providers[] = $this->getLtiProviderFactory()->createFromArray($provider); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return $this->providers; |
| 99 | } |
| 100 | |
| 101 | public function searchById(string $id): ?LtiProvider |
| 102 | { |
| 103 | foreach ($this->getProviders() as $provider) { |
| 104 | if ($provider->getId() === $id) { |
| 105 | return $provider; |
| 106 | } |
| 107 | } |
| 108 | return null; |
| 109 | } |
| 110 | |
| 111 | public function searchByOauthKey(string $oauthKey): ?LtiProvider |
| 112 | { |
| 113 | foreach ($this->getProviders() as $provider) { |
| 114 | if ($provider->getKey() === $oauthKey) { |
| 115 | return $provider; |
| 116 | } |
| 117 | } |
| 118 | return null; |
| 119 | } |
| 120 | |
| 121 | public function searchByIssuer(string $issuer, ?string $clientId = null): ?LtiProvider |
| 122 | { |
| 123 | foreach ($this->getProviders() as $provider) { |
| 124 | if ($clientId !== null && $provider->getToolClientId() !== $clientId) { |
| 125 | continue; |
| 126 | } |
| 127 | if ($provider->getToolAudience() === $issuer) { |
| 128 | return $provider; |
| 129 | } |
| 130 | } |
| 131 | return null; |
| 132 | } |
| 133 | |
| 134 | private function getLtiProviderFactory(): LtiProviderFactory |
| 135 | { |
| 136 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
| 137 | return $this->getServiceLocator()->get(LtiProviderFactory::class); |
| 138 | } |
| 139 | } |