Passed
Pull Request — master (#47)
by Anatoly
35:19
created

FormData::__construct()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 11
ccs 0
cts 7
cp 0
rs 10
cc 2
nc 1
nop 1
crap 6
1
<?php
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Nekhay <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Nekhay
8
 * @license https://github.com/sunrise-php/http-client-curl/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-client-curl
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sunrise\Http\Client\Curl;
15
16
use CURLFile;
17
use Psr\Http\Message\StreamInterface;
18
use Sunrise\Http\Message\Exception\RuntimeException;
19
20
use function array_walk_recursive;
21
22
/**
23
 * @since 2.2.0
24
 */
25
final class FormData implements StreamInterface
26
{
27
    /**
28
     * @var array<array-key, mixed>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, mixed> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, mixed>.
Loading history...
29
     */
30
    public readonly array $data;
31
32
    /**
33
     * @param array<array-key, mixed> $data
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, mixed> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, mixed>.
Loading history...
34
     */
35
    public function __construct(array $data)
36
    {
37
        array_walk_recursive($data, static function (mixed &$value): void {
38
            if ($value instanceof StreamInterface) {
39
                /** @var string $uri */
40
                $uri = $value->getMetadata('uri');
41
                $value = new CURLFile($uri);
42
            }
43
        });
44
45
        $this->data = $data;
0 ignored issues
show
Bug introduced by
The property data is declared read-only in Sunrise\Http\Client\Curl\FormData.
Loading history...
46
    }
47
48
    public function __toString(): string
49
    {
50
        return '';
51
    }
52
53
    public function close(): void
54
    {
55
    }
56
57
    public function detach(): mixed
58
    {
59
        return null;
60
    }
61
62
    public function getSize(): ?int
63
    {
64
        return null;
65
    }
66
67
    public function tell(): int
68
    {
69
        throw new RuntimeException('Stream does not support the tell operation');
70
    }
71
72
    public function eof(): bool
73
    {
74
        return true;
75
    }
76
77
    public function isSeekable(): bool
78
    {
79
        return false;
80
    }
81
82
    public function seek(int $offset, int $whence = SEEK_SET): void
83
    {
84
        throw new RuntimeException('Stream is not seekable');
85
    }
86
87
    public function rewind(): void
88
    {
89
        throw new RuntimeException('Stream is not seekable');
90
    }
91
92
    public function isWritable(): bool
93
    {
94
        return false;
95
    }
96
97
    public function write(string $string): int
98
    {
99
        throw new RuntimeException('Stream is not writable');
100
    }
101
102
    public function isReadable(): bool
103
    {
104
        return false;
105
    }
106
107
    public function read(int $length): string
108
    {
109
        throw new RuntimeException('Stream is not readable');
110
    }
111
112
    public function getContents(): string
113
    {
114
        throw new RuntimeException('Stream is not readable');
115
    }
116
117
    public function getMetadata(?string $key = null): mixed
118
    {
119
        return $key === null ? [] : null;
120
    }
121
}
122