3 times you code the same lignes :

    // Get helper service.
    $image_styles_mapping_service = \Drupal::service('image_styles_mapping.image_styles_mapping_service');

a new function like

  protected function getImageStyleMappingService() {
   return \Drupal::service('image_styles_mapping.image_styles_mapping_service');
 }

can resolve this (and give you autocomplete with correct documentation)

Comments

grimreaper’s picture

Hello,

Thanks for pointing that. But if there is a function to get the service the function will be also be called 3 times but is it better because it is in a function.

And what about adding an attribute to the controller to store the service, so I could reuse it between the controller functions?

ilmir’s picture

Yes, you're right, it's a better way.

  • Grimreaper committed cfec464 on 8.x-1.x
    Issue #2510218 by ilmir, Grimreaper: Controller duplicate code
    
grimreaper’s picture

Status: Active » Fixed

Hello,

This is commited now.

Thanks.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.