| Conditions | 12 |
| Paths | 10 |
| Total Lines | 74 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 64 | public function __invoke(Request $request, string $field, string $id) |
||
| 65 | { |
||
| 66 | $contentType = $request->headers->get('CONTENT_TYPE'); |
||
| 67 | $_format = $request->attributes->get('_format') ?: $request->getFormat($contentType); |
||
| 68 | |||
| 69 | /** |
||
| 70 | * MATCH THE ID TO A ROUTE TO FIND RESOURCE CLASS AND ID |
||
| 71 | * @var array|null $route |
||
| 72 | */ |
||
| 73 | $ctx = new RequestContext(); |
||
| 74 | $ctx->fromRequest($request); |
||
| 75 | $ctx->setMethod('GET'); |
||
| 76 | $this->urlMatcher->setContext($ctx); |
||
| 77 | $route = $this->urlMatcher->match($id); |
||
| 78 | if (empty($route) || !isset($route['_api_resource_class'])) { |
||
| 79 | return new Response(sprintf('No route/resource found for id %s', $id), Response::HTTP_BAD_REQUEST); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * GET THE ENTITY |
||
| 84 | */ |
||
| 85 | $entity = $this->itemDataProvider->getItem($route['_api_resource_class'], $route['id']); |
||
| 86 | if (!$entity) { |
||
| 87 | return new Response(sprintf('Entity not found from provider %s (ID: %s)', $route['_api_resource_class'], $route['id']), Response::HTTP_BAD_REQUEST); |
||
| 88 | } |
||
| 89 | if (!($entity instanceof FileInterface)) { |
||
| 90 | return new Response(sprintf('Provider %s does not implement %s', $route['_api_resource_class'], FileInterface::class), Response::HTTP_BAD_REQUEST); |
||
| 91 | } |
||
| 92 | $method = strtolower($request->getMethod()); |
||
| 93 | |||
| 94 | if ($method === 'get') { |
||
| 95 | $propertyAccessor = PropertyAccess::createPropertyAccessor(); |
||
| 96 | if (!$this->restrictedResourceVoter->vote($entity)) { |
||
| 97 | throw new AccessDeniedException('You are not permitted to download this file'); |
||
| 98 | } |
||
| 99 | $filePath = $propertyAccessor->getValue($entity, $field); |
||
| 100 | return new BinaryFileResponse($filePath); |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * CHECK WE HAVE A FILE - WASTE OF TIME DOING ANYTHING ELSE OTHERWISE |
||
| 105 | */ |
||
| 106 | if (!$request->files->count()) { |
||
| 107 | return new Response('No files have been submitted', Response::HTTP_BAD_REQUEST); |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * UPLOAD THE FILE |
||
| 112 | */ |
||
| 113 | $files = $request->files->all(); |
||
| 114 | try { |
||
| 115 | $entity = $this->uploader->upload($entity, $field, reset($files), $method); |
||
| 116 | } catch (InvalidArgumentException $exception) { |
||
| 117 | return new Response($exception->getMessage(), Response::HTTP_BAD_REQUEST); |
||
| 118 | } catch (RuntimeException $exception) { |
||
| 119 | return new Response($exception->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Return the entity back in the format requested |
||
| 124 | */ |
||
| 125 | $resourceMetadata = $this->resourceMetadataFactory->create($route['_api_resource_class']); |
||
| 126 | $serializerGroups = $resourceMetadata->getOperationAttribute( |
||
| 127 | ['item_operation_name' => $method], |
||
| 128 | 'serializer_groups', |
||
| 129 | [], |
||
| 130 | true |
||
| 131 | ); |
||
| 132 | $customGroups = $this->apiContextBuilder->getGroups($route['_api_resource_class'], true); |
||
| 133 | if (\count($customGroups)) { |
||
| 134 | $serializerGroups = array_merge($serializerGroups ?? [], ...$customGroups); |
||
| 135 | } |
||
| 136 | $serializedData = $this->serializer->serialize($entity, $_format, ['groups' => $serializerGroups]); |
||
| 137 | return new Response($serializedData, Response::HTTP_OK); |
||
| 138 | } |
||
| 140 |
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