Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.67% covered (success)
91.67%
11 / 12
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
OperatingSystemService
91.67% covered (success)
91.67%
11 / 12
66.67% covered (warning)
66.67%
2 / 3
8.04
0.00% covered (danger)
0.00%
0 / 1
 getOperatingSystems
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getOperatingSystem
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 cacheOperatingSystems
88.89% covered (warning)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
6.05
1<?php
2
3declare(strict_types=1);
4
5namespace Vultr\VultrPhp\Services\OperatingSystems;
6
7use Vultr\VultrPhp\Services\VultrService;
8use Vultr\VultrPhp\Util\ListOptions;
9
10/**
11 * Operating system service handler, for all os endpoints.
12 *
13 * @see https://www.vultr.com/api/#tag/os
14 */
15class OperatingSystemService extends VultrService
16{
17    private static ?array $cache_operatingsystems = null;
18
19    /**
20     * Get all operating systems that are available to be deployed.
21     *
22     * @param $options - ListOptions - Interact via reference.
23     * @throws OperatingSystemException
24     * @return OperatingSystem[]
25     */
26    public function getOperatingSystems(?ListOptions &$options = null) : array
27    {
28        return $this->getListObjects('os', new OperatingSystem(), $options);
29    }
30
31    /**
32     * Get a specific operating system that is deployable.
33     *
34     * @param $id - string - Ex 124 - OS id.
35     * @throws OperatingSystemException
36     * @return OperatingSystem|null
37     */
38    public function getOperatingSystem(int $id) : ?OperatingSystem
39    {
40        $this->cacheOperatingSystems();
41        return static::$cache_operatingsystems[$id] ?? null;
42    }
43
44    /**
45     * Query and cache all operating systems from the vultr api.
46     *
47     * @param $override - bool - Depending on whether to requery the operating systems.
48     * @throws OperatingSystemException
49     * @return void
50     */
51    public function cacheOperatingSystems(bool $override = false) : void
52    {
53        if (static::$cache_operatingsystems !== null && !$override) return;
54
55        static::$cache_operatingsystems = [];
56        $options = new ListOptions(500);
57        while (true)
58        {
59            foreach ($this->getOperatingSystems($options) as $os)
60            {
61                static::$cache_operatingsystems[$os->getId()] = $os;
62            }
63
64            if ($options->getNextCursor() == '')
65            {
66                break;
67            }
68            $options->setCurrentCursor($options->getNextCursor());
69        }
70    }
71}