Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.74% covered (success)
95.74%
45 / 47
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
PlanService
95.74% covered (success)
95.74%
45 / 47
80.00% covered (warning)
80.00%
4 / 5
22
0.00% covered (danger)
0.00%
0 / 1
 getVPSPlans
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
5
 getBMPlans
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 getPlan
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 cachePlans
87.50% covered (warning)
87.50%
14 / 16
0.00% covered (danger)
0.00%
0 / 1
9.16
 setPlanLocations
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3declare(strict_types=1);
4
5namespace Vultr\VultrPhp\Services\Plans;
6
7use Vultr\VultrPhp\Services\VultrService;
8use Vultr\VultrPhp\Services\VultrServiceException;
9use Vultr\VultrPhp\Util\ListOptions;
10
11/**
12 * Plan service handler, for all plans endpoints.
13 *
14 * @see https://www.vultr.com/api/#tag/plans
15 */
16class PlanService extends VultrService
17{
18    /**
19     * @see https://www.vultr.com/api/#tag/plans/operation/list-plans
20     */
21    public const FILTER_ALL = 'all'; // All available types
22    public const FILTER_VC2 = 'vc2'; // Cloud Compute
23    public const FILTER_VHF = 'vhf'; // High Frequency Compute
24    public const FILTER_VDC = 'vdc'; // Dedicated Cloud.
25    public const FILTER_VOC = 'voc'; // All Optimized Cloud Types
26    public const FILTER_VOCG = 'voc-g'; // General Purpose Optimized Cloud
27    public const FILTER_VOCC = 'voc-c'; // CPU Optimized Cloud
28    public const FILTER_VOCM = 'voc-m'; // Memory Optimized Cloud
29    public const FILTER_VOCS = 'voc-s'; // Storage Optimized Cloud
30    public const FILTER_VCG = 'vcg'; // Cloud gpu
31
32    public const FILTER_VBM = 'vbm';
33
34    public const FILTER_WINDOWS = 'windows';
35
36    private static ?array $cache_plans = null;
37
38    /**
39     * Get vps plans that are available at vultr.
40     *
41     * @see https://www.vultr.com/api/#operation/list-plans
42     * @param $type - string|null - FILTER_*
43     * @param $os - string|null - FILTER_WINDOWS
44     * @param $options - ListOptions - Interact via reference.
45     * @throws PlanException
46     * @return VPSPlan[]
47     */
48    public function getVPSPlans(?string $type = null, ?string $os = null, ?ListOptions &$options = null) : array
49    {
50        $plans = [];
51        if ($options === null)
52        {
53            $options = new ListOptions(100);
54        }
55
56        $params = [];
57
58        if ($type !== null)
59        {
60            $params['type'] = $type;
61        }
62
63        if ($os !== null)
64        {
65            $params['os'] = $os;
66        }
67
68        try
69        {
70            $plans = $this->list('plans', new VPSPlan(), $options, $params);
71            $this->setPlanLocations($plans);
72        }
73        catch (VultrServiceException $e)
74        {
75            throw new PlanException('Failed to get vps plans: '.$e->getMessage(), $e->getHTTPCode(), $e);
76        }
77
78        return $plans;
79    }
80
81    /**
82     * Get baremetal plans that are available at vultr.
83     *
84     * @see https://www.vultr.com/api/#operation/list-metal-plans
85     * @param $options - ListOptions - Interact via reference.
86     * @throws PlanException
87     * @return BMPlan[]
88     */
89    public function getBMPlans(?ListOptions &$options = null) : array
90    {
91        $plans = [];
92        if ($options === null)
93        {
94            $options = new ListOptions(100);
95        }
96
97        try
98        {
99            $plans = $this->list('plans-metal', new BMPlan(), $options);
100            $this->setPlanLocations($plans);
101        }
102        catch (VultrServiceException $e)
103        {
104            throw new PlanException('Failed to get baremetal plans: '.$e->getMessage(), $e->getHTTPCode(), $e);
105        }
106        return $plans;
107    }
108
109    /**
110     * Get a specific plan vps or baremetal plan from the static cache.
111     *
112     * @param $id - string - Ex vc2-1c-1gb - This can be a vps plan id or a baremetal plan id.
113     * @throws PlanException
114     * @return VPSPlan|BMPlan|null
115     */
116    public function getPlan(string $id) : VPSPlan|BMPlan|null
117    {
118        $this->cachePlans();
119        return static::$cache_plans[$id] ?? null;
120    }
121
122    /**
123     * Query and cache all plans from the vultr api.
124     *
125     * @param $override - bool - Depending on whether to requery the plans.
126     * @throws PlanException
127     * @return void
128     */
129    public function cachePlans(bool $override = false) : void
130    {
131        if (static::$cache_plans !== null && !$override) return;
132
133        static::$cache_plans = [];
134        $options = new ListOptions(500);
135        while (true)
136        {
137            foreach ($this->getVPSPlans(null, null, $options) as $plan)
138            {
139                static::$cache_plans[$plan->getId()] = $plan;
140            }
141
142            if ($options->getNextCursor() == '')
143            {
144                break;
145            }
146            $options->setCurrentCursor($options->getNextCursor());
147        }
148
149        $options = new ListOptions(500);
150        while (true)
151        {
152            foreach ($this->getBMPlans($options) as $plan)
153            {
154                static::$cache_plans[$plan->getId()] = $plan;
155            }
156
157            if ($options->getNextCursor() == '')
158            {
159                break;
160            }
161            $options->setCurrentCursor($options->getNextCursor());
162        }
163    }
164
165    private function setPlanLocations(array &$plans) : void
166    {
167        $this->getVultrClient()->regions->cacheRegions();
168        foreach ($plans as $plan)
169        {
170            $regions = [];
171            foreach ($plan->getLocations() as $id)
172            {
173                $region = $this->getVultrClient()->regions->getRegion($id);
174
175                if ($region === null) continue; // Cache failure?
176
177                $regions[] = $region;
178            }
179            $plan->setLocations($regions);
180        }
181    }
182}