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

namespace GhostCompiler\Hetzner\StorageBox\Managers;

use GhostCompiler\Hetzner\StorageBox\Collections\StorageBoxSnapshotCollection;
use GhostCompiler\Hetzner\StorageBox\DTOs\Action;
use GhostCompiler\Hetzner\StorageBox\DTOs\StorageBoxSnapshot;
use GhostCompiler\Hetzner\StorageBox\Http\Client\HetznerClient;
use GuzzleHttp\Promise\PromiseInterface;

class StorageBoxSnapshotManager extends AbstractManager
{
    protected int $storageBoxId;

    public function __construct(HetznerClient $client, int $storageBoxId)
    {
        parent::__construct($client);
        $this->storageBoxId = $storageBoxId;
    }

    /**
     * Get all snapshots for the storage box.
     *
     * @return StorageBoxSnapshotCollection|PromiseInterface
     */
    public function all()
    {
        $response = $this->getRequest("storage_boxes/{$this->storageBoxId}/snapshots", $this->buildQueryParams());

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

            return new StorageBoxSnapshotCollection($snaps);
        });
    }

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

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

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

    /**
     * Create a snapshot.
     *
     * @return StorageBoxSnapshot|PromiseInterface
     */
    public function create(array $data)
    {
        $response = $this->postRequest("storage_boxes/{$this->storageBoxId}/snapshots", $data);

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

    /**
     * Update a snapshot (description, labels).
     *
     * @return StorageBoxSnapshot|PromiseInterface
     */
    public function update(int $id, array $data)
    {
        $response = $this->putRequest("storage_boxes/{$this->storageBoxId}/snapshots/{$id}", $data);

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

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

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