|
2 | 2 |
|
3 | 3 | namespace Sensson\DirectAdmin;
|
4 | 4 |
|
| 5 | +use Illuminate\Http\Client\ConnectionException; |
| 6 | +use Illuminate\Http\Client\Response; |
5 | 7 | use Illuminate\Support\Collection;
|
6 |
| -use Illuminate\Support\Traits\ForwardsCalls; |
| 8 | +use Illuminate\Support\Facades\Http; |
7 | 9 | use Illuminate\Support\Traits\Tappable;
|
| 10 | +use JsonException; |
| 11 | +use Sensson\DirectAdmin\Exceptions\AuthenticationFailed; |
| 12 | +use Sensson\DirectAdmin\Exceptions\CommandNotFound; |
8 | 13 | use Sensson\DirectAdmin\Exceptions\ConnectionFailed;
|
9 | 14 | use Sensson\DirectAdmin\Exceptions\InvalidResponse;
|
| 15 | +use Sensson\DirectAdmin\Exceptions\Unauthorized; |
10 | 16 |
|
11 | 17 | class DirectAdmin
|
12 | 18 | {
|
13 |
| - use ForwardsCalls; |
14 | 19 | use Tappable;
|
15 | 20 |
|
16 |
| - public function __construct(public Api $api) |
| 21 | + public bool $debug = false; |
| 22 | + |
| 23 | + /** |
| 24 | + * Call the DirectAdmin API by giving it an API command and some |
| 25 | + * parameters. This will return an array with processed data. |
| 26 | + */ |
| 27 | + public function call(string $command, array $params = []): Collection |
17 | 28 | {
|
18 |
| - // |
| 29 | + try { |
| 30 | + $response = Http::acceptJson() |
| 31 | + ->withBasicAuth(config('directadmin.username'), config('directadmin.password')) |
| 32 | + ->withOptions($this->getHttpOptions()) |
| 33 | + ->withQueryParameters($this->getQueryParams()) |
| 34 | + ->post(config('directadmin.baseUrl').'/'.strtoupper($command), $params); |
| 35 | + } catch (ConnectionException $e) { |
| 36 | + throw ConnectionFailed::create($e->getMessage()); |
| 37 | + } |
| 38 | + |
| 39 | + return $this->processResponse( |
| 40 | + response: $response, |
| 41 | + command: $command, |
| 42 | + ); |
19 | 43 | }
|
20 | 44 |
|
21 | 45 | /**
|
22 |
| - * @throws ConnectionFailed |
23 |
| - * @throws InvalidResponse |
| 46 | + * Enable debug mode for HTTP requests. This can help identify |
| 47 | + * issues with the DirectAdmin server. |
24 | 48 | */
|
25 |
| - public function call(string $command, array $params = []): Collection |
| 49 | + public function debug(): static |
| 50 | + { |
| 51 | + $this->debug = true; |
| 52 | + |
| 53 | + return $this; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Process the response that's returned by the DirectAdmin API |
| 58 | + * and prepare it for further processing by third party code. |
| 59 | + * |
| 60 | + */ |
| 61 | + protected function processResponse(Response $response, string $command = ''): Collection |
26 | 62 | {
|
27 |
| - return $this->api->call($command, $params); |
| 63 | + if (! $response->successful() && $response->status() !== 500) { |
| 64 | + match ($response->status()) { |
| 65 | + 401 => throw AuthenticationFailed::create(), |
| 66 | + 403 => throw Unauthorized::create(), |
| 67 | + 405 => throw CommandNotFound::create($command), |
| 68 | + default => throw ConnectionFailed::create($response->body()), |
| 69 | + }; |
| 70 | + } |
| 71 | + |
| 72 | + try { |
| 73 | + $result = json_decode($response->body(), associative: true, flags: JSON_THROW_ON_ERROR); |
| 74 | + $result = collect($result); |
| 75 | + } catch (JsonException) { |
| 76 | + throw InvalidResponse::create('Invalid JSON returned by server: '.$response->body()); |
| 77 | + } |
| 78 | + |
| 79 | + $this->validateResult($result); |
| 80 | + |
| 81 | + return $result; |
28 | 82 | }
|
29 | 83 |
|
| 84 | + /** |
| 85 | + * Validate the content returned by DirectAdmin. Even though the |
| 86 | + * API can return a successfull response to our HTTP-request |
| 87 | + * the actual call may have failed. |
| 88 | + */ |
| 89 | + protected function validateResult(Collection $result): void |
| 90 | + { |
| 91 | + if ($result->has('error')) { |
| 92 | + $error = $result->get('error'); |
| 93 | + $description = $result->get('result'); |
| 94 | + |
| 95 | + if (! empty($description)) { |
| 96 | + $description = ': '.$description; |
| 97 | + } |
| 98 | + |
| 99 | + throw InvalidResponse::create($error.$description); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + protected function getHttpOptions(): array |
| 104 | + { |
| 105 | + return [ |
| 106 | + 'debug' => $this->debug, |
| 107 | + ]; |
| 108 | + } |
| 109 | + |
| 110 | + protected function getQueryParams(): array |
| 111 | + { |
| 112 | + return [ |
| 113 | + 'json' => 'yes', |
| 114 | + ]; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @throws ConnectionFailed |
| 119 | + * @throws InvalidResponse |
| 120 | + */ |
30 | 121 | public function __call(string $name, array $arguments)
|
31 | 122 | {
|
32 |
| - return $this->forwardCallTo($this->api, $name, $arguments); |
| 123 | + if (method_exists($this, $name)) { |
| 124 | + return $this->{$name}(...$arguments); |
| 125 | + } |
| 126 | + |
| 127 | + return $this->call($name, $arguments); |
33 | 128 | }
|
34 | 129 | }
|
0 commit comments