Conditions | 9 |
Paths | 8 |
Total Lines | 58 |
Code Lines | 28 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
51 | public function __invoke(Request $request, string $field, string $id) |
||
52 | { |
||
53 | $contentType = $request->headers->get('CONTENT_TYPE'); |
||
54 | $_format = $request->attributes->get('_format') ?: $request->getFormat($contentType); |
||
55 | |||
56 | /** |
||
57 | * MATCH THE ID TO A ROUTE TO FIND RESOURCE CLASS AND ID |
||
58 | * @var array|null $route |
||
59 | */ |
||
60 | $ctx = new RequestContext(); |
||
61 | $ctx->fromRequest($request); |
||
62 | $ctx->setMethod('GET'); |
||
63 | $this->urlMatcher->setContext($ctx); |
||
64 | $route = $this->urlMatcher->match($id); |
||
65 | if (empty($route)) { |
||
66 | return new Response(sprintf('No route found for id %s', $id), Response::HTTP_BAD_REQUEST); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * GET THE ENTITY |
||
71 | */ |
||
72 | $entity = $this->itemDataProvider->getItem($route['_api_resource_class'], $route['id']); |
||
73 | if (!$entity) { |
||
74 | return new Response(sprintf('Entity not found from provider %s (ID: %s)', $route['_api_resource_class'], $route['id']), Response::HTTP_BAD_REQUEST); |
||
75 | } |
||
76 | if (!($entity instanceof FileInterface)) { |
||
77 | return new Response(sprintf('Provider %s does not implement %s', $route['_api_resource_class'], FileInterface::class), Response::HTTP_BAD_REQUEST); |
||
78 | } |
||
79 | |||
80 | if ($request->getMethod() === 'GET') { |
||
81 | $propertyAccessor = PropertyAccess::createPropertyAccessor(); |
||
82 | $filePath = $propertyAccessor->getValue($entity, $field); |
||
83 | return new BinaryFileResponse($filePath); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * CHECK WE HAVE A FILE - WASTE OF TIME DOING ANYTHING ELSE OTHERWISE |
||
88 | */ |
||
89 | if (!$request->files->count()) { |
||
90 | return new Response('No files have been submitted', Response::HTTP_BAD_REQUEST); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * UPLOAD THE FILE |
||
95 | */ |
||
96 | $files = $request->files->all(); |
||
97 | try { |
||
98 | $entity = $this->uploader->upload($entity, $field, reset($files)); |
||
99 | } catch (InvalidArgumentException $exception) { |
||
100 | return new Response($exception->getMessage(), Response::HTTP_BAD_REQUEST); |
||
101 | } catch (RuntimeException $exception) { |
||
102 | return new Response($exception->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Return the entity back in the format requested |
||
107 | */ |
||
108 | return new Response($this->serializer->serialize($entity, $_format, ['groups' => ['component']]), Response::HTTP_OK); |
||
109 | } |
||
111 |