Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.86% covered (success)
92.86%
13 / 14
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApplicationService
92.86% covered (success)
92.86%
13 / 14
66.67% covered (warning)
66.67%
2 / 3
9.03
0.00% covered (danger)
0.00%
0 / 1
 getApplications
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getApplication
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 cacheApplications
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\Applications;
6
7use Vultr\VultrPhp\Services\VultrService;
8use Vultr\VultrPhp\Util\ListOptions;
9
10/**
11 * Application service handler, for applications endpoints.
12 *
13 * @see https://www.vultr.com/api/#tag/application
14 */
15class ApplicationService extends VultrService
16{
17    public const FILTER_ALL = 'all';
18    public const FILTER_MARKETPLACE = 'marketplace';
19    public const FILTER_ONE_CLICK = 'one-click';
20
21    private static ?array $cache_applications = null;
22
23    /**
24     * Get a list of all available application images.
25     *
26     * @param $filter - ENUM('all', 'marketplace', 'one-click')
27     * @param $options - ListOptions|null - Interact via reference.
28     * @throws ApplicationException
29     * @return Application[]
30     */
31    public function getApplications(string $filter = self::FILTER_ALL, ?ListOptions &$options = null) : array
32    {
33        if ($options === null)
34        {
35            $options = new ListOptions(150);
36        }
37        return $this->getListObjects('applications', new Application(), $options, ['type' => $filter]);
38    }
39
40    /**
41     * Get a specific application object based on the app_id.
42     *
43     * @param $id - int - Application id, whether one click or marketplace app.
44     * @throws ApplicationException
45     * @return Application|null
46     */
47    public function getApplication(int $id) : ?Application
48    {
49        $this->cacheApplications();
50        return static::$cache_applications[$id] ?? null;
51    }
52
53    /**
54     * Cache all available applications from the vultr api.
55     *
56     * @param $override - bool - Depending on whether to requery the applications.
57     * @throws ApplicationException
58     * @return void
59     */
60    public function cacheApplications(bool $override = false) : void
61    {
62        if (static::$cache_applications !== null && !$override) return;
63
64        static::$cache_applications = [];
65        $options = new ListOptions(500);
66        while (true)
67        {
68            foreach ($this->getApplications(self::FILTER_ALL, $options) as $app)
69            {
70                static::$cache_applications[$app->getId()] = $app;
71            }
72
73            if ($options->getNextCursor() == '')
74            {
75                break;
76            }
77            $options->setCurrentCursor($options->getNextCursor());
78        }
79    }
80}