forked from apisnetworks/apiscp-dns-vultr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Validator.php
43 lines (36 loc) · 1.16 KB
/
Validator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php declare(strict_types=1);
/**
* Copyright (C) Apis Networks, Inc - All Rights Reserved.
*
* Unauthorized copying of this file, via any medium, is
* strictly prohibited without consent. Any dissemination of
* material herein is prohibited.
*
* For licensing inquiries email <[email protected]>
*
* Written by Matt Saladna <[email protected]>, August 2018
*/
namespace Opcenter\Dns\Providers\Vultr;
use GuzzleHttp\Exception\RequestException;
use Opcenter\Dns\Contracts\ServiceProvider;
use Opcenter\Service\ConfigurationContext;
class Validator implements ServiceProvider
{
public function valid(ConfigurationContext $ctx, &$var): bool
{
return ctype_alnum($var) && strtoupper($var) === $var && static::keyValid((string)$var);
}
public static function keyValid(string $key): bool
{
try {
(new Api($key))->do('GET', 'account/info');
} catch (RequestException $e) {
$status = $e->getResponse()->getStatusCode();
if (403 === $status) {
return error('Vultr key failed: invalid key');
}
return error("Unable to verify Vultr key. Unknown HTTP status `%d' returned", $status);
}
return true;
}
}