|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SkillDisplay\PHPToolKit\Api; |
| 6 | + |
| 7 | +/* |
| 8 | + * Copyright (C) 2023 Julian Zangl <julian.zangl@outlook.com> |
| 9 | + * |
| 10 | + * This program is free software; you can redistribute it and/or |
| 11 | + * modify it under the terms of the GNU General Public License |
| 12 | + * as published by the Free Software Foundation; either version 2 |
| 13 | + * of the License, or (at your option) any later version. |
| 14 | + * |
| 15 | + * This program is distributed in the hope that it will be useful, |
| 16 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | + * GNU General Public License for more details. |
| 19 | + * |
| 20 | + * You should have received a copy of the GNU General Public License |
| 21 | + * along with this program; if not, write to the Free Software |
| 22 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 23 | + * 02110-1301, USA. |
| 24 | + */ |
| 25 | + |
| 26 | +use GuzzleHttp\Client; |
| 27 | +use GuzzleHttp\Exception\ClientException; |
| 28 | +use GuzzleHttp\Psr7\Request; |
| 29 | +use SkillDisplay\PHPToolKit\Configuration\Settings; |
| 30 | +use SkillDisplay\PHPToolKit\Entity\SkillSetProgress as Entity; |
| 31 | + |
| 32 | +class SkillSetProgress |
| 33 | +{ |
| 34 | + protected Settings $settings; |
| 35 | + protected Client $client; |
| 36 | + |
| 37 | + public function __construct( |
| 38 | + Settings $settings, |
| 39 | + Client $client |
| 40 | + ) { |
| 41 | + $this->settings = $settings; |
| 42 | + $this->client = $client; |
| 43 | + } |
| 44 | + |
| 45 | + public function getById(int $id): Entity |
| 46 | + { |
| 47 | + if ($id <= 0) { |
| 48 | + throw new \Exception('ID of SkillSet has to be a positive integer.', 1688639724754); |
| 49 | + } |
| 50 | + |
| 51 | + $url = $this->settings->getAPIUrl() . '/api/v1/skillset/' . $id . '/progress'; |
| 52 | + try { |
| 53 | + $result = $this->client->send(new Request( |
| 54 | + 'GET', |
| 55 | + $url, |
| 56 | + [ |
| 57 | + 'Content-Type' => 'application/json', |
| 58 | + 'x-api-key' => $this->settings->getApiKey() |
| 59 | + ] |
| 60 | + )); |
| 61 | + } catch (ClientException $e) { |
| 62 | + if ($e->getCode() === 404) { |
| 63 | + throw new \InvalidArgumentException('Given SkillSet with id "' . $id . '" not available.', 1688639748718); |
| 64 | + } |
| 65 | + throw $e; |
| 66 | + } |
| 67 | + |
| 68 | + if ($result->getStatusCode() !== 200) { |
| 69 | + throw new \Exception('Did not get proper response for SkillSetProgress.', 1688639840720); |
| 70 | + } |
| 71 | + |
| 72 | + $body = (string) $result->getBody(); |
| 73 | + |
| 74 | + if (strpos($body, 'Oops, an error occurred') !== false) { |
| 75 | + throw new \Exception('Did not get proper response for SkillSetProgress. SkillSet with id "' . $id . '" does probably not exist.', 1688639873065); |
| 76 | + } |
| 77 | + |
| 78 | + return Entity::createFromJson($body, $this->settings); |
| 79 | + } |
| 80 | +} |
0 commit comments