StreamTrait::createStream()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 20
rs 9.2
c 2
b 0
f 0
cc 4
eloc 11
nc 6
nop 2
1
<?php
2
3
namespace Psr7Middlewares\Utils;
4
5
use Psr7Middlewares\Middleware;
6
use Psr\Http\Message\StreamInterface;
7
8
/**
9
 * Trait used to create streams.
10
 */
11
trait StreamTrait
12
{
13
    /**
14
     * Get the stream factory.
15
     *
16
     * @param string|StreamInterface $file Filename or stream it's replacing
17
     * @param string                 $mode
18
     *
19
     * @return StreamInterface
20
     */
21
    private static function createStream($file = 'php://temp', $mode = 'r+')
22
    {
23
        $factory = Middleware::getStreamFactory();
24
        $replacing = null;
25
26
        if ($file instanceof StreamInterface) {
27
            $replacing = $file;
28
            $file = 'php://temp';
29
        }
30
31
        if ($factory === null) {
32
            if (class_exists('Zend\\Diactoros\\Stream')) {
33
                return new \Zend\Diactoros\Stream($file, $mode);
34
            }
35
36
            throw new \RuntimeException('Unable to create a stream. No stream factory defined');
37
        }
38
39
        return call_user_func($factory, $file, $mode, $replacing);
40
    }
41
}
42