Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
76.92% |
10 / 13 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| WebhookSender | |
76.92% |
10 / 13 |
|
33.33% |
1 / 3 |
7.60 | |
0.00% |
0 / 1 |
| performRequest | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| preformRequestWithoutAuth | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| performRequestWithAuth | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
4.03 | |||
| 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 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | namespace oat\tao\model\webhooks\task; |
| 22 | |
| 23 | use GuzzleHttp\Client; |
| 24 | use GuzzleHttp\Exception\ClientException; |
| 25 | use GuzzleHttp\Exception\GuzzleException; |
| 26 | use oat\oatbox\service\ConfigurableService; |
| 27 | use oat\tao\model\auth\AbstractAuthType; |
| 28 | use oat\tao\model\webhooks\configEntity\WebhookAuthInterface; |
| 29 | use Psr\Http\Message\RequestInterface; |
| 30 | use Psr\Http\Message\ResponseInterface; |
| 31 | use Zend\ServiceManager\ServiceLocatorAwareInterface; |
| 32 | |
| 33 | class WebhookSender extends ConfigurableService |
| 34 | { |
| 35 | /** |
| 36 | * @param RequestInterface $request |
| 37 | * @param WebhookAuthInterface|null $authConfig |
| 38 | * @return ResponseInterface |
| 39 | * @throws GuzzleException |
| 40 | * @throws ClientException |
| 41 | * @throws \InvalidArgumentException |
| 42 | * @throws \common_exception_InvalidArgumentType |
| 43 | */ |
| 44 | public function performRequest(RequestInterface $request, WebhookAuthInterface $authConfig = null) |
| 45 | { |
| 46 | return $authConfig |
| 47 | ? $this->performRequestWithAuth($request, $authConfig) |
| 48 | : $this->preformRequestWithoutAuth($request); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @param RequestInterface $request |
| 53 | * @return ResponseInterface |
| 54 | * @throws GuzzleException |
| 55 | */ |
| 56 | private function preformRequestWithoutAuth(RequestInterface $request) |
| 57 | { |
| 58 | $client = new Client(); |
| 59 | return $client->send($request); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @param RequestInterface $request |
| 64 | * @param WebhookAuthInterface $authConfig |
| 65 | * @return ResponseInterface |
| 66 | * @throws \common_exception_InvalidArgumentType |
| 67 | */ |
| 68 | private function performRequestWithAuth(RequestInterface $request, WebhookAuthInterface $authConfig) |
| 69 | { |
| 70 | $authClass = $authConfig->getAuthClass(); |
| 71 | if (!class_exists($authClass) || !is_subclass_of($authClass, AbstractAuthType::class)) { |
| 72 | throw new \InvalidArgumentException("Auth class '$authClass' is not " . AbstractAuthType::class); |
| 73 | } |
| 74 | |
| 75 | /** @var AbstractAuthType $authImpl */ |
| 76 | $authImpl = new $authClass(); |
| 77 | if ($authImpl instanceof ServiceLocatorAwareInterface) { |
| 78 | $authImpl->setServiceLocator($this->getServiceLocator()); |
| 79 | } |
| 80 | $authImpl->setCredentials($authConfig->getCredentials()); |
| 81 | |
| 82 | return $authImpl->call($request); |
| 83 | } |
| 84 | } |