mardi 10 décembre 2019

Laravel responsibility in the classes

I have a project on Laravel and need to do refactoring. I'm 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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire