Passed
Branch feature/first-release (f6e752)
by Andrea Marco
02:33
created

Psr7Stream   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A shouldHandleSource() 0 3 1
A handleSource() 0 11 2
1
<?php
2
3
namespace Cerbero\LazyJson\Handlers;
4
5
use Cerbero\LazyJson\StreamWrapper;
6
use Psr\Http\Message\StreamInterface;
7
use Traversable;
8
9
/**
10
 * The PSR-7 stream handler.
11
 *
12
 */
13
class Psr7Stream extends AbstractHandler
14
{
15
    /**
16
     * Determine whether the handler should handle the source
17
     *
18
     * @return bool
19
     */
20
    protected function shouldHandleSource(): bool
21
    {
22
        return $this->source instanceof StreamInterface;
23
    }
24
25
    /**
26
     * Handle the source
27
     *
28
     * @return Traversable|null
29
     */
30
    protected function handleSource(): ?Traversable
31
    {
32
        if (!in_array(StreamWrapper::NAME, stream_get_wrappers())) {
33
            stream_wrapper_register(StreamWrapper::NAME, StreamWrapper::class);
34
        }
35
36
        $this->source = fopen(StreamWrapper::NAME . '://stream', 'rb', false, stream_context_create([
37
            StreamWrapper::NAME => ['stream' => $this->source],
38
        ]));
39
40
        return null;
41
    }
42
}
43