Psr7Stream::getIterator()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 2
rs 10
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