Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
23 / 23 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| RegionService | |
100.00% |
23 / 23 |
|
100.00% |
4 / 4 |
12 | |
100.00% |
1 / 1 |
| getRegions | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getAvailablility | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
6 | |||
| getRegion | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| cacheRegions | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Vultr\VultrPhp\Services\Regions; |
| 6 | |
| 7 | use Exception; |
| 8 | use Vultr\VultrPhp\Services\VultrService; |
| 9 | use Vultr\VultrPhp\Util\ListOptions; |
| 10 | use Vultr\VultrPhp\Util\VultrUtil; |
| 11 | use Vultr\VultrPhp\VultrClientException; |
| 12 | |
| 13 | /** |
| 14 | * Region service handler, for all regions endpoints. |
| 15 | * |
| 16 | * @see https://www.vultr.com/api/#tag/region |
| 17 | */ |
| 18 | class RegionService extends VultrService |
| 19 | { |
| 20 | private static ?array $cache_region = null; |
| 21 | |
| 22 | /** |
| 23 | * List all regions at vultr |
| 24 | * |
| 25 | * @param $options - ListOptions - Interact via reference. |
| 26 | * @throws RegionException |
| 27 | * @return Region[] |
| 28 | */ |
| 29 | public function getRegions(?ListOptions &$options = null) : array |
| 30 | { |
| 31 | return $this->getListObjects('regions', new Region(), $options); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Get a list of the available plans in the region. |
| 36 | * |
| 37 | * @param $id - string - Ex ewr - Id of the region |
| 38 | * @param $type - string|null - PlanService Filters - FILTER_ALL, FILTER_VC2, FILTER_VHF, FILTER_VDC, FILTER_VBM |
| 39 | * @throws RegionException |
| 40 | * @return (VPSPlan|BMPlan)[] |
| 41 | */ |
| 42 | public function getAvailablility(string $id, ?string $type = null) : array |
| 43 | { |
| 44 | try |
| 45 | { |
| 46 | $params = []; |
| 47 | if ($type !== null) |
| 48 | { |
| 49 | $params['type'] = $type; |
| 50 | } |
| 51 | $response = $this->getClientHandler()->get('regions/'.$id.'/availability', $params); |
| 52 | } |
| 53 | catch (VultrClientException $e) |
| 54 | { |
| 55 | throw new RegionException('Failed to get available compute in region: '.$e->getMessage(), $e->getHTTPCode(), $e); |
| 56 | } |
| 57 | |
| 58 | $plans = []; |
| 59 | try |
| 60 | { |
| 61 | $decode = VultrUtil::decodeJSON((string)$response->getBody(), true); |
| 62 | |
| 63 | foreach ($decode['available_plans'] as $plan_id) |
| 64 | { |
| 65 | $plan = $this->getVultrClient()->plans->getPlan($plan_id); |
| 66 | if ($plan === null) continue; // Not valid plan. |
| 67 | $plans[] = $plan; |
| 68 | } |
| 69 | } |
| 70 | catch (Exception $e) |
| 71 | { |
| 72 | throw new RegionException('Failed to deserialize availability plan objects: '.$e->getMessage(), null, $e); |
| 73 | } |
| 74 | return $plans; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Get a specific region object based on the region id |
| 79 | * |
| 80 | * @param $id - string - Ex ewr - Region id. |
| 81 | * @throws RegionException |
| 82 | * @return Region|null |
| 83 | */ |
| 84 | public function getRegion(string $id) : ?Region |
| 85 | { |
| 86 | $this->cacheRegions(); |
| 87 | return static::$cache_region[$id] ?? null; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Cache all regions from the vultr api. |
| 92 | * |
| 93 | * @param $override - bool - Depending on whether to requery the regions. |
| 94 | * @throws RegionException |
| 95 | * @return void |
| 96 | */ |
| 97 | public function cacheRegions(bool $override = false) : void |
| 98 | { |
| 99 | if (static::$cache_region !== null && !$override) return; |
| 100 | |
| 101 | static::$cache_region = []; |
| 102 | $options = new ListOptions(500); |
| 103 | foreach ($this->getRegions($options) as $region) |
| 104 | { |
| 105 | static::$cache_region[$region->getId()] = $region; |
| 106 | } |
| 107 | } |
| 108 | } |