Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 31 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| RemoteSource | |
0.00% |
0 / 31 |
|
0.00% |
0 / 6 |
90 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| fetch | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| fetchByContext | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| getClient | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getParser | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| addParser | |
0.00% |
0 / 5 |
|
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) 2020-2021 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\Lists\Business\Service; |
| 24 | |
| 25 | use Traversable; |
| 26 | use RuntimeException; |
| 27 | use GuzzleHttp\Client; |
| 28 | use oat\tao\model\Context\ContextInterface; |
| 29 | use oat\oatbox\service\ConfigurableService; |
| 30 | use oat\tao\model\service\InjectionAwareService; |
| 31 | use oat\tao\model\featureFlag\FeatureFlagChecker; |
| 32 | use oat\tao\model\featureFlag\FeatureFlagCheckerInterface; |
| 33 | use oat\tao\model\Lists\Business\Domain\RemoteSourceContext; |
| 34 | |
| 35 | class RemoteSource extends InjectionAwareService |
| 36 | { |
| 37 | public const SERVICE_ID = 'tao/RemoteSource'; |
| 38 | |
| 39 | /** @var RemoteSourceParserInterface[] */ |
| 40 | private $parsers; |
| 41 | |
| 42 | /** @var Client|null */ |
| 43 | private $client; |
| 44 | |
| 45 | public function __construct(array $parsers, Client $client = null) |
| 46 | { |
| 47 | parent::__construct(); |
| 48 | |
| 49 | $this->parsers = $parsers; |
| 50 | $this->client = $client; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @deprecated Use $this->fetchByContext() |
| 55 | */ |
| 56 | public function fetch(string $sourceUrl, string $uriPath, string $labelPath, string $parser): Traversable |
| 57 | { |
| 58 | $context = new RemoteSourceContext([ |
| 59 | RemoteSourceContext::PARAM_SOURCE_URL => $sourceUrl, |
| 60 | RemoteSourceContext::PARAM_URI_PATH => $uriPath, |
| 61 | RemoteSourceContext::PARAM_LABEL_PATH => $labelPath, |
| 62 | RemoteSourceContext::PARAM_PARSER => $parser, |
| 63 | ]); |
| 64 | |
| 65 | yield from $this->fetchByContext($context); |
| 66 | } |
| 67 | |
| 68 | public function fetchByContext(ContextInterface $context): Traversable |
| 69 | { |
| 70 | $response = $this->getClient()->get($context->getParameter(RemoteSourceContext::PARAM_SOURCE_URL)); |
| 71 | $context->setParameter( |
| 72 | RemoteSourceContext::PARAM_JSON, |
| 73 | json_decode((string) $response->getBody(), true) |
| 74 | ); |
| 75 | |
| 76 | yield from $this |
| 77 | ->getParser($context->getParameter(RemoteSourceContext::PARAM_PARSER)) |
| 78 | ->iterateByContext($context); |
| 79 | } |
| 80 | |
| 81 | private function getClient(): Client |
| 82 | { |
| 83 | return $this->client ?? new Client(); |
| 84 | } |
| 85 | |
| 86 | private function getParser(string $key): RemoteSourceParserInterface |
| 87 | { |
| 88 | if (empty($this->parsers[$key])) { |
| 89 | throw new RuntimeException( |
| 90 | sprintf('No %s parsers defined', $key) |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | return $this->parsers[$key] instanceof ConfigurableService |
| 95 | ? $this->propagate($this->parsers[$key]) |
| 96 | : $this->parsers[$key]; |
| 97 | } |
| 98 | |
| 99 | public function addParser(string $key, RemoteSourceParserInterface $parser): void |
| 100 | { |
| 101 | if (isset($this->parsers[$key])) { |
| 102 | throw new RuntimeException( |
| 103 | sprintf('Parser %s is already registered', $key) |
| 104 | ); |
| 105 | } |
| 106 | $this->parsers[$key] = $parser; |
| 107 | } |
| 108 | } |