-
Notifications
You must be signed in to change notification settings - Fork 0
/
Validator.php
43 lines (36 loc) · 1.07 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.
*
* MIT License
*
* Written by Matt Saladna <matt@apisnetworks.com>, August 2018
*/
namespace Opcenter\Dns\Providers\Linode;
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_xdigit($var) && static::keyValid((string)$var);
}
public static function keyValid(string $key): bool
{
try {
(new Api($key))->do('GET', 'domains');
} catch (RequestException $e) {
$reason = $e->getMessage();
if (null !== ($response = $e->getResponse())) {
$response = \json_decode($response->getBody()->getContents(), true);
$reason = array_get($response, 'errors.0.reason', 'Invalid key');
}
return error('%(provider)s key validation failed: %(reason)s', [
'provider' => 'Linode',
'reason' => $reason
]);
}
return true;
}
}