0

I'm working on a piece of functionality that simply allows a guest user to perform an action a certain number of times before requiring them to login\create account. In this instance, they can vote on photos in a gallery 5 times before the app asks them to create an account.

My solution is roughly as follows.

  1. On initial load, set cookie with vote_count=1;
  2. retrieve and increment vote_count each time a user votes;
  3. When vote_count=5, redirect user to page asking them to login or sign up. (of course the user can delete their cookies or use another browser etc, but I'm not concerned with that right now).

My problem is where to implement this code.

  1. Even though I will only call this feature on one route, I feel the controller is the wrong place because it adds quite a bit of code, and if I change the number of times a user can vote, I'm changing code in the controller (maybe that's not a bad thing?)
  2. I don't believe this should live in a helpers.php file because it's not needed anywhere else in the app.
  3. I guess I could create it's own class and it could live as a static method, but I feel static methods are best left for functionality in which they will always return the same value and have the same result regardless of where they are called.

To put this into a good question. What is a good OOP way of implementing this that fits in line with Laravel?

  • Suppose I am not registered and I try to take an action that always requires the user to be logged in. How would the app react in that case and where do you perform the check for a logged-in user? Is there any reason the "unregistered vote limit" can't be treated similarly? – Bart van Ingen Schenau Jan 16 '16 at 9:04
  • There is an auth controller (the built in Laravel Auth Controller) that is used as a middleware piece. The reason I didn't think to put this there, is that I'm only concerned with one route\url. – dangel Jan 17 '16 at 0:25
  • if you're gonna down vote, at least comment why – dangel Feb 8 '16 at 0:31
1

Although I don't have experience with Laravel, but I do have solid Symfony2 background and I don't know how much the framework matters in this question I would say that the proper OOP solution is to create a class that implements the logic needed.

Putting this code in controller seems to you as a bad idea, because the controller is an integration layer so no business logic should be placed in it. Of course you can always compromise depending in situation.

And bout should it be static method or not - check this answer: https://stackoverflow.com/a/2671636/1904606

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.