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

namespace GhostCompiler\Hetzner\StorageBox\Managers;

use GhostCompiler\Hetzner\StorageBox\Collections\StorageBoxTypeCollection;
use GhostCompiler\Hetzner\StorageBox\DTOs\PaginationMeta;
use GhostCompiler\Hetzner\StorageBox\DTOs\StorageBoxType;
use GhostCompiler\Hetzner\StorageBox\Responses\PaginatedResponse;
use GuzzleHttp\Promise\PromiseInterface;

class StorageBoxTypeManager extends AbstractManager
{
    /**
     * Get all storage box types.
     *
     * @return StorageBoxTypeCollection|PromiseInterface
     */
    public function all()
    {
        $response = $this->getRequest('storage_box_types', $this->buildQueryParams());

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

            return new StorageBoxTypeCollection($types);
        });
    }

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

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

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

            return new PaginatedResponse(new StorageBoxTypeCollection($types), $meta);
        });
    }

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

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