2

I have an upload method that may have different kinds of implementations (Switched to another different image service).

I use that method on different controllers that needs it. I'm not sure if it's Service Container I need or a Trait and be consistent as it can't have an interface or just a simple class.

Generally, when should I use what?

-- EDIT --

I am currently trying to refactor my code to follow the DRY principle. Finding myself repeating the upload method everywhere. I currently use Tinify (laravel package for tinify).

The upload method currently does:

  • Get the source file (from the input)
  • Sets how the image should look like (Resized or cropped) and its dimensions.
  • Actual saving where.

Am I over DRY-ing here? Even if I do, that's just a simple case/example. I still need a rule of thumb/guideline on when to use what?

When is it generally acceptable to use Classes only (with/without interfaces)? Traits? Or a Service Container?

It's not only the upload I'll be using this obviously and that's why I need answers.

-- EDIT --

I am not sure what's still not clear here, but all I am asking is:

When should I use what? I know we can do same stuffs on any of the three, but for sure there's a proper one out of the three.

jen
  • 218
  • 4
  • 13
  • Can you be more specific what your actual problem is. The question as it is written does not have enough context to be answered. – samrap Oct 01 '16 at 00:47
  • It's very hard to define general rules for that. Try to define - what are the dependencies of your method - e.g. what does it need to work? It might be the case that part is coupled to http request (so it's controller's part, can be helper or trait) and another part is independent from http context (might be used in cron), so it might become a service. – Ilia Kondrashov Oct 01 '16 at 03:53
  • Mmm currently use it in my controller. I have forms on different page that requires an image, and process it. It's just the uploaded directory that changes and sometimes the size of the image. Some image only requires resizing, others need cropping. Did i gave enough information already? – jen Oct 01 '16 at 05:16
  • Can you show some code related to your issue? And try to add the additional information in the question so people don't need to go through the comments – baikho Oct 01 '16 at 15:11

0 Answers0