-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathValidator.php
50 lines (43 loc) · 1.35 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
44
45
46
47
48
49
50
<?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');
} catch (RequestException $e) {
$reason = $e->getMessage();
if (null !== ($response = $e->getResponse())) {
if (403 === $response->getStatusCode()) {
$reason = \ArgumentFormatter::format('invalid key');
} else {
$reason = "Unknown HTTP status, " . $response->getStatusCode();
}
}
return error('%(provider)s key validation failed: %(reason)s', [
'provider' => 'Vultr',
'reason' => $reason
]);
}
return true;
}
}