ApiUserController::index()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 43
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
cc 2
eloc 30
c 2
b 2
f 0
nc 2
nop 1
dl 0
loc 43
rs 9.44
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\User\Controller;
6
7
use App\User\User;
8
use App\User\UserRepository;
9
use Psr\Http\Message\ResponseInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\ResponseInterface 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\Data\Reader\Sort;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Data\Reader\Sort 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
use Yiisoft\DataResponse\DataResponseFactoryInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\DataResponse\DataResponseFactoryInterface 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...
12
use Yiisoft\Router\CurrentRoute;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Router\CurrentRoute 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...
13
use OpenApi\Attributes as OA;
0 ignored issues
show
Bug introduced by
The type OpenApi\Attributes 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...
14
15
#[OA\Tag(name: 'user', description: 'User')]
16
final class ApiUserController
17
{
18
    public function __construct(private DataResponseFactoryInterface $responseFactory)
19
    {
20
    }
21
22
    #[OA\Get(
23
        path: '/api/user',
24
        description: '',
25
        summary: 'Get users list',
26
        tags: ['user'],
27
        responses: [
28
            new OA\Response(
29
                response: '200',
30
                description: 'Success',
31
                content: new OA\XmlContent(
32
                    xml: new OA\Xml(name: 'response'),
33
                    allOf: [
34
                        new OA\Schema(ref: '#/components/schemas/Response'),
35
                        new OA\Schema(properties: [
36
                            new OA\Property(
37
                                property: 'data',
38
                                type: 'array',
39
                                items: new OA\Items(properties: [
40
                                    new OA\Property(property: 'login', type: 'string', example: 'exampleLogin'),
41
                                    new OA\Property(property: 'created_at', type: 'string', example: '13.12.2020 00:04:20'),
42
                                ])
43
                            ),
44
                        ]),
45
                    ]
46
                )
47
            ),
48
        ]
49
    )]
50
    public function index(UserRepository $userRepository): ResponseInterface
51
    {
52
        $dataReader = $userRepository
53
            ->findAll()
54
            ->withSort(Sort::only(['login'])->withOrderString('login'));
55
        $users = $dataReader->read();
56
57
        $items = [];
58
        foreach ($users as $user) {
59
            $items[] = ['login' => $user->getLogin(), 'created_at' => $user
60
                ->getCreatedAt()
61
                ->format('H:i:s d.m.Y'), ];
62
        }
63
64
        return $this->responseFactory->createResponse($items);
65
    }
66
67
    #[OA\Get(
68
        path: '/api/user/{login}',
69
        description: '',
70
        summary: 'Get user info',
71
        tags: ['user'],
72
        parameters: [
73
            new OA\Parameter(parameter: 'login', name: 'Login', in: 'path'),
74
        ],
75
        responses: [
76
            new OA\Response(
77
                response: '200',
78
                description: 'Success',
79
                content: new OA\JsonContent(
80
                    allOf: [
81
                        new OA\Schema(ref: '#/components/schemas/Response'),
82
                        new OA\Schema(properties: [
83
                            new OA\Property(property: 'data', properties: [
84
                                new OA\Property(property: 'login', type: 'string', example: 'exampleLogin'),
85
                                new OA\Property(property: 'created_at', type: 'string', example: '13.12.2020 00:04:20'),
86
                            ], type: 'object'),
87
                        ]),
88
                    ]
89
                ),
90
            ),
91
        ]
92
    )]
93
    public function profile(UserRepository $userRepository, CurrentRoute $currentRoute): ResponseInterface
94
    {
95
        $login = $currentRoute->getArgument('login');
96
97
        /** @var User $user */
98
        $user = $userRepository->findByLogin($login);
99
        if ($user === null) {
100
            return $this->responseFactory->createResponse('Page not found', 404);
101
        }
102
103
        return $this->responseFactory->createResponse(
104
            ['login' => $user->getLogin(), 'created_at' => $user
105
                ->getCreatedAt()
106
                ->format('H:i:s d.m.Y'), ]
107
        );
108
    }
109
}
110