<?php

namespace App\Framework\Http;

/**
 *
 * class AuthMiddleware
 * {
 *      public function __invoke($context, $next, $state)
 *      {
 *          $auth = $state->namespace('auth');
 *
 *          $auth->set('user', $user);              Intern: 'auth.user'
 *          $auth->set('permissions', $perms);      Intern: 'auth.permissions'
 *
 *      return $next($context);
 *      }
 * }
 */

final readonly class NamespacedState
{
    public function __construct(
        private RequestStateManager $stateManager,
        private string              $namespace
    ){}

    public function set(string $key, mixed $value): void
    {
        $this->stateManager->set("{$this->namespace}.{$key}", $value);
    }

    public function get(string $key, mixed $default = null): mixed
    {
        return $this->stateManager->get("{$this->namespace}.{$key}", $default);
    }
}
