Passed
Pull Request — master (#73)
by Dmitriy
12:08
created

AppRouterFactory::__invoke()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 92
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 18
Bugs 2 Features 0
Metric Value
eloc 61
c 18
b 2
f 0
dl 0
loc 92
rs 8.5397
cc 5
nc 1
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Factory;
4
5
use App\Blog\Archive\ArchiveController;
6
use App\Blog\BlogController;
7
use App\Blog\Post\PostController;
8
use App\Blog\Tag\TagController;
9
use App\Controller\ApiInfo;
10
use App\Controller\ApiUserController;
11
use App\Controller\AuthController;
12
use App\Controller\ContactController;
13
use App\Controller\SiteController;
14
use App\Controller\UserController;
15
use Yiisoft\Yii\Web\Formatter\JsonResponseFormatter;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Yii\Web\Formatter\JsonResponseFormatter 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...
16
use Yiisoft\Yii\Web\WebResponse;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Yii\Web\WebResponse 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...
17
use Yiisoft\Yii\Web\Middleware\WebResponseFormatter;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Yii\Web\Middleware\WebResponseFormatter 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...
18
use Yiisoft\Yii\Web\Middleware\JsonWebResponseFormatter;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Yii\Web\Middlewa...sonWebResponseFormatter 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...
19
use Yiisoft\Yii\Web\Middleware\XmlWebResponseFormatter;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Yii\Web\Middleware\XmlWebResponseFormatter 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...
20
use Psr\Container\ContainerInterface;
21
use Psr\Http\Message\ServerRequestInterface;
22
use Psr\Http\Server\RequestHandlerInterface;
23
use Yiisoft\Http\Method;
24
use Yiisoft\Router\FastRoute\UrlMatcher;
25
use Yiisoft\Router\Group;
26
use Yiisoft\Router\Route;
27
use Yiisoft\Router\RouteCollection;
28
use Yiisoft\Router\RouteCollectorInterface;
29
use Yiisoft\Yii\Web\WebResponseFactoryInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Yii\Web\WebResponseFactoryInterface 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...
30
31
class AppRouterFactory
32
{
33
    public function __invoke(ContainerInterface $container)
34
    {
35
        $routes = [
36
            // Lonely pages of site
37
            Route::get('/', [SiteController::class, 'index'])
38
                ->name('site/index'),
39
            Route::methods([Method::GET, Method::POST], '/contact', [ContactController::class, 'contact'])
40
                ->name('site/contact'),
41
            Route::methods([Method::GET, Method::POST], '/login', [AuthController::class, 'login'])
42
                ->name('site/login'),
43
            Route::get('/logout', [AuthController::class, 'logout'])
44
                ->name('site/logout'),
45
46
            // User
47
            Group::create('/user', [
48
                // Index
49
                Route::get('[/page-{page:\d+}]', [UserController::class, 'index'])
50
                    ->name('user/index'),
51
                // Profile page
52
                Route::get('/{login}', [UserController::class, 'profile'])
53
                    ->name('user/profile'),
54
            ]),
55
56
            // User
57
            Group::create('/api', [
58
                Route::get('/info/v1', function (WebResponseFactoryInterface $responseFactory) {
59
                    return $responseFactory->createResponse(['version' => '1.0', 'author' => 'yiisoft']);
60
                })->name('api/info/v1'),
61
                Route::get('/info/v2', ApiInfo::class)
62
                    ->addMiddleware(function (ServerRequestInterface $request, RequestHandlerInterface $handler) {
63
                        return (new WebResponseFormatter(new JsonResponseFormatter(), true))->process($request, $handler);
64
                    })
65
                    ->name('api/info/v2'),
66
                Route::get('/user', [ApiUserController::class, 'index'])
67
                    ->name('api/user/index'),
68
                Route::get('/user/{login}', [ApiUserController::class, 'profile'])
69
                    ->addMiddleware(JsonWebResponseFormatter::class)
70
                    ->name('api/user/profile'),
71
            ], $container)->addMiddleware(function (ServerRequestInterface $request, RequestHandlerInterface $handler) {
72
                $response = $handler->handle($request);
73
                if ($response instanceof WebResponse) {
74
                    $data = $response->getData();
0 ignored issues
show
Bug introduced by
The method getData() does not exist on Psr\Http\Message\ResponseInterface. ( Ignorable by Annotation )

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

74
                    /** @scrutinizer ignore-call */ 
75
                    $data = $response->getData();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
75
                    if ($response->getStatusCode() !== 200) {
76
                        if (is_string($data) && !empty($data)) {
77
                            $message = $data;
78
                        } else {
79
                            $message = 'Unknown error';
80
                        }
81
                        return $response->withData([
0 ignored issues
show
Bug introduced by
The method withData() does not exist on Psr\Http\Message\ResponseInterface. ( Ignorable by Annotation )

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

81
                        return $response->/** @scrutinizer ignore-call */ withData([

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
82
                                'status' => 'failed',
83
                                'error' => ['message' => $message, 'status' => $response->getStatusCode()],
84
                            ]);
85
                    }
86
                    return $response->withData(['status' => 'success', 'data' => $data]);
87
                }
88
89
                return $response;
90
            })->addMiddleware(XmlWebResponseFormatter::class),
91
92
            // Blog routes
93
            Group::create('/blog', [
94
                // Index
95
                Route::get('[/page{page:\d+}]', [BlogController::class, 'index'])
96
                    ->name('blog/index'),
97
                // Post page
98
                Route::get('/page/{slug}', [PostController::class, 'index'])
99
                    ->name('blog/post'),
100
                // Tag page
101
                Route::get('/tag/{label}[/page{page:\d+}]', [TagController::class, 'index'])
102
                    ->name('blog/tag'),
103
                // Archive
104
                Group::create('/blog', [
105
                    // Index page
106
                    Route::get('', [ArchiveController::class, 'index'])
107
                        ->name('blog/archive/index'),
108
                    // Yearly page
109
                    Route::get('/{year:\d+}', [ArchiveController::class, 'yearlyArchive'])
110
                        ->name('blog/archive/year'),
111
                    // Monthly page
112
                    Route::get('/{year:\d+}-{month:\d+}[/page{page:\d+}]', [ArchiveController::class, 'monthlyArchive'])
113
                        ->name('blog/archive/month')
114
                ]),
115
            ]),
116
        ];
117
118
        $collector = $container->get(RouteCollectorInterface::class);
119
        $collector->addGroup(
120
            Group::create(null, $routes)
121
                ->addMiddleware(WebResponseFormatter::class)
122
        );
123
124
        return new UrlMatcher(new RouteCollection($collector));
125
    }
126
}
127