Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.34% |
50 / 53 |
|
90.00% |
9 / 10 |
CRAP | |
0.00% |
0 / 1 |
| VultrService | |
94.34% |
50 / 53 |
|
90.00% |
9 / 10 |
22.09 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getVultrClient | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getClientHandler | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getObject | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| getListObjects | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| patchObject | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| deleteObject | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| createObject | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| list | |
84.21% |
16 / 19 |
|
0.00% |
0 / 1 |
6.14 | |||
| getReadableClassType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Vultr\VultrPhp\Services; |
| 6 | |
| 7 | use Throwable; |
| 8 | use Vultr\VultrPhp\Util\ListOptions; |
| 9 | use Vultr\VultrPhp\Util\ModelInterface; |
| 10 | use Vultr\VultrPhp\Util\VultrUtil; |
| 11 | use Vultr\VultrPhp\VultrClient; |
| 12 | use Vultr\VultrPhp\VultrClientException; |
| 13 | use Vultr\VultrPhp\VultrClientHandler; |
| 14 | use Vultr\VultrPhp\VultrException; |
| 15 | |
| 16 | abstract class VultrService |
| 17 | { |
| 18 | protected VultrClient $vultr; |
| 19 | private VultrClientHandler $client; |
| 20 | |
| 21 | public function __construct(VultrClient $vultr, VultrClientHandler $client) |
| 22 | { |
| 23 | $this->vultr = $vultr; |
| 24 | $this->client = $client; |
| 25 | } |
| 26 | |
| 27 | protected function getVultrClient() : VultrClient |
| 28 | { |
| 29 | return $this->vultr; |
| 30 | } |
| 31 | |
| 32 | protected function getClientHandler() : VultrClientHandler |
| 33 | { |
| 34 | return $this->client; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @param $uri - string - the url address to query after api.vultr.com/v2 |
| 39 | * @param $model - ModelInterface - the object that will be mapped to the get response. |
| 40 | * @throws Child of VultrServiceObject |
| 41 | * @return ModelInterface |
| 42 | */ |
| 43 | protected function getObject(string $uri, ModelInterface $model) : ModelInterface |
| 44 | { |
| 45 | try |
| 46 | { |
| 47 | $response = $this->getClientHandler()->get($uri); |
| 48 | } |
| 49 | catch (VultrClientException $e) |
| 50 | { |
| 51 | $exception_class = $model->getModelExceptionClass(); |
| 52 | throw new $exception_class('Failed to get '.$this->getReadableClassType($model). ' info: '.$e->getMessage(), $e->getHTTPCode(), $e); |
| 53 | } |
| 54 | |
| 55 | return VultrUtil::convertJSONToObject((string)$response->getBody(), clone $model, $model->getResponseName()); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @param $uri - string - the url address to query after api.vultr.com/v2 |
| 60 | * @param $model - ModelInterface - the object that will be mapped to the get response. |
| 61 | * @param $options - ListOptions - Pagination object |
| 62 | * @param $params - array - filter parameters. |
| 63 | * @throws Child of VultrServiceObject |
| 64 | * @return ModelInterface[] |
| 65 | */ |
| 66 | protected function getListObjects(string $uri, ModelInterface $model, ?ListOptions &$options = null, ?array $params = null) : array |
| 67 | { |
| 68 | if ($options === null) |
| 69 | { |
| 70 | $options = new ListOptions(100); |
| 71 | } |
| 72 | |
| 73 | $objects = []; |
| 74 | try |
| 75 | { |
| 76 | $objects = $this->list($uri, clone $model, $options, $params); |
| 77 | } |
| 78 | catch (VultrServiceException $e) |
| 79 | { |
| 80 | $exception_class = $model->getModelExceptionClass(); |
| 81 | throw new $exception_class('Failed to list '.$model->getResponseListName().': '.$e->getMessage(), $e->getHTTPCode(), $e); |
| 82 | } |
| 83 | |
| 84 | return $objects; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @param $uri - string - the url address to query after api.vultr.com/v2 |
| 89 | * @param $model - ModelInterface - the object model that we are updating. This needs to be a fully initialized object. |
| 90 | * @throws Child of VultrServiceObject |
| 91 | * @return void |
| 92 | */ |
| 93 | protected function patchObject(string $uri, ModelInterface $model, ?array $params = null) : void |
| 94 | { |
| 95 | $payload = $model->getUpdateArray(); |
| 96 | if ($params !== null) |
| 97 | { |
| 98 | $payload = $params; |
| 99 | } |
| 100 | |
| 101 | try |
| 102 | { |
| 103 | $this->getClientHandler()->patch($uri, $payload); |
| 104 | } |
| 105 | catch (VultrClientException $e) |
| 106 | { |
| 107 | $exception_class = $model->getModelExceptionClass(); |
| 108 | throw new $exception_class('Failed to update '.$this->getReadableClassType($model).': '.$e->getMessage(), $e->getHTTPCode(), $e); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @param $uri - string - the url address to query after api.vultr.com/v2 |
| 114 | * @param $model - ModelInterface - the object model that we are acting on deleting. This doesn't need to be a fully initialized object. |
| 115 | * @throws Child of VultrServiceObject |
| 116 | * @return void |
| 117 | */ |
| 118 | protected function deleteObject(string $uri, ModelInterface $model) : void |
| 119 | { |
| 120 | try |
| 121 | { |
| 122 | $this->getClientHandler()->delete($uri); |
| 123 | } |
| 124 | catch (VultrClientException $e) |
| 125 | { |
| 126 | $exception_class = $model->getModelExceptionClass(); |
| 127 | throw new $exception_class('Failed to delete '.$this->getReadableClassType($model).': '.$e->getMessage(), $e->getHTTPCode(), $e); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @param $uri - string - the url address to query after api.vultr.com/v2 |
| 133 | * @param $model - ModelInterface - the object model that we are creating |
| 134 | * @param $params - array - The values that we will be sending. Refactor to use getUpdateParams/getUpdateArray? |
| 135 | * @throws Child of VultrServiceObject |
| 136 | * @return ModelInterface |
| 137 | */ |
| 138 | protected function createObject(string $uri, ModelInterface $model, array $params) : ModelInterface |
| 139 | { |
| 140 | try |
| 141 | { |
| 142 | $response = $this->getClientHandler()->post($uri, $params); |
| 143 | } |
| 144 | catch (VultrClientException $e) |
| 145 | { |
| 146 | $exception_class = $model->getModelExceptionClass(); |
| 147 | throw new $exception_class('Failed to create '.$this->getReadableClassType($model).': '.$e->getMessage(), $e->getHTTPCode(), $e); |
| 148 | } |
| 149 | |
| 150 | return VultrUtil::convertJSONToObject((string)$response->getBody(), clone $model, $model->getResponseName()); |
| 151 | } |
| 152 | |
| 153 | protected function list(string $uri, ModelInterface $model, ListOptions &$options, ?array $params = null) : array |
| 154 | { |
| 155 | try |
| 156 | { |
| 157 | if ($params === null) |
| 158 | { |
| 159 | $params = []; |
| 160 | } |
| 161 | $params['per_page'] = $options->getPerPage(); |
| 162 | |
| 163 | if ($options->getCurrentCursor() != '') |
| 164 | { |
| 165 | $params['cursor'] = $options->getCurrentCursor(); |
| 166 | } |
| 167 | |
| 168 | $response = $this->getClientHandler()->get($uri, $params); |
| 169 | } |
| 170 | catch (VultrClientException $e) |
| 171 | { |
| 172 | throw new VultrServiceException('Failed to list: '.$e->getMessage(), VultrException::SERVICE_CODE, $e->getHTTPCode(), $e); |
| 173 | } |
| 174 | |
| 175 | $objects = []; |
| 176 | try |
| 177 | { |
| 178 | $stdclass = VultrUtil::decodeJSON((string)$response->getBody()); |
| 179 | $options->setTotal($stdclass->meta->total); |
| 180 | $options->setNextCursor($stdclass->meta->links->next); |
| 181 | $options->setPrevCursor($stdclass->meta->links->prev); |
| 182 | $list_name = $model->getResponseListName(); |
| 183 | foreach ($stdclass->$list_name as $object) |
| 184 | { |
| 185 | $objects[] = VultrUtil::mapObject($object, $model); |
| 186 | } |
| 187 | } |
| 188 | catch (Throwable $e) |
| 189 | { |
| 190 | throw new VultrServiceException('Failed to deserialize list: '. $e->getMessage(), VultrException::SERVICE_CODE, null, $e); |
| 191 | } |
| 192 | |
| 193 | return $objects; |
| 194 | } |
| 195 | |
| 196 | private function getReadableClassType(ModelInterface $model) : string |
| 197 | { |
| 198 | return str_replace('_', ' ', $model->getResponseName()); |
| 199 | } |
| 200 | } |