Body::eof()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace kalanis\google_maps\Remote;
4
5
6
use Psr\Http\Message\StreamInterface;
7
use RuntimeException;
8
9
10
/**
11
 * Simple class which represents body as stream
12
 */
13
class Body implements StreamInterface
14
{
15
    protected int $pointer = 0;
16
17 47
    public function __construct(
18
        protected string $content = '',
19
    )
20
    {
21 47
    }
22
23 7
    public function __toString(): string
24
    {
25 7
        return $this->content;
26
    }
27
28 2
    public function close(): void
29
    {
30 2
    }
31
32
    /**
33
     * @return resource|null
34
     */
35 2
    public function detach()
36
    {
37 2
        return null;
38
    }
39
40 2
    public function tell(): int
41
    {
42 2
        return $this->pointer;
43
    }
44
45 2
    public function seek(int $offset, int $whence = SEEK_SET): void
46
    {
47 1
        $this->pointer = match ($whence) {
48 2
            SEEK_SET => $offset,
49 2
            SEEK_CUR => min($this->getSize(), $this->pointer + $offset),
50 2
            SEEK_END => $this->getSize(),
51 1
            default => throw new RuntimeException('Bad seek mode!')
52 1
        };
53
    }
54
55 3
    public function getSize(): int
56
    {
57 3
        return strlen($this->content);
58
    }
59
60 2
    public function rewind(): void
61
    {
62 2
        $this->pointer = 0;
63
    }
64
65 1
    public function isWritable(): bool
66
    {
67 1
        return true;
68
    }
69
70 1
    public function write(string $string): int
71
    {
72 1
        $this->content .= $string;
73 1
        return strlen($string);
74
    }
75
76 1
    public function isReadable(): bool
77
    {
78 1
        return true;
79
    }
80
81 1
    public function read(int $length): string
82
    {
83 1
        $newPos = $this->pointer + $length;
84 1
        $toReturn = substr($this->content, $this->pointer, $length);
85 1
        $this->pointer = min($newPos, $this->getSize());
86 1
        return $toReturn;
87
    }
88
89 41
    public function getContents(): string
90
    {
91 41
        return substr($this->content, $this->pointer);
92
    }
93
94
    /**
95
     * @param string|null $key
96
     * @return array|mixed|null
97
     */
98 1
    public function getMetadata(?string $key = null)
99
    {
100 1
        $available = [
101 1
            'timed_out' => false,
102 1
            'blocked' => false,
103 1
            'eof' => $this->eof(),
104 1
            'seekable' => $this->isSeekable(),
105 1
        ];
106 1
        if (!is_null($key)) {
107 1
            return $available[$key] ?? null;
108
        }
109 1
        return $available;
110
    }
111
112 3
    public function eof(): bool
113
    {
114 3
        return $this->getSize() <= $this->pointer;
115
    }
116
117 2
    public function isSeekable(): bool
118
    {
119 2
        return true;
120
    }
121
}
122