yiisoft /
demo
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace App\Blog; |
||
| 6 | |||
| 7 | use App\Formatter\PaginatorFormatter; |
||
| 8 | use App\User\UserRequest; |
||
| 9 | use OpenApi\Attributes as OA; |
||
|
0 ignored issues
–
show
|
|||
| 10 | use Psr\Http\Message\ResponseInterface as Response; |
||
|
0 ignored issues
–
show
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. 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
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. 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\Input\Http\Attribute\Parameter\Query; |
||
|
0 ignored issues
–
show
The type
Yiisoft\Input\Http\Attribute\Parameter\Query 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. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 13 | use Yiisoft\Router\HydratorAttribute\RouteArgument; |
||
|
0 ignored issues
–
show
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. 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: 'blog', description: 'Blog')] |
||
| 16 | #[OA\Parameter(parameter: 'PageRequest', name: 'page', in: 'query', schema: new OA\Schema(type: 'int', example: '2'))] |
||
| 17 | final class BlogController |
||
| 18 | { |
||
| 19 | private DataResponseFactoryInterface $responseFactory; |
||
| 20 | private PostRepository $postRepository; |
||
| 21 | private PostFormatter $postFormatter; |
||
| 22 | private PostBuilder $postBuilder; |
||
| 23 | private BlogService $blogService; |
||
| 24 | |||
| 25 | public function __construct( |
||
| 26 | PostRepository $postRepository, |
||
| 27 | DataResponseFactoryInterface $responseFactory, |
||
| 28 | PostFormatter $postFormatter, |
||
| 29 | PostBuilder $postBuilder, |
||
| 30 | BlogService $blogService |
||
| 31 | ) { |
||
| 32 | $this->postRepository = $postRepository; |
||
| 33 | $this->responseFactory = $responseFactory; |
||
| 34 | $this->postFormatter = $postFormatter; |
||
| 35 | $this->postBuilder = $postBuilder; |
||
| 36 | $this->blogService = $blogService; |
||
| 37 | } |
||
| 38 | |||
| 39 | #[OA\Get( |
||
| 40 | path: '/blog/', |
||
| 41 | description: '', |
||
| 42 | summary: 'Returns paginated blog posts', |
||
| 43 | tags: ['blog'], |
||
| 44 | parameters: [ |
||
| 45 | new OA\Parameter(ref: '#/components/parameters/PageRequest'), |
||
| 46 | ], |
||
| 47 | responses: [ |
||
| 48 | new OA\Response( |
||
| 49 | response:'200', |
||
| 50 | description:'Success', |
||
| 51 | content: new OA\JsonContent( |
||
| 52 | allOf: [ |
||
| 53 | new OA\Schema(ref: '#/components/schemas/Response'), |
||
| 54 | new OA\Schema(properties: [ |
||
| 55 | new OA\Property( |
||
| 56 | property: 'data', |
||
| 57 | properties: [ |
||
| 58 | new OA\Property(property: 'posts', type: 'array', items: new OA\Items(ref:'#/components/schemas/Post')), |
||
| 59 | new OA\Property(property: 'paginator', ref: '#/components/schemas/Paginator', type: 'object'), |
||
| 60 | ], |
||
| 61 | type: 'object' |
||
| 62 | ), |
||
| 63 | ]), |
||
| 64 | ] |
||
| 65 | ), |
||
| 66 | ), |
||
| 67 | ] |
||
| 68 | )] |
||
| 69 | public function index(PaginatorFormatter $paginatorFormatter, #[Query('page')] int $page = 1): Response |
||
| 70 | { |
||
| 71 | $paginator = $this->blogService->getPosts($page); |
||
|
0 ignored issues
–
show
|
|||
| 72 | $posts = []; |
||
| 73 | foreach ($paginator->read() as $post) { |
||
| 74 | $posts[] = $this->postFormatter->format($post); |
||
|
0 ignored issues
–
show
|
|||
| 75 | } |
||
| 76 | |||
| 77 | return $this->responseFactory->createResponse( |
||
|
0 ignored issues
–
show
|
|||
| 78 | [ |
||
| 79 | 'paginator' => $paginatorFormatter->format($paginator), |
||
| 80 | 'posts' => $posts, |
||
| 81 | ] |
||
| 82 | ); |
||
| 83 | } |
||
| 84 | |||
| 85 | #[OA\Get( |
||
| 86 | path: '/blog/{id}', |
||
| 87 | description: '', |
||
| 88 | summary: 'Returns a post with a given ID', |
||
| 89 | tags: ['blog'], |
||
| 90 | parameters: [ |
||
| 91 | new OA\Parameter(parameter: 'id', name: 'id', in: 'path', schema: new OA\Schema(type: 'int', example: '2')), |
||
| 92 | ], |
||
| 93 | responses: [ |
||
| 94 | new OA\Response( |
||
| 95 | response:'200', |
||
| 96 | description:'Success', |
||
| 97 | content: new OA\JsonContent( |
||
| 98 | allOf: [ |
||
| 99 | new OA\Schema(ref: '#/components/schemas/Response'), |
||
| 100 | new OA\Schema(properties: [ |
||
| 101 | new OA\Property( |
||
| 102 | property: 'data', |
||
| 103 | properties: [ |
||
| 104 | new OA\Property(property: 'post', ref: '#/components/schemas/Post', type: 'object'), |
||
| 105 | ], |
||
| 106 | type: 'object' |
||
| 107 | ), |
||
| 108 | ]), |
||
| 109 | ] |
||
| 110 | ), |
||
| 111 | ), |
||
| 112 | new OA\Response( |
||
| 113 | response: '404', |
||
| 114 | description: 'Not found', |
||
| 115 | content: new OA\JsonContent(allOf: [ |
||
| 116 | new OA\Schema(ref: '#/components/schemas/BadResponse'), |
||
| 117 | new OA\Schema(properties: [ |
||
| 118 | new OA\Property(property:'error_message', example:'Entity not found'), |
||
| 119 | new OA\Property(property: 'error_code', example: 404, nullable: true), |
||
| 120 | ]), |
||
| 121 | ]) |
||
| 122 | ), |
||
| 123 | ] |
||
| 124 | )] |
||
| 125 | public function view(#[RouteArgument('id')] int $id): Response |
||
| 126 | { |
||
| 127 | return $this->responseFactory->createResponse( |
||
|
0 ignored issues
–
show
|
|||
| 128 | [ |
||
| 129 | 'post' => $this->postFormatter->format( |
||
|
0 ignored issues
–
show
|
|||
| 130 | $this->blogService->getPost($id) |
||
|
0 ignored issues
–
show
|
|||
| 131 | ), |
||
| 132 | ] |
||
| 133 | ); |
||
| 134 | } |
||
| 135 | |||
| 136 | #[OA\Post( |
||
| 137 | path: '/blog/', |
||
| 138 | description: '', |
||
| 139 | summary: 'Creates a blog post', |
||
| 140 | security: [new OA\SecurityScheme(ref: '#/components/securitySchemes/ApiKey')], |
||
| 141 | requestBody: new OA\RequestBody(required: true, content: new OA\JsonContent( |
||
| 142 | allOf: [ |
||
| 143 | new OA\Schema(ref: '#/components/schemas/EditPostRequest'), |
||
| 144 | ] |
||
| 145 | )), |
||
| 146 | tags: ['blog'], |
||
| 147 | responses: [ |
||
| 148 | new OA\Response( |
||
| 149 | response: '200', |
||
| 150 | description: 'Success', |
||
| 151 | content: new OA\JsonContent(ref: '#/components/schemas/Response') |
||
| 152 | ), |
||
| 153 | new OA\Response( |
||
| 154 | response: '400', |
||
| 155 | description: 'Bad request', |
||
| 156 | content: new OA\JsonContent(ref: '#/components/schemas/BadResponse') |
||
| 157 | ), |
||
| 158 | ] |
||
| 159 | )] |
||
| 160 | public function create(EditPostRequest $postRequest, UserRequest $userRequest): Response |
||
| 161 | { |
||
| 162 | $post = $this->postBuilder->build(new Post(), $postRequest); |
||
|
0 ignored issues
–
show
|
|||
| 163 | $post->setUser($userRequest->getUser()); |
||
| 164 | |||
| 165 | $this->postRepository->save($post); |
||
|
0 ignored issues
–
show
|
|||
| 166 | |||
| 167 | return $this->responseFactory->createResponse(); |
||
|
0 ignored issues
–
show
|
|||
| 168 | } |
||
| 169 | |||
| 170 | #[OA\Put( |
||
| 171 | path: '/blog/{id}', |
||
| 172 | description: '', |
||
| 173 | summary: 'Updates a blog post with a given ID', |
||
| 174 | security: [new OA\SecurityScheme(ref: '#/components/securitySchemes/ApiKey')], |
||
| 175 | requestBody: new OA\RequestBody(required: true, content: new OA\JsonContent( |
||
| 176 | allOf: [ |
||
| 177 | new OA\Schema(ref: '#/components/schemas/EditPostRequest'), |
||
| 178 | ] |
||
| 179 | )), |
||
| 180 | tags: ['blog'], |
||
| 181 | parameters: [ |
||
| 182 | new OA\Parameter(parameter: 'id', name: 'id', in: 'path', schema: new OA\Schema(type: 'int', example: '2')), |
||
| 183 | ], |
||
| 184 | responses: [ |
||
| 185 | new OA\Response( |
||
| 186 | response: '200', |
||
| 187 | description: 'Success', |
||
| 188 | content: new OA\JsonContent(ref: '#/components/schemas/Response') |
||
| 189 | ), |
||
| 190 | new OA\Response( |
||
| 191 | response: '400', |
||
| 192 | description: 'Bad request', |
||
| 193 | content: new OA\JsonContent(ref: '#/components/schemas/BadResponse') |
||
| 194 | ), |
||
| 195 | ] |
||
| 196 | )] |
||
| 197 | public function update(EditPostRequest $postRequest, #[RouteArgument('id')] int $id): Response |
||
| 198 | { |
||
| 199 | $post = $this->postBuilder->build( |
||
|
0 ignored issues
–
show
|
|||
| 200 | $this->blogService->getPost($id), |
||
|
0 ignored issues
–
show
|
|||
| 201 | $postRequest |
||
| 202 | ); |
||
| 203 | |||
| 204 | $this->postRepository->save($post); |
||
|
0 ignored issues
–
show
|
|||
| 205 | |||
| 206 | return $this->responseFactory->createResponse(); |
||
|
0 ignored issues
–
show
|
|||
| 207 | } |
||
| 208 | } |
||
| 209 |
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths