HttpResourceObject   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 8
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 33
rs 10
c 8
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 3 1
A request() 0 12 1
A _invokeRequest() 0 6 1
A __construct() 0 3 1
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, Query $params = [])
13
 * @method HttpResourceObject head(AbstractUri|string $uri, Query $params = [])
14
 * @method HttpResourceObject put(AbstractUri|string $uri, Query $params = [])
15
 * @method HttpResourceObject post(AbstractUri|string $uri, Query $params = [])
16
 * @method HttpResourceObject patch(AbstractUri|string $uri, Query $params = [])
17
 * @method HttpResourceObject delete(AbstractUri|string $uri, Query $params = [])
18
 * @property-read int        $code
19
 * @property-read Headers    $headers
20
 * @property-read HttpBody   $body
21
 * @property-read string     $view
22
 * @psalm-import-type Query from Types
23
 * @psalm-import-type Headers from Types
24
 * @psalm-import-type HttpBody from Types
25
 */
26
final class HttpResourceObject extends ResourceObject implements InvokeRequestInterface
27
{
28
    public function __construct(
29
        private readonly HttpRequestInterface $httpRequest,
30
    ) {
31
    }
32
33
    /** @SuppressWarnings(PHPMD.CamelCaseMethodName) Underscore prefix indicates internal API method */
34
    #[Override]
35
    public function _invokeRequest(InvokerInterface $invoker, AbstractRequest $request): ResourceObject
36
    {
37
        unset($invoker);
38
39
        return $this->request($request);
40
    }
41
42
    public function request(AbstractRequest $request): ResourceObject
43
    {
44
        $uri = $request->resourceObject->uri;
45
        $method = strtoupper($uri->method);
46
        [
47
            'code' => $this->code,
0 ignored issues
show
Bug introduced by
The property code is declared read-only in BEAR\Resource\HttpResourceObject.
Loading history...
48
            'headers' => $this->headers,
0 ignored issues
show
Bug introduced by
The property headers is declared read-only in BEAR\Resource\HttpResourceObject.
Loading history...
49
            'body' => $this->body,
0 ignored issues
show
Bug introduced by
The property body is declared read-only in BEAR\Resource\HttpResourceObject.
Loading history...
50
            'view' => $this->view,
0 ignored issues
show
Bug introduced by
The property view is declared read-only in BEAR\Resource\HttpResourceObject.
Loading history...
51
        ] =  $this->httpRequest->request($method, (string) $uri, $uri->query);
0 ignored issues
show
Bug introduced by
$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

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