user service class
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!
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
}
Please or to participate in this conversation.