Laravel Hetzner Robot

PHP MIT

Laravel SDK for the Hetzner Robot API with fluent resources, type-safe responses, failover IP management, dedicated server automation, and seamless Laravel integration.

Stars
20
Forks
0
Downloads
2,250
Open Issues
0
Files main

Repository Files

Loading file structure...
src/Managers/AbstractManager.php
<?php

namespace Vendor\HetznerRobot\Managers;

use GuzzleHttp\Promise\PromiseInterface;
use Vendor\HetznerRobot\Http\Client\HetznerClient;

abstract class AbstractManager
{
    protected HetznerClient $client;

    protected array $filters = [];

    public function __construct(HetznerClient $client)
    {
        $this->client = $client;
    }

    /**
     * Set filters for the request.
     *
     * @return $this
     */
    public function filter(array $filters): self
    {
        $this->filters = array_merge($this->filters, $filters);

        return $this;
    }

    /**
     * Configure next request to run asynchronously.
     *
     * @return $this
     */
    public function async(bool $async = true): self
    {
        $this->client->setAsync($async);

        return $this;
    }

    /**
     * Build parameters for GET requests.
     */
    protected function buildQueryParams(): array
    {
        $params = $this->filters;
        $this->resetQuery();

        return $params;
    }

    /**
     * Reset the query builder state.
     */
    protected function resetQuery(): void
    {
        $this->filters = [];
    }

    /**
     * Perform a GET request.
     *
     * @return mixed
     */
    protected function getRequest(string $uri, array $query = [])
    {
        return $this->client->request('GET', $uri, ['query' => $query]);
    }

    /**
     * Perform a POST request.
     *
     * @return mixed
     */
    protected function postRequest(string $uri, array $data = [])
    {
        $options = [];
        if (! empty($data)) {
            $options['form_params'] = $data;
        }

        return $this->client->request('POST', $uri, $options);
    }

    /**
     * Perform a PUT request.
     *
     * @return mixed
     */
    protected function putRequest(string $uri, array $data = [])
    {
        $options = [];
        if (! empty($data)) {
            $options['form_params'] = $data;
        }

        return $this->client->request('PUT', $uri, $options);
    }

    /**
     * Perform a DELETE request.
     *
     * @return mixed
     */
    protected function deleteRequest(string $uri, array $data = [])
    {
        $options = [];
        if (! empty($data)) {
            $options['form_params'] = $data;
        }

        return $this->client->request('DELETE', $uri, $options);
    }

    /**
     * Helper to wrap a promise mapping response data to DTOs or collections.
     *
     * @param  mixed  $result  The return value of request() which can be Guzzle Promise or decoded array
     * @return mixed PromiseInterface or hydrated DTO/Collection
     */
    protected function hydrate($result, callable $hydrator)
    {
        if ($result instanceof PromiseInterface) {
            return $result->then($hydrator);
        }

        return $hydrator($result);
    }
}