<?php

declare(strict_types=1);

namespace SHServ\Integrations\GAuth\Http;

use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\StreamInterface;

final class Psr7Factory implements RequestFactoryInterface, StreamFactoryInterface
{
    public function createRequest(string $method, $uri): RequestInterface
    {
        return new Psr7Request($method, (string) $uri);
    }

    public function createStream(string $content = ''): StreamInterface
    {
        return new Psr7Stream($content);
    }

    public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
    {
        $resource = fopen($filename, $mode);
        if ($resource === false) {
            throw new \RuntimeException("Unable to open file: {$filename}");
        }
        return Psr7Stream::fromResource($resource);
    }

    public function createStreamFromResource($resource): StreamInterface
    {
        return Psr7Stream::fromResource($resource);
    }
}
