| Conditions | 1 |
| Paths | 1 |
| Total Lines | 187 |
| Code Lines | 142 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 50 | #[ |
||
| 51 | Get( |
||
| 52 | path: "/posts/custom-filters", |
||
| 53 | operationId: "postGetWithCustomFilters", |
||
| 54 | description: "Get all posts with pagination (10 items per page by default, page 1 by default) |
||
| 55 | |||
| 56 | This API will get records from the database and return them as a paginated list. |
||
| 57 | The default number of items per page is 10 and the default page number is 1. You can change these values by passing the `per_page` and `page` query parameters. |
||
| 58 | ", |
||
| 59 | summary: "Get posts by custom filters with pagination", |
||
| 60 | tags: ["Post"], |
||
| 61 | parameters: [ |
||
| 62 | new Parameter( |
||
| 63 | name: 'tags', |
||
| 64 | description: 'Filter posts by tag specific tag IDs.', |
||
| 65 | in: 'query', |
||
| 66 | required: false, |
||
| 67 | schema: new Schema( |
||
| 68 | type: 'array', |
||
| 69 | items: new Items(description: 'Input the exclude tag ID', type: 'integer'), |
||
| 70 | default: null |
||
| 71 | ) |
||
| 72 | ), |
||
| 73 | new Parameter( |
||
| 74 | name: 'categories', |
||
| 75 | description: 'Filter posts by categories IDs', |
||
| 76 | in: 'query', |
||
| 77 | required: false, |
||
| 78 | schema: new Schema( |
||
| 79 | type: 'array', |
||
| 80 | items: new Items(description: 'Input the category ID', type: 'integer'), |
||
| 81 | default: null, |
||
| 82 | ) |
||
| 83 | ), |
||
| 84 | new Parameter( |
||
| 85 | name: 'categories_exclude', |
||
| 86 | description: 'Filter posts by excluding specific category IDs.', |
||
| 87 | in: 'query', |
||
| 88 | required: false, |
||
| 89 | schema: new Schema( |
||
| 90 | type: 'array', |
||
| 91 | items: new Items(description: 'Input the exclude category ID', type: 'integer'), |
||
| 92 | default: null |
||
| 93 | ) |
||
| 94 | ), |
||
| 95 | new Parameter( |
||
| 96 | name: 'exclude', |
||
| 97 | description: 'Filter posts by excluding specific post IDs.', |
||
| 98 | in: 'query', |
||
| 99 | required: false, |
||
| 100 | schema: new Schema( |
||
| 101 | type: 'array', |
||
| 102 | items: new Items(description: 'Input the exclude post ID', type: 'integer'), |
||
| 103 | default: null |
||
| 104 | ) |
||
| 105 | ), |
||
| 106 | new Parameter( |
||
| 107 | name: 'include', |
||
| 108 | description: 'Filter posts by include post IDs.', |
||
| 109 | in: 'query', |
||
| 110 | required: false, |
||
| 111 | schema: new Schema( |
||
| 112 | type: 'array', |
||
| 113 | items: new Items(description: 'Input the include post ID', type: 'integer'), |
||
| 114 | default: null |
||
| 115 | ) |
||
| 116 | ), |
||
| 117 | new Parameter( |
||
| 118 | name: 'author', |
||
| 119 | description: 'Filter posts by author IDs', |
||
| 120 | in: 'query', |
||
| 121 | required: false, |
||
| 122 | schema: new Schema( |
||
| 123 | type: 'array', |
||
| 124 | items: new Items(description: 'Input the author ID', type: 'integer'), |
||
| 125 | default: null |
||
| 126 | ) |
||
| 127 | ), |
||
| 128 | new Parameter( |
||
| 129 | name: 'author_exclude', |
||
| 130 | description: 'Filter posts by excluding specific author IDs.', |
||
| 131 | in: 'query', |
||
| 132 | required: false, |
||
| 133 | schema: new Schema( |
||
| 134 | type: 'array', |
||
| 135 | items: new Items(description: 'Input the exclude author ID', type: 'integer'), |
||
| 136 | default: null |
||
| 137 | ) |
||
| 138 | ), |
||
| 139 | new Parameter( |
||
| 140 | name: 'featured', |
||
| 141 | description: 'Filter posts by featured status. Accepts values: |
||
| 142 | 1 for featured posts |
||
| 143 | 0 for non-featured posts.', |
||
| 144 | in: 'query', |
||
| 145 | required: false, |
||
| 146 | schema: new Schema( |
||
| 147 | type: 'integer', |
||
| 148 | default: null, |
||
| 149 | enum: [0, 1], |
||
| 150 | nullable: true |
||
| 151 | ) |
||
| 152 | ), |
||
| 153 | new Parameter( |
||
| 154 | name: 'search', |
||
| 155 | description: 'Search for posts where the given keyword appears in either the name or description fields.', |
||
| 156 | in: 'query', |
||
| 157 | required: false, |
||
| 158 | schema: new Schema(type: 'string', default: null) |
||
| 159 | ), |
||
| 160 | new Parameter( |
||
| 161 | name: 'order_by', |
||
| 162 | description: 'Can order by field: id, views, comments_count, likes_count, created_at, ...', |
||
| 163 | in: 'query', |
||
| 164 | required: false, |
||
| 165 | schema: new Schema(type: 'string', default: 'created_at') |
||
| 166 | ), |
||
| 167 | new Parameter( |
||
| 168 | name: 'order', |
||
| 169 | description: 'Order direction: |
||
| 170 | ASC for ascending |
||
| 171 | DESC for descending', |
||
| 172 | in: 'query', |
||
| 173 | required: false, |
||
| 174 | schema: new Schema(type: 'string', default: 'ASC', enum: ['ASC', 'DESC']) |
||
| 175 | ), |
||
| 176 | new Parameter( |
||
| 177 | name: 'per_page', |
||
| 178 | description: 'Number of items per page', |
||
| 179 | in: 'query', |
||
| 180 | required: false, |
||
| 181 | schema: new Schema(type: 'integer', default: 10) |
||
| 182 | ), |
||
| 183 | new Parameter( |
||
| 184 | name: 'page', |
||
| 185 | description: 'Page number', |
||
| 186 | in: 'query', |
||
| 187 | required: false, |
||
| 188 | schema: new Schema(type: 'integer', default: 1) |
||
| 189 | ), |
||
| 190 | ], |
||
| 191 | responses: [ |
||
| 192 | new Response( |
||
| 193 | response: 200, |
||
| 194 | description: "Get list posts successfully", |
||
| 195 | content: new JsonContent( |
||
| 196 | properties: [ |
||
| 197 | new Property( |
||
| 198 | property: 'error', |
||
| 199 | description: 'Error status', |
||
| 200 | type: 'boolean', |
||
| 201 | default: false |
||
| 202 | ), |
||
| 203 | new Property( |
||
| 204 | property: "data", |
||
| 205 | ref: PostListResourceSchema::class, |
||
| 206 | description: "Data of model", |
||
| 207 | type: "object", |
||
| 208 | ), |
||
| 209 | ] |
||
| 210 | ) |
||
| 211 | ), |
||
| 212 | new Response( |
||
| 213 | ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\BadRequestResponseSchema::class, |
||
| 214 | response: 400, |
||
| 215 | ), |
||
| 216 | new Response( |
||
| 217 | ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\ErrorNotFoundResponseSchema::class, |
||
| 218 | response: 404, |
||
| 219 | ), |
||
| 220 | new Response( |
||
| 221 | ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\InternalServerResponseSchema::class, |
||
| 222 | response: 500, |
||
| 223 | ), |
||
| 224 | ] |
||
| 225 | ) |
||
| 226 | ] |
||
| 227 | public function __invoke(PostGetFiltersRequest $request): BaseHttpResponse|JsonResponse|JsonResource|RedirectResponse |
||
| 228 | { |
||
| 229 | $filters = FilterPost::setFilters($request->validated()); |
||
| 230 | |||
| 231 | $data = $this->postService->getCustomFilters((array) $filters); |
||
| 232 | |||
| 233 | return $this |
||
| 234 | ->httpResponse() |
||
| 235 | ->setData(ListPostResource::collection($data)) |
||
| 236 | ->toApiResponse(); |
||
| 237 | } |
||
| 239 |