Resource   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Test Coverage

Coverage 93.02%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
c 1
b 0
f 0
dl 0
loc 154
rs 10
ccs 40
cts 43
cp 0.9302
wmc 15

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __get() 0 5 1
A __construct() 0 7 1
A newInstance() 0 8 2
A uri() 0 10 1
A object() 0 4 1
A get() 0 4 1
A post() 0 4 1
A href() 0 10 1
A options() 0 4 1
A methodUri() 0 5 1
A patch() 0 4 1
A delete() 0 4 1
A head() 0 4 1
A put() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource;
6
7
use BEAR\Resource\Exception\MethodException;
8
use Override;
9
10
use function assert;
11
use function is_string;
12
13
/**
14
 * @property $this $get
15
 * @property $this $post
16
 * @property $this $put
17
 * @property $this $patch
18
 * @property $this $delete
19
 * @property $this $head
20
 * @property $this $options
21
 * @psalm-import-type Query from Types
22
 */
23
final class Resource implements ResourceInterface
24
{
25
    /** @psalm-suppress PropertyNotSetInConstructor */
26
    private Request $request;
27
    private string $method = 'get';
28
    /** @noinspection MoreThanThreeArgumentsInspection */
29
30
    /**
31
     * @param FactoryInterface $factory Resource factory
32
     * @param InvokerInterface $invoker Resource request invoker
33
     * @param AnchorInterface  $anchor  Resource anchor
34
     * @param LinkerInterface  $linker  Resource linker
35
     * @param UriFactory       $uri     URI factory
36
     */
37
    public function __construct(
38
        private readonly FactoryInterface $factory,
39
        private readonly InvokerInterface $invoker,
40
        private readonly AnchorInterface $anchor,
41
        private readonly LinkerInterface $linker,
42
        private readonly UriFactory $uri,
43
    ) {
44
    }
45
46
    public function __get(string $name): self
47
    {
48
        $this->method = $name;
49
50
        return $this;
51
    }
52
53
    /**
54
     * {@inheritDoc}
55
     */
56
    #[Override]
57
    public function newInstance($uri): ResourceObject
58
    {
59
        if (is_string($uri)) {
60
            $uri = ($this->uri)($uri);
61
        }
62
63
        return $this->factory->newInstance($uri);
64
    }
65
66 47
    /**
67
     * {@inheritDoc}
68
     *
69
     * @throws MethodException
70
     */
71
    #[Override]
72 47
    public function object(ResourceObject $ro): RequestInterface
73 47
    {
74 47
        return new Request($this->invoker, $ro, $this->method);
75 47
    }
76 47
77
    /**
78
     * {@inheritDoc}
79
     */
80
    #[Override]
81
    public function uri($uri): RequestInterface
82
    {
83 34
        $method = $this->method; // save method, this may change on newInstance(), this is singleton!
84
        $this->method = 'get';
85 34
        $ro = $this->newInstance($uri);
86
        $ro->uri->method = $method;
87 34
        $this->request = new Request($this->invoker, $ro, $ro->uri->method, $ro->uri->query, [], $this->linker);
0 ignored issues
show
Bug introduced by
$ro->uri->query of type BEAR\Resource\Query is incompatible with the type array expected by parameter $query of BEAR\Resource\Request::__construct(). ( Ignorable by Annotation )

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

87
        $this->request = new Request($this->invoker, $ro, $ro->uri->method, /** @scrutinizer ignore-type */ $ro->uri->query, [], $this->linker);
Loading history...
88
89
        return $this->request;
90
    }
91
92
    /**
93 40
     * {@inheritDoc}
94
     *
95 40
     * @psalm-suppress MixedPropertyFetch
96
     */
97
    #[Override]
98
    public function href(string $rel, array $query = []): ResourceObject
99
    {
100
        [$method, $uri] = $this->anchor->href($rel, $this->request, $query);
101
        // Dynamic property access via magic __get() returns mixed
102
        /** @psalm-suppress MixedMethodCall */
103 1
        $resourceObject = $this->{$method}->uri($uri)->addQuery($query)->eager->request(); // @phpstan-ignore-line
104
        assert($resourceObject instanceof ResourceObject);
105 1
106
        return $resourceObject;
107
    }
108
109
    /**
110
     * {@inheritDoc}
111 39
     */
112
    #[Override]
113 39
    public function get(string $uri, array $query = []): ResourceObject
114 34
    {
115
        return $this->methodUri(Request::GET, $uri)($query);
116 39
    }
117 39
118 39
    /**
119 39
     * {@inheritDoc}
120 39
     */
121
    #[Override]
122 39
    public function post(string $uri, array $query = []): ResourceObject
123
    {
124
        return $this->methodUri(Request::POST, $uri)($query);
125
    }
126
127
    /**
128 3
     * {@inheritDoc}
129
     */
130 3
    #[Override]
131
    public function put(string $uri, array $query = []): ResourceObject
132 3
    {
133
        return $this->methodUri(Request::PUT, $uri)($query);
134
    }
135 1
136
    /**
137 1
     * {@inheritDoc}
138
     */
139 1
    #[Override]
140
    public function patch(string $uri, array $query = []): ResourceObject
141
    {
142 1
        return $this->methodUri(Request::PATCH, $uri)($query);
143
    }
144 1
145
    /**
146 1
     * {@inheritDoc}
147
     */
148
    #[Override]
149 1
    public function delete(string $uri, array $query = []): ResourceObject
150
    {
151 1
        return $this->methodUri(Request::DELETE, $uri)($query);
152
    }
153 1
154
    /**
155
     * {@inheritDoc}
156 1
     */
157
    #[Override]
158 1
    public function options(string $uri, array $query = []): ResourceObject
159
    {
160 1
        return $this->methodUri(Request::OPTIONS, $uri)($query);
161
    }
162
163 1
    /**
164
     * {@inheritDoc}
165 1
     */
166
    #[Override]
167 1
    public function head(string $uri, array $query = []): ResourceObject
168
    {
169
        return $this->methodUri(Request::HEAD, $uri)($query);
170
    }
171
172
    private function methodUri(string $method, string $uri): RequestInterface
173
    {
174
        $this->method = $method;
175
176
        return $this->uri($uri);
177
    }
178
}
179