Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
VultrClient
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
8 / 8
16
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
 create
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __get
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 setClient
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setRequestFactory
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setResponseFactory
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setStreamFactory
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setClientHandler
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3declare(strict_types=1);
4
5namespace Vultr\VultrPhp;
6
7use Http\Discovery\Psr17FactoryDiscovery;
8use Http\Discovery\Psr18ClientDiscovery;
9use Psr\Http\Client\ClientInterface;
10use Psr\Http\Message\RequestFactoryInterface;
11use Psr\Http\Message\ResponseFactoryInterface;
12use Psr\Http\Message\StreamFactoryInterface;
13use ReflectionClass;
14use ReflectionProperty;
15use Throwable;
16use Vultr\VultrPhp\Services;
17
18/**
19 * @property-read Services\Account\AccountService $account
20 * @property-read Services\Applications\ApplicationService $applications
21 * @property-read Services\Backups\BackupService $backups
22 * @property-read Services\BareMetal\BareMetalService $baremetal
23 * @property-read Services\Billing\BillingService $billing
24 * @property-read Services\BlockStorage\BlockStorageService $blockstorage
25 * @property-read Services\DNS\DNSService $dns
26 * @property-read Services\Firewall\FirewallService $firewall
27 * @property-read Services\Instances\InstanceService $instances
28 * @property-read Services\ISO\ISOService $iso
29 * @property-read Services\Kubernetes\KubernetesService $kubernetes
30 * @property-read Services\LoadBalancers\LoadBalancerService $loadbalancers
31 * @property-read Services\ObjectStorage\ObjectStorageService $objectstorage
32 * @property-read Services\OperatingSystems\OperatingSystemService $operating_system
33 * @property-read Services\Plans\PlanService $plans
34 * @property-read Services\ReservedIP\ReservedIPService $reserved_ip
35 * @property-read Services\Regions\RegionService $regions
36 * @property-read Services\Snapshots\SnapshotService $snapshots
37 * @property-read Services\SSHKeys\SSHKeyService $ssh_keys
38 * @property-read Services\StartupScripts\StartupScriptService $startup_scripts
39 * @property-read Services\Users\UserService $users
40 * @property-read Services\VPC\VPCService $vpc
41 */
42class VultrClient
43{
44    private VultrClientHandler $client;
45
46    /**
47     * @var int[]
48     */
49    private array $service_props = [];
50
51    ////
52    // Service Properties Start
53    // @codingStandardsIgnoreStart
54    ////
55
56    private Services\Account\AccountService $_account;
57    private Services\Applications\ApplicationService $_applications;
58    private Services\Backups\BackupService $_backups;
59    private Services\BareMetal\BareMetalService $_baremetal;
60    private Services\Billing\BillingService $_billing;
61    private Services\BlockStorage\BlockStorageService $_blockstorage;
62    private Services\DNS\DNSService $_dns;
63    private Services\Firewall\FirewallService $_firewall;
64    private Services\Instances\InstanceService $_instances;
65    private Services\ISO\ISOService $_iso;
66    private Services\Kubernetes\KubernetesService $_kubernetes;
67    private Services\LoadBalancers\LoadBalancerService $_loadbalancers;
68    private Services\ObjectStorage\ObjectStorageService $_objectstorage;
69    private Services\OperatingSystems\OperatingSystemService $_operating_system;
70    private Services\Plans\PlanService $_plans;
71    private Services\ReservedIP\ReservedIPService $_reserved_ip;
72    private Services\Regions\RegionService $_regions;
73    private Services\Snapshots\SnapshotService $_snapshots;
74    private Services\SSHKeys\SSHKeyService $_ssh_keys;
75    private Services\StartupScripts\StartupScriptService $_startup_scripts;
76    private Services\Users\UserService $_users;
77    private Services\VPC\VPCService $_vpc;
78
79    ////
80    // Service Properties End
81    // @codingStandardsIgnoreEnd
82    ////
83
84    /**
85     * @param $http - PSR18 ClientInterface - https://www.php-fig.org/psr/psr-18/
86     * @param $request - PSR17 RequestFactoryInterface - https://www.php-fig.org/psr/psr-17/#21-requestfactoryinterface
87     * @param $response - PSR17 ResponseFactoryInterface - https://www.php-fig.org/psr/psr-17/#22-responsefactoryinterface
88     * @param $stream - PSR17 StreamFactoryInterface - https://www.php-fig.org/psr/psr-17/#22-responsefactoryinterface
89     */
90    private function __construct(
91        string $API_KEY,
92        ?ClientInterface $http = null,
93        ?RequestFactoryInterface $request = null,
94        ?ResponseFactoryInterface $response = null,
95        ?StreamFactoryInterface $stream = null
96    )
97    {
98        try
99        {
100            $this->setClientHandler($API_KEY, $http, $request, $response, $stream);
101        }
102        catch (Throwable $e)
103        {
104            throw new VultrException('Failed to initialize client: '.$e->getMessage(), VultrException::DEFAULT_CODE, null, $e);
105        }
106
107        foreach ((new ReflectionClass($this))->getProperties(ReflectionProperty::IS_PRIVATE) as $property)
108        {
109            $type_name = $property->getType()->getName();
110            if (stripos($type_name, 'Services') === false) continue;
111
112            $prop_name = $property->getName();
113            $this->$prop_name = new $type_name($this, $this->client);
114            $this->service_props[$prop_name] = 1;
115        }
116    }
117
118    /**
119     * The entry point into using the client and all its related services.
120     */
121    public static function create(
122        string $API_KEY,
123        ?ClientInterface $http = null,
124        ?RequestFactoryInterface $request = null,
125        ?ResponseFactoryInterface $response = null,
126        ?StreamFactoryInterface $stream = null
127    ) : VultrClient
128    {
129        return new VultrClient($API_KEY, $http, $request, $response, $stream);
130    }
131
132    /**
133     * Magic method that allows interaction with readonly properties that house service handlers.
134     *
135     * Client will return null if the service object does not exist.
136     */
137    public function __get(string $name) : mixed
138    {
139        $name = '_'.$name;
140        return ($this->service_props[$name] ?? null) !== null ? $this->$name : null;
141    }
142
143    public function setClient(ClientInterface $http) : void
144    {
145        $this->client->setClient($http);
146    }
147
148    public function setRequestFactory(RequestFactoryInterface $request) : void
149    {
150        $this->client->setRequestFactory($request);
151    }
152
153    public function setResponseFactory(ResponseFactoryInterface $response) : void
154    {
155        $this->client->setResponseFactory($response);
156    }
157
158    public function setStreamFactory(StreamFactoryInterface $stream) : void
159    {
160        $this->client->setStreamFactory($stream);
161    }
162
163    protected function setClientHandler(
164        string $API_KEY,
165        ?ClientInterface $http = null,
166        ?RequestFactoryInterface $request = null,
167        ?ResponseFactoryInterface $response = null,
168        ?StreamFactoryInterface $stream = null
169    ) : void
170    {
171        $this->client = new VultrClientHandler(
172            new VultrAuth($API_KEY),
173            $http ?: Psr18ClientDiscovery::find(),
174            $request ?: Psr17FactoryDiscovery::findRequestFactory(),
175            $response ?: Psr17FactoryDiscovery::findResponseFactory(),
176            $stream ?: Psr17FactoryDiscovery::findStreamFactory()
177        );
178    }
179}