Passed
Pull Request — master (#95)
by Dmitriy
03:04
created

StreamHandler::withEncoder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\VarDumper\Handler;
6
7
use RuntimeException;
8
use Yiisoft\VarDumper\HandlerInterface;
9
10
final class StreamHandler implements HandlerInterface
11
{
12
    /**
13
     * @var callable|null
14
     */
15
    private $encoder = null;
16
    /**
17
     * @var resource|null
18
     */
19
    private $stream = null;
20
21
    /**
22
     * @var resource|string|null
23
     */
24
    private $uri = null;
25
26
    /**
27
     * @param mixed|resource|string $uri
28
     */
29 10
    public function __construct(
30
        $uri = 'udp://127.0.0.1:8890'
31
    ) {
32 10
        if (!is_string($uri) && !is_resource($uri)) {
33
            throw new \InvalidArgumentException(
34
                sprintf(
35
                    'Argument $uri must be a string or a resource, "%s" given.',
36
                    gettype($uri)
37
                )
38
            );
39
        }
40 10
        $this->uri = $uri;
41
    }
42
43
    /**
44
     * Encodes with {@see self::$encoder} {@param $variable} and sends the result to the stream.
0 ignored issues
show
Bug introduced by
The type Yiisoft\VarDumper\Handler\and was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
45
     */
46 10
    public function handle(mixed $variable, int $depth, bool $highlight = false): void
47
    {
48 10
        $data = ($this->encoder ?? 'json_encode')($variable);
49 10
        if (!is_string($data)) {
50
            throw new RuntimeException(
51
                sprintf(
52
                    'Encoder must return a string, %s returned.',
53
                    gettype($data)
54
                )
55
            );
56
        }
57
58 10
        $this->initializeStream();
59
60 10
        if (!$this->writeToStream($data)) {
61
            $this->initializeStream();
62
63
            $this->writeToStream($data);
64
        }
65
    }
66
67 1
    public function withEncoder(callable $encoder): HandlerInterface
68
    {
69 1
        $new = clone $this;
70 1
        $new->encoder = $encoder;
71 1
        return $new;
72
    }
73
74 10
    private function initializeStream(): void
75
    {
76 10
        if (is_string($this->uri)) {
77 1
            $this->stream = fsockopen($this->uri);
78
        } else {
79 9
            $this->stream = $this->uri;
80
        }
81
82 10
        if (!is_resource($this->stream)) {
83 1
            throw new RuntimeException('Cannot initialize a stream.');
84
        }
85
    }
86
87 10
    private function writeToStream(string $data): bool
88
    {
89 10
        if ($this->stream === null) {
90
            return false;
91
        }
92 10
        return @fwrite($this->stream, $data) !== false;
93
    }
94
}
95