Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
],
"require": {
"php": ">=8.0",
"guzzlehttp/guzzle": "^7.10"
"guzzlehttp/guzzle": "^7.10",
"nesbot/carbon": "^3.10"
Comment thread
achyutkneupane marked this conversation as resolved.
},
"require-dev": {
"laravel/pint": "^1.25",
Expand Down
34 changes: 9 additions & 25 deletions src/Contracts/HamroCDNContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,12 @@

namespace HamroCDN\Contracts;

use HamroCDN\Models\Upload;

/**
* @phpstan-type HamroCDNFile array{
* url: string,
* size: int
* }
* @phpstan-type HamroCDNUser array{
* name: string,
* email: string,
* role: string
* }
* @phpstan-type HamroCDNObject array{
* nanoId: string,
* user: HamroCDNUser,
* delete_at: string|null,
* original: HamroCDNFile
* }
* @phpstan-import-type HamroCDNObject from Upload
* @phpstan-import-type UploadWithPagination from Upload
*
* @phpstan-type HamroCDNData array{
* data: HamroCDNObject
* }
Expand All @@ -33,28 +23,22 @@ interface HamroCDNContract
/**
* List all of your files in HamroCDN.
*
* @return HamroCDNObjectWithPagination
* @return UploadWithPagination
*/
public function index(): array;

/**
* Fetch a file from HamroCDN.
*
* @return HamroCDNData
*/
public function fetch(string $nanoId): array;
public function fetch(string $nanoId): Upload;

/**
* Upload a file to HamroCDN.
*
* @return HamroCDNData
*/
public function upload(string $filePath): array;
public function upload(string $filePath): Upload;

/**
* Upload a file to HamroCDN by URL.
*
* @return HamroCDNData
*/
public function uploadByURL(string $url): array;
public function uploadByURL(string $url): Upload;
}
35 changes: 25 additions & 10 deletions src/HamroCDN.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
use GuzzleHttp\Client;
use HamroCDN\Contracts\HamroCDNContract;
use HamroCDN\Exceptions\HamroCDNException;
use HamroCDN\Models\Upload;
use HamroCDN\Traits\HasConfigValues;
use HamroCDN\Traits\Requestable;

/**
* @phpstan-import-type HamroCDNObject from Upload
* @phpstan-import-type HamroCDNData from HamroCDNContract
* @phpstan-import-type HamroCDNObjectWithPagination from HamroCDNContract
*/
Expand All @@ -37,37 +39,46 @@ public function __construct(?string $apiKey = null, ?string $baseUrl = null, ?Cl
}

