Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
83.93% |
47 / 56 |
|
87.50% |
7 / 8 |
CRAP | |
0.00% |
0 / 1 |
| LtiProviderService | |
83.93% |
47 / 56 |
|
87.50% |
7 / 8 |
15.93 | |
0.00% |
0 / 1 |
| count | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| searchByToolClientId | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| findAll | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| searchByLabel | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| aggregate | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| searchById | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 | |||
| searchByOauthKey | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
| searchByIssuer | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| 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 oat\generis\model\OntologyAwareTrait; |
| 26 | use oat\oatbox\service\ConfigurableService; |
| 27 | |
| 28 | /** |
| 29 | * Service methods to manage the LTI provider business objects. |
| 30 | */ |
| 31 | class LtiProviderService extends ConfigurableService implements LtiProviderRepositoryInterface |
| 32 | { |
| 33 | use OntologyAwareTrait; |
| 34 | |
| 35 | public const SERVICE_ID = 'taoLti/LtiProviderService'; |
| 36 | public const LTI_PROVIDER_LIST_IMPLEMENTATIONS = 'ltiProviderListImplementations'; |
| 37 | |
| 38 | /** |
| 39 | * Counts the number of LTI providers found from all implementations configured. |
| 40 | */ |
| 41 | public function count(): int |
| 42 | { |
| 43 | return $this->aggregate( |
| 44 | 0, |
| 45 | static function ($count, LtiProviderRepositoryInterface $implementation) { |
| 46 | return $count + $implementation->count(); |
| 47 | } |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | public function searchByToolClientId(string $clientId): ?LtiProvider |
| 52 | { |
| 53 | foreach ($this->findAll() as $provider) { |
| 54 | if ($clientId === $provider->getToolClientId()) { |
| 55 | return $provider; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return null; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Gathers LTI providers found from all implementations configured. |
| 64 | * |
| 65 | * @return LtiProvider[] |
| 66 | */ |
| 67 | public function findAll(): array |
| 68 | { |
| 69 | return $this->aggregate( |
| 70 | [], |
| 71 | static function ($providers, LtiProviderRepositoryInterface $implementation) { |
| 72 | return array_merge($providers, $implementation->findAll()); |
| 73 | } |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Gathers LTI providers found from all implementations configured and filters them by a string contained in label. |
| 79 | * |
| 80 | * @return LtiProvider[] |
| 81 | */ |
| 82 | public function searchByLabel(string $label): array |
| 83 | { |
| 84 | return $this->aggregate( |
| 85 | [], |
| 86 | static function ($providers, LtiProviderRepositoryInterface $implementation) use ($label) { |
| 87 | return array_merge($providers, $implementation->searchByLabel($label)); |
| 88 | } |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Aggregates results of each implementation. |
| 94 | * |
| 95 | * @param array|int $result |
| 96 | * @param callable $method |
| 97 | * |
| 98 | * @return array|int |
| 99 | */ |
| 100 | private function aggregate($result, $method) |
| 101 | { |
| 102 | foreach ($this->getOption(self::LTI_PROVIDER_LIST_IMPLEMENTATIONS) as $implementation) { |
| 103 | if (is_array($implementation)) { |
| 104 | $implementation = $this->buildService($implementation); |
| 105 | } |
| 106 | $this->propagate($implementation); |
| 107 | $result = $method($result, $implementation); |
| 108 | } |
| 109 | |
| 110 | return $result; |
| 111 | } |
| 112 | |
| 113 | public function searchById(string $id): ?LtiProvider |
| 114 | { |
| 115 | return current( |
| 116 | array_filter( |
| 117 | $this->aggregate( |
| 118 | [], |
| 119 | static function ($providers, LtiProviderRepositoryInterface $implementation) use ($id) { |
| 120 | return array_merge($providers, [$implementation->searchById($id)]); |
| 121 | } |
| 122 | ) |
| 123 | ) |
| 124 | ) ?: null; |
| 125 | } |
| 126 | |
| 127 | public function searchByOauthKey(string $oauthKey): ?LtiProvider |
| 128 | { |
| 129 | $found = array_filter($this->aggregate( |
| 130 | [], |
| 131 | static function ($providers, LtiProviderRepositoryInterface $implementation) use ($oauthKey) { |
| 132 | return array_merge($providers, [$implementation->searchByOauthKey($oauthKey)]); |
| 133 | } |
| 134 | )); |
| 135 | return count($found) > 0 |
| 136 | ? reset($found) |
| 137 | : null; |
| 138 | } |
| 139 | |
| 140 | public function searchByIssuer(string $issuer, ?string $clientId = null): ?LtiProvider |
| 141 | { |
| 142 | $found = array_filter($this->aggregate( |
| 143 | [], |
| 144 | static function ($providers, LtiProviderRepositoryInterface $implementation) use ($issuer, $clientId) { |
| 145 | return array_merge($providers, [$implementation->searchByIssuer($issuer, $clientId)]); |
| 146 | } |
| 147 | )); |
| 148 | return count($found) > 0 |
| 149 | ? reset($found) |
| 150 | : null; |
| 151 | } |
| 152 | } |