Files
many-notes/app/Actions/GetPathFromVaultNode.php
2025-01-27 21:30:53 +00:00

45 lines
1.0 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Actions;
use App\Models\User;
use App\Models\Vault;
use App\Models\VaultNode;
final readonly class GetPathFromVaultNode
{
public function handle(VaultNode $node, bool $includeSelf = true): string
{
/** @var Vault $vault */
$vault = $node->vault;
/** @var User $user */
$user = $vault->user;
$relativePath = '';
if ($node->parent) {
/**
* @var string $fullPath
*
* @phpstan-ignore-next-line larastan.noUnnecessaryCollectionCall
*/
$fullPath = $node->parent->ancestorsAndSelf()->get()->last()->full_path;
$relativePath = $fullPath . '/';
}
$path = sprintf(
'private/vaults/%u/%s/%s',
$user->id,
$vault->name,
$relativePath,
);
if ($includeSelf) {
$path .= $node->name . ($node->is_file ? '.' . $node->extension : '');
}
return $path;
}
}