Passed
Push — master ( 65b336...1323cd )
by Бабичев
44s
created

Resolver::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Bavix\Router;
4
5
class Resolver implements GroupResolution
6
{
7
8
    /**
9
     * @var callable
10
     */
11
    protected $collections;
12
13
    /**
14
     * @var callable
15
     */
16
    protected $patterns;
17
18
    /**
19
     * Resolver constructor.
20
     *
21
     * @param callable $patterns
22
     * @param callable $collections
23
     */
24
    public function __construct(callable $patterns, callable $collections)
25
    {
26
        $this->collections = $collections;
27
        $this->patterns = $patterns;
28
    }
29
30
    /**
31
     * @param ResourceCollection $collection
32
     *
33
     * @return ResourceCollection
34
     */
35
    protected function pushCollection(ResourceCollection $collection): ResourceCollection
36
    {
37
        return \call_user_func($this->collections, $collection);
38
    }
39
40
    /**
41
     * @param Pattern $pattern
42
     *
43
     * @return Pattern
44
     */
45
    protected function pushPattern(Pattern $pattern): Pattern
46
    {
47
        return \call_user_func($this->patterns, $pattern);
48
    }
49
50
    /**
51
     * @param string $path
52
     * @param null|string $name
53
     * @return Pattern
54
     */
55
    protected function pattern(string $path, ?string $name): Pattern
56
    {
57
        return new Pattern($path, $name);
58
    }
59
60
    /**
61
     * @param array       $methods
62
     * @param string      $path
63
     * @param null|string $name
64
     *
65
     * @return Pattern
66
     */
67
    public function methods(array $methods, string $path, ?string $name): Pattern
68
    {
69
        return $this->pushPattern($this->pattern($path, $name))->methods($methods);
70
    }
71
72
    /**
73
     * GET|POST|PUT|PATCH|HEAD|OPTIONS|DELETE
74
     *
75
     * @param string      $path
76
     * @param null|string $name
77
     *
78
     * @return Pattern
79
     */
80
    public function any(string $path, ?string $name = null): Pattern
81
    {
82
        return $this->pushPattern($this->pattern($path, $name))->any();
83
    }
84
85
    /**
86
     * @param string      $path
87
     * @param null|string $name
88
     *
89
     * @return Pattern
90
     */
91
    public function get(string $path, ?string $name = null): Pattern
92
    {
93
        return $this->pushPattern($this->pattern($path, $name))->get();
94
    }
95
96
    /**
97
     * @param string      $path
98
     * @param null|string $name
99
     *
100
     * @return Pattern
101
     */
102
    public function post(string $path, ?string $name = null): Pattern
103
    {
104
        return $this->pushPattern($this->pattern($path, $name))->post();
105
    }
106
107
    /**
108
     * @param string      $path
109
     * @param null|string $name
110
     *
111
     * @return Pattern
112
     */
113
    public function put(string $path, ?string $name = null): Pattern
114
    {
115
        return $this->pushPattern($this->pattern($path, $name))->put();
116
    }
117
118
    /**
119
     * @param string      $path
120
     * @param null|string $name
121
     *
122
     * @return Pattern
123
     */
124
    public function patch(string $path, ?string $name = null): Pattern
125
    {
126
        return $this->pushPattern($this->pattern($path, $name))->patch();
127
    }
128
129
    /**
130
     * @param string      $path
131
     * @param null|string $name
132
     *
133
     * @return Pattern
134
     */
135
    public function delete(string $path, ?string $name = null): Pattern
136
    {
137
        return $this->pushPattern($this->pattern($path, $name))->delete();
138
    }
139
140
    /**
141
     * entityName -> /users
142
     *
143
     *  GET         users.index     /users
144
     *
145
     * @param string $entityName
146
     * @param null|string $name
147
     * @return Pattern
148
     */
149
    protected function _index(string $entityName, ?string $name = null): Pattern
150
    {
151
        return $this->pattern(
152
            $entityName,
153
            $this->name($name, 'index')
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type null; however, parameter $name of Bavix\Router\Resolver::name() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

153
            $this->name(/** @scrutinizer ignore-type */ $name, 'index')
Loading history...
154
        )->get();
155
    }
156
157
    /**
158
     * @param string $entityName
159
     * @param null|string $name
160
     * @return Pattern
161
     */
162
    public function index(string $entityName, ?string $name = null): Pattern
163
    {
164
        return $this->pushPattern($this->_index($entityName, $name));
165
    }
166
167
    /**
168
     * entityName -> /users
169
     *
170
     *  GET         users.create    /users/create
171
     *
172
     * @param string $entityName
173
     * @param null|string $name
174
     * @return Pattern
175
     */
176
    protected function _create(string $entityName, ?string $name = null): Pattern
177
    {
178
        return $this->pattern(
179
            $this->action($entityName, 'create'),
180
            $this->name($name, 'create')
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type null; however, parameter $name of Bavix\Router\Resolver::name() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

180
            $this->name(/** @scrutinizer ignore-type */ $name, 'create')
Loading history...
181
        )->get();
182
    }
183
184
    /**
185
     * @param string $entityName
186
     * @param null|string $name
187
     * @return Pattern
188
     */
189
    public function create(string $entityName, ?string $name = null): Pattern
190
    {
191
        return $this->pushPattern($this->_create($entityName, $name));
192
    }
193
194
    /**
195
     * entityName -> /users
196
     *
197
     *  POST        users.store     /users
198
     *
199
     * @param string $entityName
200
     * @param null|string $name
201
     * @return Pattern
202
     */
203
    protected function _store(string $entityName, ?string $name = null): Pattern
204
    {
205
        return $this->pattern(
206
            $entityName,
207
            $this->name($name, 'store')
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type null; however, parameter $name of Bavix\Router\Resolver::name() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

207
            $this->name(/** @scrutinizer ignore-type */ $name, 'store')
Loading history...
208
        )->post();
209
    }
210
211
    /**
212
     * @param string $entityName
213
     * @param null|string $name
214
     * @return Pattern
215
     */
216
    public function store(string $entityName, ?string $name = null): Pattern
217
    {
218
        return $this->pushPattern($this->_store($entityName, $name));
219
    }
220
221
    /**
222
     * entityName -> /users
223
     *
224
     *  GET         users.show      /users/{id}
225
     *
226
     * @param string $entityName
227
     * @param null|string $name
228
     * @param null|string $id
229
     * @return Pattern
230
     */
231
    protected function _show(string $entityName, ?string $name = null, ?string $id = null): Pattern
232
    {
233
        return $this->pattern(
234
            $this->id($entityName, $id),
0 ignored issues
show
Bug introduced by
It seems like $id can also be of type null; however, parameter $id of Bavix\Router\Resolver::id() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

234
            $this->id($entityName, /** @scrutinizer ignore-type */ $id),
Loading history...
235
            $this->name($name, 'show')
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type null; however, parameter $name of Bavix\Router\Resolver::name() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

235
            $this->name(/** @scrutinizer ignore-type */ $name, 'show')
Loading history...
236
        )->get();
237
    }
238
239
    /**
240
     * @param string $entityName
241
     * @param null|string $name
242
     * @param null|string $id
243
     * @return Pattern
244
     */
245
    public function show(string $entityName, ?string $name = null, ?string $id = null): Pattern
246
    {
247
        return $this->pushPattern($this->_show($entityName, $name, $id));
248
    }
249
250
    /**
251
     * entityName -> /users
252
     *
253
     *  GET         users.edit      /users/{id}/edit
254
     *
255
     * @param string $entityName
256
     * @param null|string $name
257
     * @param null|string $id
258
     * @return Pattern
259
     */
260
    protected function _edit(string $entityName, ?string $name = null, ?string $id = null): Pattern
261
    {
262
        return $this->pattern(
263
            $this->idAction($entityName, $id, 'edit'),
0 ignored issues
show
Bug introduced by
It seems like $id can also be of type null; however, parameter $id of Bavix\Router\Resolver::idAction() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

263
            $this->idAction($entityName, /** @scrutinizer ignore-type */ $id, 'edit'),
Loading history...
264
            $this->name($name, 'edit')
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type null; however, parameter $name of Bavix\Router\Resolver::name() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

264
            $this->name(/** @scrutinizer ignore-type */ $name, 'edit')
Loading history...
265
        )->get();
266
    }
267
268
    /**
269
     * @param string $entityName
270
     * @param null|string $name
271
     * @param null|string $id
272
     * @return Pattern
273
     */
274
    public function edit(string $entityName, ?string $name = null, ?string $id = null): Pattern
275
    {
276
        return $this->pushPattern($this->_edit($entityName, $name, $id));
277
    }
278
279
    /**
280
     * entityName -> /users
281
     *
282
     *  PUT/PATCH   users.update    /users/{id}/edit
283
     *
284
     * @param string $entityName
285
     * @param null|string $name
286
     * @param null|string $id
287
     * @return Pattern
288
     */
289
    protected function _update(string $entityName, ?string $name = null, ?string $id = null): Pattern
290
    {
291
        return $this->pattern(
292
            $this->idAction($entityName, $id, 'edit'),
0 ignored issues
show
Bug introduced by
It seems like $id can also be of type null; however, parameter $id of Bavix\Router\Resolver::idAction() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

292
            $this->idAction($entityName, /** @scrutinizer ignore-type */ $id, 'edit'),
Loading history...
293
            $this->name($name, 'update')
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type null; however, parameter $name of Bavix\Router\Resolver::name() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

293
            $this->name(/** @scrutinizer ignore-type */ $name, 'update')
Loading history...
294
        )->setMethods(['PUT', 'PATCH']);
295
    }
296
297
    /**
298
     * @param string $entityName
299
     * @param null|string $name
300
     * @param null|string $id
301
     * @return Pattern
302
     */
303
    public function update(string $entityName, ?string $name = null, ?string $id = null): Pattern
304
    {
305
        return $this->pushPattern($this->_update($entityName, $name, $id));
306
    }
307
308
    /**
309
     * entityName -> /users
310
     *
311
     *  DELETE      users.destroy   /users/{id}
312
     *
313
     * @param string $entityName
314
     * @param null|string $name
315
     * @param null|string $id
316
     * @return Pattern
317
     */
318
    protected function _destroy(string $entityName, ?string $name = null, ?string $id = null): Pattern
319
    {
320
        return $this->pattern(
321
            $this->id($entityName, $id),
0 ignored issues
show
Bug introduced by
It seems like $id can also be of type null; however, parameter $id of Bavix\Router\Resolver::id() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

321
            $this->id($entityName, /** @scrutinizer ignore-type */ $id),
Loading history...
322
            $this->name($name, 'destroy')
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type null; however, parameter $name of Bavix\Router\Resolver::name() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

322
            $this->name(/** @scrutinizer ignore-type */ $name, 'destroy')
Loading history...
323
        )->delete();
324
    }
325
326
    /**
327
     * @param string $entityName
328
     * @param null|string $name
329
     * @param null|string $id
330
     * @return Pattern
331
     */
332
    public function destroy(string $entityName, ?string $name = null, ?string $id = null): Pattern
333
    {
334
        return $this->pushPattern($this->_destroy($entityName, $name, $id));
335
    }
336
337
    /**
338
     * @param string $name
339
     * @param string $index
340
     * @return string
341
     */
342
    protected function name(string $name, string $index): string
343
    {
344
        return $name . '.' . $index;
345
    }
346
347
    /**
348
     * @param string $entityName
349
     * @param string $action
350
     * @return string
351
     */
352
    protected function action(string $entityName, string $action): string
353
    {
354
        return $entityName . '/' . $action;
355
    }
356
357
    /**
358
     * @param string $entityName
359
     * @param string $id
360
     * @return string
361
     */
362
    protected function id(string $entityName, string $id): string
363
    {
364
        return $this->action($entityName, '<' . $id . '>');
365
    }
366
367
    /**
368
     * @param string $entityName
369
     * @param string $id
370
     * @param string $action
371
     * @return string
372
     */
373
    protected function idAction(string $entityName, string $id, string $action): string
374
    {
375
        return $this->action($this->id($entityName, $id), $action);
376
    }
377
378
    /**
379
     * entityName -> /users
380
     *
381
     *  GET         users.index     /users
382
     *  GET         users.create    /users/create
383
     *  POST        users.store     /users
384
     *  GET         users.show      /users/{id}
385
     *  GET         users.edit      /users/{id}/edit
386
     *  PUT/PATCH   users.update    /users/{id}/edit
387
     *  DELETE      users.destroy   /users/{id}
388
     *
389
     * @param string      $entityName
390
     * @param null|string $name
391
     * @param null|string $id
392
     *
393
     * @return ResourceCollection
394
     */
395
    public function resource(string $entityName, ?string $name = null, ?string $id = null): ResourceCollection
396
    {
397
        $entityName = \rtrim($entityName, '/');
398
        $name = $name ?: \ltrim($entityName, '/');
399
        $id = $id ?: $name;
400
401
        return $this->pushCollection(new ResourceCollection([
402
            'index' => $this->_index($entityName, $name),
403
            'create' => $this->_create($entityName, $name),
404
            'store' => $this->_store($entityName, $name),
405
            'show' => $this->_show($entityName, $name, $id),
406
            'edit' => $this->_edit($entityName, $name, $id),
407
            'update' => $this->_update($entityName, $name, $id),
408
            'destroy' => $this->_destroy($entityName, $name, $id)
409
        ]));
410
    }
411
412
    /**
413
     * @param string      $entityName
414
     * @param null|string $name
415
     * @param null|string $id
416
     *
417
     * @return ResourceCollection
418
     */
419
    public function apiResource(string $entityName, ?string $name = null, ?string $id = null): ResourceCollection
420
    {
421
        return $this->resource($entityName, $name, $id)->only([
422
            'index', 'store', 'show', 'update', 'destroy'
423
        ]);
424
    }
425
426
}
427