Passed
Pull Request — master (#75)
by
unknown
22:37 queued 07:36
created

SmartStreamFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 14
c 1
b 0
f 0
dl 0
loc 28
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B createStream() 0 19 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Stream;
6
7
use Psr\Http\Message\StreamFactoryInterface;
8
use Psr\Http\Message\StreamInterface;
9
10
final class SmartStreamFactory
11
{
12
    private StreamFactoryInterface $defaultFactory;
13
14
    public function __construct(StreamFactoryInterface $defaultFactory)
15
    {
16
        $this->defaultFactory = $defaultFactory;
17
    }
18
19
    public function createStream($data): StreamInterface
20
    {
21
        if (is_string($data)) {
22
            return $this->defaultFactory->createStream($data);
23
        }
24
        if ($data instanceof \SplFileInfo) {
25
            return $this->defaultFactory->createStream($data->getPath());
26
        }
27
        if (is_resource($data)) {
28
            return $this->defaultFactory->createStreamFromResource($data);
29
        }
30
        if ($data instanceof \Generator) {
31
            return new \App\Stream\GeneratorStream($data);
32
        }
33
        if (is_array($data) || is_object($data)) {
34
            return new \App\Stream\DataStream($data);
35
        }
36
37
        throw new \InvalidArgumentException('Unsupported data type');
38
    }
39
}
40