Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel responsibility in the classes

I have a project on Laravel and need to do refactoring. I've read about Service provider and Dependency injection and have some questions. This is a short structure: user model, event model, favorite user model and etc. Also, there are controllers for all models. Every event has a creator and client (user relationship). In every class, I am injecting appropriate service: User Service, Event service, Favorite user service and etc. Let's consider the example - I want to delete the user:

class UserController extends Controller
{
    /**
     * @var UserService $userService
     */
    protected $userService;

    /**
     * UserController constructor.
     * @param UserService $userService
     */
    public function __construct(UserService $userService)
    {
        $this->userService = $userService;
    }
    protected function delete(int $id)
    {
        $user = User::find($id);
        if ($user) {
            $this->userService->setUser($user);
            $this->userService->delete();
        }
    }

Inside User service, I am processing user deleting - update the appropriate field. Also, I need to cancel all user events and delete favorite users. My question is where should I do it? Should I inject event and favorite user service in UserController or in UserService? Or maybe there is a better way to do this action. Thx in advance

like image 596
V-K Avatar asked Dec 10 '19 13:12

V-K


People also ask

What is the role of Laravel developer?

Laravel developers use the Laravel web framework to design and build web applications, services, sites, and tools. Laravel is a PHP-based, MVC architecture that relies on OOP to create websites, databases, forums, and caches.

What is single responsibility principle in Laravel?

The "S" in SOLID stands for single responsibility principle which requires that each module or class should have a single responsibility for the functionality of the application. A more subtle understanding is put forward by Robert C Martin who says that "A class should have only one reason to change".

Who is Laravel engineer?

A Laravel developer is in charge of designing and maintaining databases and must perform backend and User Interface tests on applications to optimize performance. They also collaborate and work with other developers in the company to fill data acquisition requirements.


1 Answers

Seems like you have many actions depending on deleting user, so I would consider using Events and inside each listener handle the specifics of it.

class UserController extends Controller
{
    /**
     * @var UserService $userService
     */
    protected $userService;

    /**
     * UserController constructor.

     * @param UserService $userService
     */
    public function __construct(UserService $userService)
    {
        $this->userService = $userService;
    }

    protected function delete(int $id)
    {
        if(!$this->userService->delete($id)) {
          // return some error;
        }

        event(new UserWasRemoved($id));
        // return success response
    }
class DeleteUserService {

    protected $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }

    public function delete($id){
       return $this->user->delete($id);
    }
}
// app/Providers/EventServiceProvider

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        UserWasRemoved::class => [
            CancelUserEvents::class,
            RemoveUserFavorites::class,
            // etc...
        ],
    ];
}
like image 133
Helder Lucas Avatar answered Oct 08 '22 23:10

Helder Lucas