mardi 2 mai 2017

How to organize or refactor this code?

I have been looking at some coding videos on Laracasts and the focus seems to be very readable code and keeping the controller clean. How can I clean this up?

public function update(Request $request, $id)
{
    $this->validate($request, [
    'avatar' => 'required|image|max:10000|mimes:jpg,jpeg,gif,bmp,png'
    ]);

    $UploadedFile = $request->file('avatar');

    $name = renameFile($UploadedFile); // helper function.

    $user = User::where('id', '=', $id)->first();

    if(!is_null($user->avatar)){ // avatar's name stored in user table

        Storage::disk('public')->delete('avatars/' . $user->avatar);
    };

    $image = Image::make($UploadedFile)
        ->resize(400, null, function ($constraint) { $constraint->aspectRatio(); } )
        ->encode('jpg', 80);

    Storage::disk('public')->put('avatars/' . $name, $image);

    $user->avatar = $name;

    $user->save();

    return back()->with(message('User Profile Photo has been updated!', 'success'));

};

Aucun commentaire:

Enregistrer un commentaire