Issues (55)

src/HttpResourceObject.php (4 issues)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource;
6
7
use function strtoupper;
8
9
/**
10
 * @method HttpResourceObject get(AbstractUri|string $uri, array $params = [])
11
 * @method HttpResourceObject head(AbstractUri|string $uri, array $params = [])
12
 * @method HttpResourceObject put(AbstractUri|string $uri, array $params = [])
13
 * @method HttpResourceObject post(AbstractUri|string $uri, array $params = [])
14
 * @method HttpResourceObject patch(AbstractUri|string $uri, array $params = [])
15
 * @method HttpResourceObject delete(AbstractUri|string $uri, array $params = [])
16
 * @property-read string                $code
17
 * @property-read array<string, string> $headers
18
 * @property-read array<string, string> $body
19
 * @property-read string                $view
20
 */
21
final class HttpResourceObject extends ResourceObject implements InvokeRequestInterface
22
{
23
    public function __construct(
24
        private readonly HttpRequestInterface $httpRequest,
25
    ) {
26
    }
27
28
    /** @SuppressWarnings(PHPMD.CamelCaseMethodName) */
29
    public function _invokeRequest(InvokerInterface $invoker, AbstractRequest $request): ResourceObject
30
    {
31
        unset($invoker);
32
33
        return $this->request($request);
34
    }
35
36
    public function request(AbstractRequest $request): ResourceObject
37
    {
38
        $uri = $request->resourceObject->uri;
39
        $method = strtoupper($uri->method);
40
        [
41
            'code' => $this->code,
0 ignored issues
show
The property code is declared read-only in BEAR\Resource\HttpResourceObject.
Loading history...
42
            'headers' => $this->headers,
0 ignored issues
show
The property headers is declared read-only in BEAR\Resource\HttpResourceObject.
Loading history...
43
            'body' => $this->body,
0 ignored issues
show
The property body is declared read-only in BEAR\Resource\HttpResourceObject.
Loading history...
44
            'view' => $this->view,
0 ignored issues
show
The property view is declared read-only in BEAR\Resource\HttpResourceObject.
Loading history...
45
        ] =  $this->httpRequest->request($method, (string) $uri, $uri->query);
46
47
        return $this;
48
    }
49
50
    public function __toString(): string
51
    {
52
        return $this->view;
53
    }
54
}
55