jorenvanhee's avatar

Where do I put this

Hi, I'm having some trouble with deciding where to put several pieces of code.

Image upload and creation of different versions

I've added a code snippet from a method in a controller, where a user can add an image to a 'wedding'. I will also need to create multiple versions of the image (blurred, scaled, ...). Should I create a dedicated class for that? If so, how should I structure this class and where should I put it.

I should also tell you that this is only one of the images that can be uploaded. This is the header image. But there will also be an avatar and more. All the images will have their own set of unique versions.

public function update($user)
{
    $aboutForm = App::make('Trouwen\Forms\About');
    $input = Input::all();

    // Validation
    try
    {
        $aboutForm->validate($input);
    }
    catch (FormValidationException $e)
    {
        return Redirect::back()->withInput()->withErrors($e->getErrors());
    }

    // Pictures
    if (Input::hasFile('picture'))
    {
        $file = Input::file('picture');
        $path = public_path() . '/uploads/weddings/' . $user->username . '/picture/';
        $fileName = $file->getClientOriginalName();

        File::exists($path) or File::makeDirectory($path, 0755, true);

        $image = Image::make($file);
        $image->save($path . $fileName);

        $input['picture'] = $fileName;
    }
    else {
        unset($input['picture']);
    }

    $wedding = $this->weddingRepo->getByUserId($user->id);

    $success = $this->weddingRepo->updateById($wedding->id, $input);

    return Redirect::back()->withSuccess($success);
}

Deletion of a user with its wedding and all the pictures linked to that.

Where should I put that? A DeleteUser class?

Thanks!

0 likes
5 replies
jorenvanhee's avatar

Thanks for the quick replies!

@AlnourAltegani I guess this is for the "deletion of a user"? Do you have an example of a similar service class? I have never used service classes before as this is my first medium sized project.

@pmall Using Laravel 4.2 here, need to get this project finished by the end of the week so I guess I'll stick with that :p

NoorDeen's avatar
Level 6

simply make new service class like this :

namespace Users;

use UserRepository;
use WiddingRepository;

class Service {

    public function uploadWidding($photo) {
        // do what you need 
        return $widdingEntity;
    }

    public function deletUser($id) {
        // delete it here
    }

}

in the controller inject the users service

use Users\Service;

class UsersController {

    protected $service
    public function __construct(Service $service) {
        $this->service = $service;
    }

    public function update($user) {
        // validation here
        if (Input::hasFile('picture')) {
            $widding = $this->service->uploadWidding();
        } else {
            $widding = $this->widdingRepo->getByUserId($user);
        }
        // here do what you need
    } 

1 like
jorenvanhee's avatar

@AlnourAltegani Thanks a lot! I'll try that. I think I'll make 2 classes. One for the user (delete), and one for the wedding images.

Please or to participate in this conversation.