mardi 10 novembre 2015

Lines of code duplicated in Controller, where to create a method with them?

Aloha, I have two methods in my Controller, one for setting the profile picture and the other for updating it. I'm using this lines of code in both methods:

    $user = Auth::user();

    if (Input::file('image')) {
        $image = Image::make(Input::file('image'));
        $fullName = Input::file('image')->getClientOriginalName();
        $extension = Input::file('image')->getClientOriginalExtension();

        $pathToCreate = public_path() .'/images/'. $user->email . '/';
        $fullPath = $pathToCreate . $user->email . '.' . $extension;
        $pathDatabase = 'images/' . $user->email . '/' . $user->email . '.' .$extension;

        // Creating directory if it does not exists
        File::exists($pathToCreate) or File::makeDirectory($pathToCreate);
        $image->resize(null, 145, function ($constraint) { $constraint->aspectRatio(); })
            ->crop(130,130)
            ->save($fullPath);
        $user->picture = $pathDatabase;

I want to create a method with this lines of code but I feel that the controller is not a good place for it. Where should I place this method?

Aucun commentaire:

Enregistrer un commentaire