Psr7Stream   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 38
ccs 11
cts 11
cp 1
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A matches() 0 3 1
A getIterator() 0 11 2
A calculateSize() 0 3 1
1
<?php
2
3
namespace Cerbero\JsonParser\Sources;
4
5
use Psr\Http\Message\StreamInterface;
6
use Traversable;
7
8
use function in_array;
9
10
/**
11
 * The PSR-7 stream source.
12
 *
13
 * @property-read StreamInterface $source
14
 */
15
class Psr7Stream extends Source
16
{
17
    /**
18
     * Retrieve the JSON fragments
19
     *
20
     * @return Traversable<int, string>
21
     */
22 6
    public function getIterator(): Traversable
23
    {
24 6
        if (!in_array(StreamWrapper::NAME, stream_get_wrappers())) {
25 1
            stream_wrapper_register(StreamWrapper::NAME, StreamWrapper::class);
26
        }
27
28 6
        $stream = fopen(StreamWrapper::NAME . '://stream', 'rb', false, stream_context_create([
29 6
            StreamWrapper::NAME => ['stream' => $this->source],
30 6
        ]));
31
32 6
        return new JsonResource($stream, $this->config);
33
    }
34
35
    /**
36
     * Determine whether the JSON source can be handled
37
     *
38
     * @return bool
39
     */
40 2
    public function matches(): bool
41
    {
42 2
        return $this->source instanceof StreamInterface;
43
    }
44
45
    /**
46
     * Retrieve the calculated size of the JSON source
47
     *
48
     * @return int|null
49
     */
50 1
    protected function calculateSize(): ?int
51
    {
52 1
        return $this->source->getSize();
53
    }
54
}
55