| Conditions | 10 |
| Paths | 12 |
| Total Lines | 49 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 110 |
| 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 |
||
| 30 | public function __invoke(?object $data, Request $request, UploadableFileManager $uploadableFileManager, PublishableStatusChecker $publishableStatusChecker, PublishableNormalizer $publishableNormalizer) |
||
| 31 | { |
||
| 32 | $contentType = $request->headers->get('CONTENT_TYPE'); |
||
| 33 | if (null === $contentType) { |
||
|
1 ignored issue
–
show
|
|||
| 34 | throw new UnsupportedMediaTypeHttpException('The "Content-Type" header must exist.'); |
||
| 35 | } |
||
| 36 | |||
| 37 | $contentType = explode(';', $contentType)[0]; |
||
| 38 | $formats = ['multipart/form-data']; |
||
| 39 | if (!\in_array(strtolower($contentType), $formats, true)) { |
||
| 40 | throw new UnsupportedMediaTypeHttpException(sprintf('The content-type "%s" is not supported. Supported MIME type is "%s".', $contentType, implode('", "', $formats))); |
||
| 41 | } |
||
| 42 | |||
| 43 | $resourceClass = $request->attributes->get('_api_resource_class'); |
||
| 44 | $resource = $data ?? new $resourceClass(); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * if it IS publishable |
||
| 48 | * if NOT asking to update published ?published=true |
||
| 49 | * if it IS currently published |
||
| 50 | * if the user DOES have permission. |
||
| 51 | */ |
||
| 52 | $publishableAnnotationReader = $publishableStatusChecker->getAnnotationReader(); |
||
| 53 | if ($publishableAnnotationReader->isConfigured($resource)) { |
||
| 54 | $configuration = $publishableAnnotationReader->getConfiguration($resource); |
||
| 55 | $isGranted = $publishableStatusChecker->isGranted($resource); |
||
| 56 | if (!$data) { |
||
| 57 | if (!$isGranted) { |
||
| 58 | $accessor = PropertyAccess::createPropertyAccessor(); |
||
| 59 | $accessor->setValue($resource, $configuration->fieldName, date('Y-m-d H:i:s')); |
||
| 60 | } |
||
| 61 | } elseif ( |
||
| 62 | $isGranted && |
||
| 63 | !$publishableStatusChecker->isPublishedRequest($request) && |
||
| 64 | $publishableStatusChecker->isActivePublishedAt($resource) |
||
| 65 | ) { |
||
| 66 | $resource = $publishableNormalizer->createDraft($resource, $configuration, $resourceClass); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | try { |
||
| 71 | $uploadableFileManager->setUploadedFilesFromFileBag($resource, $request->files); |
||
| 72 | } catch (InvalidArgumentException $exception) { |
||
| 73 | throw new UnprocessableEntityHttpException($exception->getMessage()); |
||
| 74 | } |
||
| 75 | |||
| 76 | $request->attributes->set('data', $resource); |
||
| 77 | |||
| 78 | return $resource; |
||
| 79 | } |
||
| 81 |