<?php

declare(strict_types=1);

namespace Apexl\Io\Cache\Services;

use Apexl\Io\Cache\Interfaces\CacheStreamInterface;
use DI\Container;
use DI\DependencyException;
use DI\NotFoundException;
use Stash\Driver\BlackHole;
use Stash\Interfaces\DriverInterface;

final readonly class CacheStreamRegistry
{
    public function __construct(private Container $container) {}

    /**
     * @throws DependencyException
     * @throws NotFoundException
     */
    public function blackHole(): CacheStreamInterface
    {
        return $this->get($this->container->get(BlackHole::class));
    }

    /**
     * @throws DependencyException
     * @throws NotFoundException
     */
    public function get(DriverInterface $driver): CacheStreamInterface
    {
        $name = sprintf('%s:%s', CacheStreamInterface::class, $driver::class);
        if (!$this->container->has($name)) {
            $this->container->set($name, $this->container->make(CacheStreamInterface::class, [
                'driver' => $driver,
            ]));
        }
        return $this->container->get($name);
    }
}