| Conditions | 21 |
| Paths | 63 |
| Total Lines | 113 |
| 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 |
||
| 62 | public function __invoke(Request $request): JsonResponse |
||
| 63 | { |
||
| 64 | $field = $request->get('field'); |
||
| 65 | $code = $request->get('code'); |
||
| 66 | $objectId = $request->get('objectId'); |
||
| 67 | $value = $originalValue = $request->get('value'); |
||
| 68 | $context = $request->get('context'); |
||
| 69 | |||
| 70 | $admin = $this->pool->getInstance($code); |
||
| 71 | $admin->setRequest($request); |
||
| 72 | |||
| 73 | // alter should be done by using a post method |
||
| 74 | if (!$request->isXmlHttpRequest()) { |
||
| 75 | return new JsonResponse('Expected an XmlHttpRequest request header', Response::HTTP_METHOD_NOT_ALLOWED); |
||
| 76 | } |
||
| 77 | |||
| 78 | if (Request::METHOD_POST !== $request->getMethod()) { |
||
| 79 | return new JsonResponse(sprintf( |
||
| 80 | 'Invalid request method given "%s", %s expected', |
||
| 81 | $request->getMethod(), |
||
| 82 | Request::METHOD_POST |
||
| 83 | ), Response::HTTP_METHOD_NOT_ALLOWED); |
||
| 84 | } |
||
| 85 | |||
| 86 | $rootObject = $object = $admin->getObject($objectId); |
||
| 87 | |||
| 88 | if (!$object) { |
||
| 89 | return new JsonResponse('Object does not exist', Response::HTTP_NOT_FOUND); |
||
| 90 | } |
||
| 91 | |||
| 92 | // check user permission |
||
| 93 | if (false === $admin->hasAccess('edit', $object)) { |
||
| 94 | return new JsonResponse('Invalid permissions', Response::HTTP_FORBIDDEN); |
||
| 95 | } |
||
| 96 | |||
| 97 | if ('list' === $context) { |
||
| 98 | $fieldDescription = $admin->getListFieldDescription($field); |
||
| 99 | } else { |
||
| 100 | return new JsonResponse('Invalid context', Response::HTTP_BAD_REQUEST); |
||
| 101 | } |
||
| 102 | |||
| 103 | if (!$fieldDescription) { |
||
| 104 | return new JsonResponse('The field does not exist', Response::HTTP_BAD_REQUEST); |
||
| 105 | } |
||
| 106 | |||
| 107 | if (!$fieldDescription->getOption('editable')) { |
||
| 108 | return new JsonResponse('The field cannot be edited, editable option must be set to true', Response::HTTP_BAD_REQUEST); |
||
| 109 | } |
||
| 110 | |||
| 111 | $propertyPath = new PropertyPath($field); |
||
| 112 | |||
| 113 | // If property path has more than 1 element, take the last object in order to validate it |
||
| 114 | if ($propertyPath->getLength() > 1) { |
||
| 115 | $object = $this->pool->getPropertyAccessor()->getValue($object, $propertyPath->getParent()); |
||
|
|
|||
| 116 | |||
| 117 | $elements = $propertyPath->getElements(); |
||
| 118 | $field = end($elements); |
||
| 119 | $propertyPath = new PropertyPath($field); |
||
| 120 | } |
||
| 121 | |||
| 122 | // Handle date and datetime types have setter expecting a DateTime object |
||
| 123 | if ('' !== $value && \in_array($fieldDescription->getType(), ['date', 'datetime'], true)) { |
||
| 124 | $value = new \DateTime($value); |
||
| 125 | } |
||
| 126 | |||
| 127 | // Handle boolean type transforming the value into a boolean |
||
| 128 | if ('' !== $value && 'boolean' === $fieldDescription->getType()) { |
||
| 129 | $value = filter_var($value, FILTER_VALIDATE_BOOLEAN); |
||
| 130 | } |
||
| 131 | |||
| 132 | // Handle entity choice association type, transforming the value into entity |
||
| 133 | if ('' !== $value |
||
| 134 | && 'choice' === $fieldDescription->getType() |
||
| 135 | && null !== $fieldDescription->getOption('class') |
||
| 136 | // NEXT_MAJOR: Replace this call with "$fieldDescription->getOption('class') === $fieldDescription->getTargetModel()". |
||
| 137 | && $this->hasFieldDescriptionAssociationWithClass($fieldDescription, $fieldDescription->getOption('class')) |
||
| 138 | ) { |
||
| 139 | $value = $admin->getModelManager()->find($fieldDescription->getOption('class'), $value); |
||
| 140 | |||
| 141 | if (!$value) { |
||
| 142 | return new JsonResponse(sprintf( |
||
| 143 | 'Edit failed, object with id: %s not found in association: %s.', |
||
| 144 | $originalValue, |
||
| 145 | $field |
||
| 146 | ), Response::HTTP_NOT_FOUND); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | $this->pool->getPropertyAccessor()->setValue($object, $propertyPath, '' !== $value ? $value : null); |
||
| 151 | |||
| 152 | $violations = $this->validator->validate($object); |
||
| 153 | |||
| 154 | if (\count($violations)) { |
||
| 155 | $messages = []; |
||
| 156 | |||
| 157 | foreach ($violations as $violation) { |
||
| 158 | $messages[] = $violation->getMessage(); |
||
| 159 | } |
||
| 160 | |||
| 161 | return new JsonResponse(implode("\n", $messages), Response::HTTP_BAD_REQUEST); |
||
| 162 | } |
||
| 163 | |||
| 164 | $admin->update($object); |
||
| 165 | |||
| 166 | // render the widget |
||
| 167 | // todo : fix this, the twig environment variable is not set inside the extension ... |
||
| 168 | $extension = $this->twig->getExtension(SonataAdminExtension::class); |
||
| 169 | \assert($extension instanceof SonataAdminExtension); |
||
| 170 | |||
| 171 | $content = $extension->renderListElement($this->twig, $rootObject, $fieldDescription); |
||
| 172 | |||
| 173 | return new JsonResponse($content, Response::HTTP_OK); |
||
| 174 | } |
||
| 175 | |||
| 182 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: