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

namespace GhostCompiler\Hetzner\StorageBox\Managers;

use GhostCompiler\Hetzner\StorageBox\Collections\StorageBoxSubaccountCollection;
use GhostCompiler\Hetzner\StorageBox\DTOs\Action;
use GhostCompiler\Hetzner\StorageBox\DTOs\StorageBoxSubaccount;
use GhostCompiler\Hetzner\StorageBox\Http\Client\HetznerClient;
use GuzzleHttp\Promise\PromiseInterface;

class StorageBoxSubaccountManager extends AbstractManager
{
    protected int $storageBoxId;

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

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

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

            return new StorageBoxSubaccountCollection($subs);
        });
    }

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

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

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

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

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

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

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

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

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

    /**
     * Change home directory.
     *
     * @return Action|PromiseInterface
     */
    public function changeHomeDirectory(int $id, string $homeDirectory)
    {
        return $this->postAction($id, 'change_home_directory', ['home_directory' => $homeDirectory]);
    }

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

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

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

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