Passed
Pull Request — 1.x (#285)
by
unknown
11:40
created

HttpResourceObject::formatHeader()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource;
6
7
use BadFunctionCallException;
8
use InvalidArgumentException;
9
use Symfony\Contracts\HttpClient\HttpClientInterface;
10
use Symfony\Contracts\HttpClient\ResponseInterface;
11
12
use function count;
13
use function is_array;
14
use function strtoupper;
15
use function ucwords;
16
17
/**
18
 * @method HttpResourceObject get(AbstractUri|string $uri, array $params = [])
19
 * @method HttpResourceObject head(AbstractUri|string $uri, array $params = [])
20
 * @method HttpResourceObject put(AbstractUri|string $uri, array $params = [])
21
 * @method HttpResourceObject post(AbstractUri|string $uri, array $params = [])
22
 * @method HttpResourceObject patch(AbstractUri|string $uri, array $params = [])
23
 * @method HttpResourceObject delete(AbstractUri|string $uri, array $params = [])
24
 * @property-read string                $code
25
 * @property-read array<string, string> $headers
26
 * @property-read array<string, string> $body
27
 * @property-read string                $view
28
 */
29
final class HttpResourceObject extends ResourceObject
30
{
31
    /** {@inheritDoc} */
32
    public $body;
33
34
    /** @psalm-suppress PropertyNotSetInConstructor */
35
    private ResponseInterface $response;
36
37
    public function __construct(
38
        private HttpClientInterface $client,
39
    ) {
40
        unset($this->code, $this->headers, $this->body, $this->view);
41
    }
42
43
    /**
44
     * @param 'code'|'headers'|'body'|'view'|string $name
0 ignored issues
show
Documentation Bug introduced by
The doc comment 'code'|'headers'|'body'|'view'|string at position 0 could not be parsed: Unknown type name ''code'' at position 0 in 'code'|'headers'|'body'|'view'|string.
Loading history...
45
     *
46
     * @return array<int|string, mixed>|int|string
47
     */
48
    public function __get(string $name): array|int|string
49
    {
50
        if ($name === 'code') {
51
            return $this->response->getStatusCode();
52
        }
53
54
        if ($name === 'headers') {
55
            /** @var array<string, array<string>> $headers */
56
            $headers = $this->response->getHeaders();
57
58
            return $this->formatHeader($headers);
59
        }
60
61
        if ($name === 'body') {
62
            return $this->response->toArray();
63
        }
64
65
        if ($name === 'view') {
66
            return $this->response->getContent();
67
        }
68
69
        throw new InvalidArgumentException($name);
70
    }
71
72
    /**
73
     * @param array<string, array<string>> $headers
74
     *
75
     * @return array<string, string|array<string>>
76
     */
77
    private function formatHeader(array $headers): array
78
    {
79
        $formated = [];
80
        foreach ($headers as $key => $header) {
81
            $ucFirstKey = ucwords($key);
82
            $formated[$ucFirstKey] = count($header) === 1 ? $header[0] : $header;
83
        }
84
85
        return $formated;
86
    }
87
88
    public function __set(string $name, mixed $value): void
89
    {
90
        unset($value);
91
92
        throw new BadFunctionCallException($name);
93
    }
94
95
    public function __isset(string $name): bool
96
    {
97
        return isset($this->{$name});
98
    }
99
100
    public function __toString(): string
101
    {
102
        return $this->response->getContent();
103
    }
104
105
    public function request(AbstractRequest $request): self
106
    {
107
        $uri = $request->resourceObject->uri;
108
        $method = strtoupper($uri->method);
109
        $options = $method === 'GET' ? ['query' => $uri->query] : ['body' => $uri->query];
110
        $clientOptions = isset($uri->query['_options']) && is_array($uri->query['_options']) ? $uri->query['_options'] : [];
111
        $options += $clientOptions;
112
        $this->response = $this->client->request($method, (string) $uri, $options);
113
114
        return $this;
115
    }
116
}
117