RestGroupFactory::createDefaultRoutes()   A
last analyzed

Complexity

Conditions 6
Paths 12

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 9
nc 12
nop 1
dl 0
loc 15
rs 9.2222
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Factory;
6
7
use ReflectionClass;
8
use Yiisoft\Http\Method;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Http\Method was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Yiisoft\Router\Group;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Router\Group was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Yiisoft\Router\Route;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Router\Route was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
12
final class RestGroupFactory
13
{
14
    private const ENTITY_PATTERN = '{id:\d+}';
15
16
    private const METHODS = [
17
        'get' => Method::GET,
18
        'list' => Method::GET,
19
        'post' => Method::POST,
20
        'put' => Method::PUT,
21
        'delete' => Method::DELETE,
22
        'patch' => Method::PATCH,
23
        'options' => Method::OPTIONS,
24
    ];
25
26
    public static function create(string $prefix, string $controller): Group
27
    {
28
        return Group::create($prefix)->routes(...self::createDefaultRoutes($controller));
29
    }
30
31
    private static function createDefaultRoutes(string $controller): array
32
    {
33
        $routes = [];
34
        $reflection = new ReflectionClass($controller);
35
        foreach (self::METHODS as $methodName => $httpMethod) {
36
            if ($reflection->hasMethod($methodName)) {
37
                $pattern = ($methodName === 'list' || $methodName === 'post') ? '' : self::ENTITY_PATTERN;
38
                $routes[] = Route::methods([$httpMethod], $pattern)->action([$controller, $methodName]);
39
            }
40
        }
41
        if ($reflection->hasMethod('options')) {
42
            $routes[] = Route::methods([Method::OPTIONS], '')->action([$controller, 'options']);
43
        }
44
45
        return $routes;
46
    }
47
}
48