| Conditions | 12 |
| Paths | 12 |
| Total Lines | 44 |
| Code Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 35 | public function onPreSerialize(ViewEvent $event): void |
||
| 36 | { |
||
| 37 | $controllerResult = $event->getControllerResult(); |
||
| 38 | $request = $event->getRequest(); |
||
| 39 | |||
| 40 | if ($controllerResult instanceof Response || !$request->attributes->getBoolean('_api_respond', true)) { |
||
| 41 | return; |
||
| 42 | } |
||
| 43 | $attributes = RequestAttributesExtractor::extractAttributes($request); |
||
| 44 | |||
| 45 | if (!($attributes = RequestAttributesExtractor::extractAttributes($request)) || |
||
| 46 | //!\is_a($attributes['resource_class'], ResourceFile::class, true) |
||
| 47 | !\is_a($attributes['resource_class'], AbstractResource::class, true) |
||
| 48 | ) { |
||
| 49 | return; |
||
| 50 | } |
||
| 51 | $mediaObjects = $controllerResult; |
||
| 52 | |||
| 53 | if (!is_iterable($mediaObjects)) { |
||
| 54 | $mediaObjects = [$mediaObjects]; |
||
| 55 | } |
||
| 56 | //error_log($request->get('getFile')); |
||
| 57 | $getFile = $request->get('getFile'); |
||
| 58 | //$getFile = true; |
||
| 59 | foreach ($mediaObjects as $mediaObject) { |
||
| 60 | if (!$mediaObject instanceof AbstractResource) { |
||
| 61 | continue; |
||
| 62 | } |
||
| 63 | if ($mediaObject->hasResourceNode()) { |
||
| 64 | $resourceNode = $mediaObject->getResourceNode(); |
||
| 65 | |||
| 66 | $params = [ |
||
| 67 | 'id' => $resourceNode->getId(), |
||
| 68 | 'tool' => $resourceNode->getResourceType()->getTool()->getName(), |
||
| 69 | 'type' => $resourceNode->getResourceType()->getName(), |
||
| 70 | ]; |
||
| 71 | |||
| 72 | $mediaObject->contentUrl = $this->generator->generate('chamilo_core_resource_view_file', $params); |
||
| 73 | |||
| 74 | if ($getFile && |
||
| 75 | $resourceNode->hasResourceFile() && |
||
| 76 | $resourceNode->hasEditableContent() |
||
| 77 | ) { |
||
| 78 | $mediaObject->contentFile = $this->resourceNodeRepository->getResourceNodeFileContent($resourceNode); |
||
| 79 | } |
||
| 84 |