Passed
Pull Request — master (#95)
by Dmitriy
02:34
created

StreamHandler   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 3
Bugs 1 Features 1
Metric Value
eloc 32
c 3
b 1
f 1
dl 0
loc 83
ccs 33
cts 36
cp 0.9167
rs 10
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
A withEncoder() 0 5 1
A writeToStream() 0 6 2
A initializeStream() 0 10 3
A __construct() 0 12 5
A handle() 0 18 3
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 13
    public function __construct(
30
        mixed $uri = 'udp://127.0.0.1:8890'
31
    ) {
32 13
        if (!is_string($uri) && !is_resource($uri) && !(is_object($uri) && $uri instanceof \Socket)) {
33 1
            throw new \InvalidArgumentException(
34 1
                sprintf(
35 1
                    'Argument $uri must be a string or a resource, "%s" given.',
36 1
                    gettype($uri)
37 1
                )
38 1
            );
39
        }
40 12
        $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 12
    public function handle(mixed $variable, int $depth, bool $highlight = false): void
47
    {
48 12
        $data = ($this->encoder ?? 'json_encode')($variable);
49 12
        if (!is_string($data)) {
50 1
            throw new RuntimeException(
51 1
                sprintf(
52 1
                    'Encoder must return a string, %s returned.',
53 1
                    gettype($data)
54 1
                )
55 1
            );
56
        }
57
58 11
        $this->initializeStream();
59
60 11
        if (!$this->writeToStream($data)) {
61
            $this->initializeStream();
62
63
            $this->writeToStream($data);
64
        }
65
    }
66
67 2
    public function withEncoder(callable $encoder): HandlerInterface
68
    {
69 2
        $new = clone $this;
70 2
        $new->encoder = $encoder;
71 2
        return $new;
72
    }
73
74 11
    private function initializeStream(): void
75
    {
76 11
        if (is_string($this->uri)) {
77 2
            $this->stream = fsockopen($this->uri);
78
        } else {
79 9
            $this->stream = $this->uri;
80
        }
81
82 11
        if (!is_resource($this->stream)) {
83 1
            throw new RuntimeException('Cannot initialize a stream.');
84
        }
85
    }
86
87 11
    private function writeToStream(string $data): bool
88
    {
89 11
        if (!is_resource($this->stream)) {
90
            return false;
91
        }
92 11
        return @fwrite($this->stream, $data) !== false;
93
    }
94
}
95