UserController::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 43
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 26
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 43
rs 9.504
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\User;
6
7
use App\Exception\NotFoundException;
8
use App\RestControllerTrait;
9
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...
10
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...
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\HydratorAttribute\RouteArgument;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Router\HydratorAttribute\RouteArgument 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
14
#[OA\Tag(name: 'user', description: 'User')]
15
final class UserController
16
{
17
    use RestControllerTrait;
18
19
    private DataResponseFactoryInterface $responseFactory;
20
    private UserRepository $userRepository;
21
    private UserFormatter $userFormatter;
22
23
    public function __construct(
24
        DataResponseFactoryInterface $responseFactory,
25
        UserRepository $userRepository,
26
        UserFormatter $userFormatter
27
    ) {
28
        $this->responseFactory = $responseFactory;
29
        $this->userRepository = $userRepository;
30
        $this->userFormatter = $userFormatter;
31
    }
32
33
    #[OA\Get(
34
        path: '/users/',
35
        description: '',
36
        summary: 'Returns paginated users',
37
        security: [new OA\SecurityScheme(ref: '#/components/securitySchemes/ApiKey')],
38
        tags: ['user'],
39
        responses: [
40
            new OA\Response(
41
                response: '200',
42
                description: 'Success',
43
                content: new OA\JsonContent(
44
                    allOf: [
45
                        new OA\Schema(ref: '#/components/schemas/Response'),
46
                        new OA\Schema(properties: [
47
                            new OA\Property(
48
                                property: 'data',
49
                                properties: [
50
                                    new OA\Property(
51
                                        property: 'user',
52
                                        type: 'array',
53
                                        items: new OA\Items(ref: '#/components/schemas/User')
54
                                    ),
55
                                ],
56
                                type: 'object'
57
                            ),
58
                        ]),
59
                    ]
60
                )
61
            ),
62
        ]
63
    )]
64
    public function list(): ResponseInterface
65
    {
66
        $dataReader = $this->userRepository->findAllOrderByLogin();
67
        $result = [];
68
        foreach ($dataReader->read() as $user) {
69
            $result[] = $this->userFormatter->format($user);
70
        }
71
72
        return $this->responseFactory->createResponse(
73
            [
74
                'users' => $result,
75
            ]
76
        );
77
    }
78
79
    #[OA\Get(
80
        path: '/users/{id}',
81
        description: '',
82
        summary: 'Returns a user with a given ID',
83
        security: [new OA\SecurityScheme(ref: '#/components/securitySchemes/ApiKey')],
84
        tags: ['user'],
85
        parameters: [
86
            new OA\Parameter(parameter: 'id', name: 'id', in: 'path', example: 2),
87
        ],
88
        responses: [
89
            new OA\Response(
90
                response: '200',
91
                description: 'Success',
92
                content: new OA\JsonContent(
93
                    allOf: [
94
                        new OA\Schema(ref: '#/components/schemas/Response'),
95
                        new OA\Schema(properties: [
96
                            new OA\Property(
97
                                property: 'data',
98
                                properties: [
99
                                    new OA\Property(property: 'user', ref: '#/components/schemas/User', type: 'object'),
100
                                ],
101
                                type: 'object'
102
                            ),
103
                        ]),
104
                    ]
105
                )
106
            ),
107
        ]
108
    )]
109
    public function get(#[RouteArgument('id')] int $id): ResponseInterface
110
    {
111
        /**
112
         * @var User $user
113
         */
114
        $user = $this->userRepository->findByPK($id);
115
        if ($user === null) {
116
            throw new NotFoundException();
117
        }
118
119
        return $this->responseFactory->createResponse(
120
            [
121
                'user' => $this->userFormatter->format($user),
122
            ]
123
        );
124
    }
125
}
126