HEX
Server: Apache
System: Linux webm004.cluster121.gra.hosting.ovh.net 5.15.167-ovh-vps-grsec-zfs-classid #1 SMP Tue Sep 17 08:14:20 UTC 2024 x86_64
User: grainesdfo (155059)
PHP: 5.4.45
Disabled: _dyuweyrj4,_dyuweyrj4r,dl
Upload Files
File: /home/grainesdfo/www/wp-content/plugins/backwpup/src/Infrastructure/Http/Message/StreamFactory.php
<?php

declare(strict_types=1);

namespace Inpsyde\BackWPup\Infrastructure\Http\Message;

use GuzzleHttp\Psr7\Utils;
use Inpsyde\BackWPup\Infrastructure\Http\Message\Exception\CouldNotCreateStream;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;
use Webmozart\Assert\Assert;

/**
 * Factory for creating streams.
 *
 * Uses GuzzleHttp\Psr7 objects for implementation.
 */
final class StreamFactory implements StreamFactoryInterface
{
    /**
     * @var string[]
     */
    private const VALID_MODES = ['r', 'w', 'a', 'x', 'c'];

    /**
     * {@inheritdoc}
     */
    public function createStream(string $content = ''): StreamInterface
    {
        return Utils::streamFor($content);
    }

    /**
     * {@inheritdoc}
     */
    public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
    {
        Assert::inArray(strtolower($mode[0]), self::VALID_MODES, __('Invalid mode provided for fopen: %s', 'backwpup'));

        $resource = Utils::tryFopen($filename, $mode);

        return Utils::streamFor($resource);
    }

    /**
     * {@inheritdoc}
     *
     * @throws CouldNotCreateStream If the stream is not readable
     */
    public function createStreamFromResource($resource): StreamInterface
    {
        Assert::resource($resource, null, __('Stream must be a resource', 'backwpup'));

        $stream = Utils::streamFor($resource);

        if (!$stream->isReadable()) {
            throw CouldNotCreateStream::becauseItIsNotReadable();
        }

        return $stream;
    }
}