JackJones's avatar

Tidying up my Controller

I've watched possibly too many Laracast videos and in Jeffrey's voice I can hear my "controller feels like its getting a bit gross"

    public function storeProfilePicture(Request $request, ImageManager $imagemanager)
    {
        /*
         * We're using Dropzone.js so we need to manually return the validation input
         */
        $validator = Validator::make($request->file(), ['file' => ['required',
                                                                   'max:' . Setting::get('usercp_profile_picture_max_filesize'),
                                                                   'mimes:' . implode(',', json_decode(Setting::get('usercp_profile_picture_valid_filetypes')))
                                                                   ]]);


        if($validator->fails())
            return response()->json($validator->messages()->all(), 409);

        $image = $imagemanager->make($request->file('file'), [  'width' => Setting::get('usercp_profile_picture_width'),
                                                                'height' => Setting::get('usercp_profile_picture_height'),
                                                                'convert_to' => Setting::get('usercp_profile_picture_convert_to'),
                                                                'quality' => Setting::get('usercp_profile_picture_quality'),
                                                                'dir' => Setting::get('usercp_profile_picture_directory') . '/' . auth()->user()->id,
                                                                'disk' => Setting::get('usercp_profile_picture_disk')
                                                                ]);

        if(Setting::get('usercp_profile_picture_create_tn'))
            $tn = $imagemanager->make($request->file('file'), [ 'width' => Setting::get('usercp_profile_picture_tn_width'),
                                                                'height' => Setting::get('usercp_profile_picture_tn_height'),
                                                                'convert_to' => Setting::get('usercp_profile_picture_tn_convert_to'),
                                                                'quality' => Setting::get('usercp_profile_picture_tn_quality'),
                                                                'dir' => Setting::get('usercp_profile_picture_directory') . '/' . auth()->user()->id . '/' . Setting::get('usercp_profile_picture_tn_directory'),
                                                                'prefix' => Setting::get('usercp_profile_picture_tn_prefix')
                                                                ]);
        else
            $tn = null;

        $this->saveProfilePictureToDb($image, $tn);
    }

I basically have an image manager that deals with uploading my images, because unfortunately Intervention/Image isn't yet compatible with UploadedFile->store(), so I'm having to call the ->save() method, which is a bit of a nuisance to say the least

Anyways, my thumbnails for avatars use certain parameters, my other images use the same class, as do other various image uploads

My question boils down to this:

Should I just make a trait ImageManager, with the save and make methods on it, and then extend it to use my Setting's for avatar's, forum pictures, listing pictures etc, and then simply inject each specific type that I need into the method, or is there a better way?

I could could enter each type into a config file, but I don't really like the idea of resolving database options into a config file

How do other people deal with resolving similar classes that have subtle differences?

Thanks Lee

0 likes
1 reply
michimawan's avatar

I won't make the controller has a responsibility to save the profile picture, I would like to do it in a separate class that receive request object and return the success status when storing the image instead

1 like

Please or to participate in this conversation.