Psr7Stream   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 11 2
A handles() 0 3 1
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 Resource
14
{
15
    /**
16
     * Determine whether the handler can handle the given source
17
     *
18
     * @param mixed $source
19
     * @return bool
20
     */
21 3
    public function handles($source): bool
22
    {
23 3
        return $source instanceof StreamInterface;
24
    }
25
26
    /**
27
     * Handle the given source
28
     *
29
     * @param mixed $source
30
     * @param string $path
31
     * @return Traversable
32
     */
33 5
    public function handle($source, string $path): Traversable
34
    {
35 5
        if (!in_array(StreamWrapper::NAME, stream_get_wrappers())) {
36 3
            stream_wrapper_register(StreamWrapper::NAME, StreamWrapper::class);
37
        }
38
39 5
        $stream = fopen(StreamWrapper::NAME . '://stream', 'rb', false, stream_context_create([
40 5
            StreamWrapper::NAME => ['stream' => $source],
41
        ]));
42
43 5
        return parent::handle($stream, $path);
44
    }
45
}
46