Issues (108)

src/HttpResourceObject.php (5 issues)

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
        ] =  $this->httpRequest->request($method, (string) $uri, /** @scrutinizer ignore-type */ $uri->query);
Loading history...
50
51
        return $this;
52
    }
53
54
    public function __toString(): string
55
    {
56
        return $this->view;
57
    }
58
}
59