Passed
Pull Request — 1.x (#296)
by Akihito
14:13
created

HttpResourceObject   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 10
Bugs 1 Features 0
Metric Value
eloc 11
c 10
b 1
f 0
dl 0
loc 31
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 3 1
A request() 0 12 1
A _invokeRequest() 0 5 1
A __construct() 0 3 1
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 HttpRequestInterface $httpRequest,
25
    ) {
26
    }
27
28
    public function _invokeRequest(InvokerInterface $invoker, AbstractRequest $request): ResourceObject
29
    {
30
        unset($invoker);
31
32
        return $this->request($request);
33
    }
34
35
    public function request(AbstractRequest $request): ResourceObject
36
    {
37
        $uri = $request->resourceObject->uri;
38
        $method = strtoupper($uri->method);
39
        [
40
            'code' => $this->code,
0 ignored issues
show
Bug introduced by
The property code is declared read-only in BEAR\Resource\HttpResourceObject.
Loading history...
41
            'headers' => $this->headers,
0 ignored issues
show
Bug introduced by
The property headers is declared read-only in BEAR\Resource\HttpResourceObject.
Loading history...
42
            'body' => $this->body,
0 ignored issues
show
Bug introduced by
The property body is declared read-only in BEAR\Resource\HttpResourceObject.
Loading history...
43
            'view' => $this->view,
0 ignored issues
show
Bug introduced by
The property view is declared read-only in BEAR\Resource\HttpResourceObject.
Loading history...
44
        ] =  $this->httpRequest->request($method, (string) $uri, $uri->query);
45
46
        return $this;
47
    }
48
49
    public function __toString(): string
50
    {
51
        return $this->view;
52
    }
53
}
54