Laravel Hetzner Storagebox

PHP MIT

Production-ready Laravel package for integrating Hetzner Storage Box into Laravel applications using the native Storage facade and filesystem API.

Stars
19
Forks
0
Downloads
2,357
Open Issues
0
Files main

Repository Files

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

namespace GhostCompiler\Hetzner\StorageBox\Managers;

use GhostCompiler\Hetzner\StorageBox\Http\Client\HetznerClient;
use GuzzleHttp\Promise\Utils;

class HetznerStorageBoxManager
{
    private HetznerClient $client;

    private array $managers = [];

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

    public function authenticate(string $token): self
    {
        $this->client->authenticate($token);

        return $this;
    }

    public function client(): HetznerClient
    {
        return $this->client;
    }

    public function version(): string
    {
        $path = __DIR__.'/../../composer.json';
        if (file_exists($path)) {
            $composer = json_decode(file_get_contents($path), true);

            return $composer['version'] ?? '1.0.0';
        }

        return '1.0.0';
    }

    public function config(): array
    {
        if (function_exists('config')) {
            return config('hetzner-storagebox') ?: [];
        }

        return [];
    }

    public function rateLimit(): array
    {
        return $this->client->getLastRateLimit();
    }

    public function ping(): bool
    {
        try {
            $this->storageBoxes()->perPage(1)->get();

            return true;
        } catch (\Throwable $e) {
            return false;
        }
    }

    public function health(): array
    {
        $start = microtime(true);
        $ping = $this->ping();
        $latency = (int) ((microtime(true) - $start) * 1000);

        return [
            'status' => $ping ? 'healthy' : 'unhealthy',
            'latency_ms' => $latency,
            'timestamp' => time(),
        ];
    }

    /**
     * Run multiple SDK requests concurrently.
     */
    public function batch(array $callbacks): array
    {
        $this->client->startBatch();

        $callbackPromises = [];
        foreach ($callbacks as $callback) {
            $callbackPromises[] = $callback();
        }

        $this->client->endBatch();

        if (empty($callbackPromises)) {
            return [];
        }

        // Wait for all callback promises concurrently
        return Utils::all($callbackPromises)->wait();
    }

    public function storageBoxes(): StorageBoxManager
    {
        return $this->getManager(StorageBoxManager::class);
    }

    public function storageBoxTypes(): StorageBoxTypeManager
    {
        return $this->getManager(StorageBoxTypeManager::class);
    }

    public function actions(): ActionManager
    {
        return $this->getManager(ActionManager::class);
    }

    public function locations(): LocationManager
    {
        return $this->getManager(LocationManager::class);
    }

    /**
     * Lazy instantiate and cache managers.
     */
    private function getManager(string $class)
    {
        if (! isset($this->managers[$class])) {
            $this->managers[$class] = new $class($this->client);
        }

        return $this->managers[$class];
    }
}