Passed
Pull Request — master (#75)
by
unknown
12:43
created

AppRouterFactory   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 7
Bugs 1 Features 0
Metric Value
wmc 1
eloc 62
c 7
b 1
f 0
dl 0
loc 87
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 85 1
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\AuthController;
10
use App\Controller\ContactController;
11
use App\Controller\SiteController;
12
use App\Controller\UserController;
13
use App\Middleware\ActionCaller;
14
use App\Middleware\SetFormat;
15
use Psr\Container\ContainerInterface;
16
use Psr\Http\Message\ServerRequestInterface;
17
use Psr\Http\Server\RequestHandlerInterface;
18
use Yiisoft\Http\Method;
19
use Yiisoft\Router\FastRoute\UrlMatcher;
20
use Yiisoft\Router\Group;
21
use Yiisoft\Router\Route;
22
use Yiisoft\Router\RouteCollection;
23
use Yiisoft\Router\RouteCollectorInterface;
24
25
class AppRouterFactory
26
{
27
    public function __invoke(ContainerInterface $container)
28
    {
29
        $routes = [
30
            // Lonely pages of site
31
            Route::get('/', [SiteController::class, 'index'])
32
                ->name('site/index'),
33
            Route::methods([Method::GET, Method::POST], '/contact', [ContactController::class, 'contact'])
34
                ->name('site/contact'),
35
            Route::methods([Method::GET, Method::POST], '/login', [AuthController::class, 'login'])
36
                ->name('site/login'),
37
            Route::get('/logout', [AuthController::class, 'logout'])
38
                ->name('site/logout'),
39
40
            // User
41
            Group::create('/user', [
42
                // Index
43
                Route::get('[/page-{page:\d+}]', [UserController::class, 'index'])
44
                    ->name('user/index'),
45
                // Profile page
46
                Route::get('/{login}', [UserController::class, 'profile'])
47
                    ->name('user/profile'),
48
            ]),
49
50
            // Blog routes
51
            Group::create('/blog', [
52
                // Index
53
                Route::get('[/page{page:\d+}]', [BlogController::class, 'index'])
54
                    ->name('blog/index'),
55
                // Post page
56
                Route::get('/page/{slug}', [PostController::class, 'index'])
57
                    ->name('blog/post'),
58
                // Tag page
59
                Route::get('/tag/{label}[/page{page:\d+}]', [TagController::class, 'index'])
60
                    ->name('blog/tag'),
61
                // Archive
62
                Group::create('/archive', [
63
                    // Index page
64
                    Group::create('', [
65
                        Route::get('', fn (ServerRequestInterface $request, RequestHandlerInterface $handler) => (
66
                            new ActionCaller(ArchiveController::class, 'index', $container)
67
                        )->process($request, $handler))
68
                            ->addMiddleware(fn (ServerRequestInterface $request, RequestHandlerInterface $handler) => (
69
                                new SetFormat('text/html', null)
70
                            )->process($request, $handler))
71
                            ->name('blog/archive/index'),
72
                        Route::get('/print_r', fn (ServerRequestInterface $request, RequestHandlerInterface $handler) => (
73
                            new ActionCaller(ArchiveController::class, 'index', $container)
74
                        )->process($request, $handler))
75
                            ->addMiddleware(fn (ServerRequestInterface $request, RequestHandlerInterface $handler) => (
76
                                new SetFormat('text/plain')
77
                            )->process($request, $handler))
78
                            ->name('blog/archive/index/print_r'),
79
                        Route::get('/xml', fn (ServerRequestInterface $request, RequestHandlerInterface $handler) =>  (
80
                            new ActionCaller(ArchiveController::class, 'index', $container)
81
                        )->process($request, $handler))
82
                            ->addMiddleware(fn (ServerRequestInterface $request, RequestHandlerInterface $handler) => (
83
                                new SetFormat('text/xml')
84
                            )->process($request, $handler))
85
                            ->name('blog/archive/index/xml'),
86
                        Route::get('/json', fn (ServerRequestInterface $request, RequestHandlerInterface $handler) => (
87
                            new ActionCaller(ArchiveController::class, 'index', $container)
88
                        )->process($request, $handler))
89
                            ->name('blog/archive/index/json'),
90
                        Route::get('/custom', fn (ServerRequestInterface $request, RequestHandlerInterface $handler) => new ActionCaller(ArchiveController::class, 'custom', $container))
0 ignored issues
show
Unused Code introduced by
The parameter $handler is not used and could be removed. ( Ignorable by Annotation )

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

90
                        Route::get('/custom', fn (ServerRequestInterface $request, /** @scrutinizer ignore-unused */ RequestHandlerInterface $handler) => new ActionCaller(ArchiveController::class, 'custom', $container))

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

90
                        Route::get('/custom', fn (/** @scrutinizer ignore-unused */ ServerRequestInterface $request, RequestHandlerInterface $handler) => new ActionCaller(ArchiveController::class, 'custom', $container))

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
91
                            ->name('blog/archive/index/custom'),
92
                    ]),
93
                    // Yearly page
94
                    Route::get('/{year:\d+}', [ArchiveController::class, 'yearlyArchive'])
95
                        ->name('blog/archive/year'),
96
                    // Monthly page
97
                    Route::get('/{year:\d+}-{month:\d+}[/page{page:\d+}]', [ArchiveController::class, 'monthlyArchive'])
98
                        ->name('blog/archive/month')
99
                ]
100
                )->addMiddleware(
101
                    fn(ServerRequestInterface $request, RequestHandlerInterface $handler) => (new SetFormat(
102
                        'application/json'
103
                    ))->process($request, $handler)
104
                ),
105
            ]),
106
        ];
107
108
        $collector =  $container->get(RouteCollectorInterface::class);
109
        $collector->addGroup(Group::create(null, $routes));
110
111
        return new UrlMatcher(new RouteCollection($collector));
112
    }
113
}
114