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/StorageBoxManager.php
<?php

namespace GhostCompiler\Hetzner\StorageBox\Managers;

use GhostCompiler\Hetzner\StorageBox\Collections\ActionCollection;
use GhostCompiler\Hetzner\StorageBox\Collections\StorageBoxCollection;
use GhostCompiler\Hetzner\StorageBox\DTOs\Action;
use GhostCompiler\Hetzner\StorageBox\DTOs\PaginationMeta;
use GhostCompiler\Hetzner\StorageBox\DTOs\StorageBox;
use GhostCompiler\Hetzner\StorageBox\Responses\PaginatedResponse;
use GhostCompiler\Hetzner\StorageBox\Responses\StorageBoxCreateResponse;
use GuzzleHttp\Promise\PromiseInterface;

class StorageBoxManager extends AbstractManager
{
    /**
     * Get all storage boxes.
     *
     * @return StorageBoxCollection|PromiseInterface
     */
    public function all()
    {
        $response = $this->getRequest('storage_boxes', $this->buildQueryParams());

        return $this->hydrate($response, function (array $data) {
            $boxes = array_map(fn (array $item) => StorageBox::fromArray($item), $data['storage_boxes'] ?? []);

            return new StorageBoxCollection($boxes);
        });
    }

    /**
     * Alias for all().
     *
     * @return StorageBoxCollection|PromiseInterface
     */
    public function get()
    {
        return $this->all();
    }

    /**
     * Paginate storage boxes.
     *
     * @return PaginatedResponse|PromiseInterface
     */
    public function paginate(int $perPage = 25, int $page = 1)
    {
        $this->perPage($perPage)->page($page);
        $response = $this->getRequest('storage_boxes', $this->buildQueryParams());

        return $this->hydrate($response, function (array $data) {
            $boxes = array_map(fn (array $item) => StorageBox::fromArray($item), $data['storage_boxes'] ?? []);
            $meta = PaginationMeta::fromArray($data['meta']['pagination'] ?? []);

            return new PaginatedResponse(new StorageBoxCollection($boxes), $meta);
        });
    }

    /**
     * Find a storage box by ID.
     *
     * @return StorageBox|PromiseInterface
     */
    public function find(int $id)
    {
        $response = $this->getRequest("storage_boxes/{$id}");

        return $this->hydrate($response, function (array $data) {
            return StorageBox::fromArray($data['storage_box'] ?? []);
        });
    }

    /**
     * Create a storage box.
     *
     * @return StorageBoxCreateResponse|PromiseInterface
     */
    public function create(array $data)
    {
        $response = $this->postRequest('storage_boxes', $data);

        return $this->hydrate($response, function (array $data) {
            return StorageBoxCreateResponse::fromArray($data);
        });
    }

    /**
     * Update a storage box.
     *
     * @return StorageBox|PromiseInterface
     */
    public function update(int $id, array $data)
    {
        $response = $this->putRequest("storage_boxes/{$id}", $data);

        return $this->hydrate($response, function (array $data) {
            return StorageBox::fromArray($data['storage_box'] ?? []);
        });
    }

    /**
     * Delete a storage box.
     *
     * @return Action|null|PromiseInterface
     */
    public function delete(int $id)
    {
        $response = $this->deleteRequest("storage_boxes/{$id}");

        return $this->hydrate($response, function (array $data) {
            return isset($data['action']) ? Action::fromArray($data['action']) : null;
        });
    }

    /**
     * List folders in a storage box.
     *
     * @return array|PromiseInterface
     */
    public function folders(int $id, ?string $path = null)
    {
        $params = [];
        if ($path !== null) {
            $params['path'] = $path;
        }

        return $this->getRequest("storage_boxes/{$id}/folders", $params);
    }

    /**
     * Get action history for a storage box.
     *
     * @return ActionCollection|PromiseInterface
     */
    public function actions(int $id)
    {
        $response = $this->getRequest("storage_boxes/{$id}/actions");

        return $this->hydrate($response, function (array $data) {
            $actions = array_map(fn (array $item) => Action::fromArray($item), $data['actions'] ?? []);

            return new ActionCollection($actions);
        });
    }

    /**
     * Change delete protection.
     *
     * @return Action|PromiseInterface
     */
    public function changeProtection(int $id, bool $delete)
    {
        return $this->postAction($id, 'change_protection', ['delete' => $delete]);
    }

    /**
     * Change storage box type.
     *
     * @return Action|PromiseInterface
     */
    public function changeType(int $id, string $type)
    {
        return $this->postAction($id, 'change_type', ['storage_box_type' => $type]);
    }

    /**
     * Reset password.
     *
     * @return Action|PromiseInterface
     */
    public function resetPassword(int $id, string $password)
    {
        return $this->postAction($id, 'reset_password', ['password' => $password]);
    }

    /**
     * Update access settings.
     *
     * @return Action|PromiseInterface
     */
    public function updateAccessSettings(int $id, array $settings)
    {
        return $this->postAction($id, 'update_access_settings', $settings);
    }

    /**
     * Rollback snapshot.
     *
     * @return Action|PromiseInterface
     */
    public function rollbackSnapshot(int $id, string $snapshot)
    {
        return $this->postAction($id, 'rollback_snapshot', ['snapshot' => $snapshot]);
    }

    /**
     * Enable snapshot plan.
     *
     * @return Action|PromiseInterface
     */
    public function enableSnapshotPlan(int $id, array $plan)
    {
        return $this->postAction($id, 'enable_snapshot_plan', $plan);
    }

    /**
     * Disable snapshot plan.
     *
     * @return Action|PromiseInterface
     */
    public function disableSnapshotPlan(int $id)
    {
        return $this->postAction($id, 'disable_snapshot_plan');
    }

    /**
     * Get the subaccount manager for a storage box.
     */
    public function subaccounts(int $storageBoxId): StorageBoxSubaccountManager
    {
        return new StorageBoxSubaccountManager($this->client, $storageBoxId);
    }

    /**
     * Get the snapshot manager for a storage box.
     */
    public function snapshots(int $storageBoxId): StorageBoxSnapshotManager
    {
        return new StorageBoxSnapshotManager($this->client, $storageBoxId);
    }

    /**
     * Post a storage box action.
     */
    private function postAction(int $storageBoxId, string $actionName, array $params = [])
    {
        $response = $this->postRequest("storage_boxes/{$storageBoxId}/actions/{$actionName}", $params);

        return $this->hydrate($response, function (array $data) {
            return Action::fromArray($data['action'] ?? []);
        });
    }
}