/**
* @return HamroCDNObjectWithPagination
*
* @throws HamroCDNException

Copilot AI Oct 28, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return type documentation is missing for the index() method. While the interface declares @return UploadWithPagination, the actual implementation returns a transformed array structure with Upload objects. Consider adding a return type annotation like @return array{data: array<Upload>, meta: array{total: int, per_page: int, page: int}} to clarify the return structure.

Suggested change
* @throws HamroCDNException
* @throws HamroCDNException
* @return array{data: array<Upload>, meta: array{total: int, per_page: int, page: int}}

Copilot uses AI. Check for mistakes.
*/
public function index(?int $per_page = 20, ?int $page = 1): array
{
/** @var HamroCDNObjectWithPagination */
return $this->get('uploads', [
/** @var HamroCDNObjectWithPagination $response */
$response = $this->get('uploads', [
'per_page' => $per_page,
'page' => $page,
]);

return [
'data' => array_map(
/** @param HamroCDNObject $item */
fn (array $item): Upload => Upload::fromArray($item),
$response['data']
),
'meta' => $response['meta'],
];
}

/**
* @throws HamroCDNException

Copilot AI Oct 28, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing return type documentation. Consider adding @return Upload to match the interface documentation style and clarify what is returned.

Suggested change
* @throws HamroCDNException
* @throws HamroCDNException
* @return Upload

Copilot uses AI. Check for mistakes.
*/
public function fetch(string $nanoId): array
public function fetch(string $nanoId): Upload
{
return $this->get("uploads/{$nanoId}");
$response = $this->get("uploads/{$nanoId}");

return Upload::fromArray($response['data']);
}

/**
* @throws HamroCDNException

Copilot AI Oct 28, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing return type documentation. Consider adding @return Upload to match the interface documentation style and clarify what is returned.

Suggested change
* @throws HamroCDNException
* @throws HamroCDNException
* @return Upload

Copilot uses AI. Check for mistakes.
*/
public function upload(string $filePath): array
public function upload(string $filePath): Upload
{
if (! file_exists($filePath)) {
throw HamroCDNException::fileError($filePath);
}

return $this->post('uploads', [
$response = $this->post('uploads', [
'multipart' => [
[
'name' => 'file',
Expand All @@ -76,17 +87,21 @@ public function upload(string $filePath): array
],
],
]);

return Upload::fromArray($response['data']);
}

/**
* @throws HamroCDNException

Copilot AI Oct 28, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing return type documentation. Consider adding @return Upload to match the interface documentation style and clarify what is returned.

Suggested change
* @throws HamroCDNException
* @throws HamroCDNException
* @return Upload

Copilot uses AI. Check for mistakes.
*/
public function uploadByURL(string $url): array
public function uploadByURL(string $url): Upload
{
return $this->post('upload-from-url', [
$response = $this->post('upload-from-url', [
'json' => [
'url' => $url,
],
]);

return Upload::fromArray($response['data']);
}
}
47 changes: 47 additions & 0 deletions src/Models/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace HamroCDN\Models;

/**
* @phpstan-type HamroCDNFile array{
* url: string,
* size: int
* }
*/
final class File
{
public function __construct(
private string $url,
private int $size
) {}

/** @param HamroCDNFile $data */
public static function fromArray(array $data): self
{
return new self(
$data['url'],
(int) $data['size'],
);
}

public function getUrl(): string
{
return $this->url;
}

public function getSize(): int
{
return $this->size;
}

/** @return HamroCDNFile */
public function toArray(): array
{
return [
'url' => $this->url,
'size' => $this->size,
];
}
}
81 changes: 81 additions & 0 deletions src/Models/Upload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

namespace HamroCDN\Models;

use Carbon\Carbon;

/**
* @phpstan-import-type HamroCDNUser from User
* @phpstan-import-type HamroCDNFile from File
*
* @phpstan-type HamroCDNObject array{
* nanoId: string,
* user: HamroCDNUser|null,
* delete_at: string|Carbon|null,
* original: HamroCDNFile
* }
* @phpstan-type UploadWithPagination array{
* data: array<Upload>,
* meta: array{total: int, per_page: int, page: int}
* }
*/
final class Upload
{
public function __construct(
private string $nanoId,
private ?User $user,
private ?Carbon $deleteAt,
private File $original
) {}

/** @param HamroCDNObject $data */
public static function fromArray(array $data): self
{
$user = null;
if (isset($data['user'])) {
$user = User::fromArray($data['user']);
}

$original = File::fromArray($data['original']);

return new self(
$data['nanoId'],
$user,
isset($data['delete_at']) ? new Carbon($data['delete_at']) : null,
$original
);
}

public function getNanoId(): string
{
return $this->nanoId;
}

public function getUser(): ?User
{
return $this->user;
}

public function getDeleteAt(): ?Carbon
{
return $this->deleteAt;
}

public function getOriginal(): File
{
return $this->original;
}

/** @return HamroCDNObject */
public function toArray(): array
{
return [
'nanoId' => $this->nanoId,
'user' => $this->user?->toArray(),
'delete_at' => $this->deleteAt,
'original' => $this->original->toArray(),
];
}
}
47 changes: 47 additions & 0 deletions src/Models/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace HamroCDN\Models;

/**
* @phpstan-type HamroCDNUser array{
* name: string,
* email: string
* }
*/
final class User
{
public function __construct(
private string $name,
private string $email
) {}

/** @param HamroCDNUser $data */
public static function fromArray(array $data): self
{
return new self(
$data['name'],
$data['email'],
);
}

public function getName(): string
{
return $this->name;
}

public function getEmail(): string
{
return $this->email;
}

/** @return HamroCDNUser */
public function toArray(): array
{
return [
'name' => $this->name,
'email' => $this->email,
];
}
}
Loading