Skip to content

Commit

Permalink
chore: remove direct API calls and use Macroable
Browse files Browse the repository at this point in the history
  • Loading branch information
ju5t committed Jun 2, 2024
1 parent b5e160c commit 746557b
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 47 deletions.
11 changes: 0 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,6 @@ user, you can use the `become` method:
$result = DirectAdmin::become('user')->post('{DIRECTADMIN_API_CALL}');
```

### Passing the DirectAdmin API call as a method

You can also call any DirectAdmin API command by passing it as method to
the `DirectAdmin` facade like so:

```php
$result = DirectAdmin::CMD_API_DOMAIN_OWNERS();
```

This will be a POST request by default.

For more information on the available commands, please refer to the
[DirectAdmin API documentation](https://docs.directadmin.com/directadmin/customizing-workflow/api-all-about.html).

Expand Down
12 changes: 3 additions & 9 deletions src/DirectAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Sensson\DirectAdmin;

use Carbon\Traits\Macro;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Support\Traits\Tappable;
use JsonException;
use Sensson\DirectAdmin\Enums\Method;
Expand All @@ -17,6 +19,7 @@

class DirectAdmin
{
use Macroable;
use Tappable;

public bool $debug = false;
Expand Down Expand Up @@ -143,13 +146,4 @@ protected function getQueryParams(): array
'json' => 'yes',
];
}

public function __call(string $name, array $arguments)
{
if (method_exists($this, $name)) {
return $this->{$name}(...$arguments);
}

return $this->call($name, $arguments);
}
}
27 changes: 0 additions & 27 deletions tests/DirectAdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,13 @@
]);
});

it('processes api call directly', function () {
Http::fake(function () {
return Http::response([
'domain.com' => 'username',
]);
});

$response = app(DirectAdmin::class)->CMD_API_DOMAIN_OWNERS();

expect($response->toArray())
->toMatchArray([
'domain.com' => 'username',
]);
});

it('becomes a user', function () {
config()->set('directadmin.username', 'admin');
$directadmin = app(DirectAdmin::class)->become('user');

expect($directadmin->username)->toBe('admin|user');
});

it('becomes a user even when passing the command as a method', function () {
Http::fake(function () {
return Http::response([
'domain.com' => 'username',
]);
});

config()->set('directadmin.username', 'admin');
/** @noinspection PhpUndefinedMethodInspection */
app(DirectAdmin::class)->become('user')->CMD_API_DOMAIN_OWNERS();
})->throwsNoExceptions();

it('fails when incorrect credentials are used', function () {
Http::fake(fn () => Http::response([], 401));

Expand Down

0 comments on commit 746557b

Please sign in to comment.