haheute haheute -4 years ago 129
PHP Question

Use a repository, event or service provider here?

I have this function in an

UploadController
, which is used to cancel an upload, and I am trying to improve it.

This
"File::delete..block"
exists twice in my application. Would it for example make sense to exclude this block from the controller? And if yes, what should I use,
Repository
,
Service Provider
or an
Event
?

public function postDelete(Request $request)
{
$filename = $request->input('filename');

$upload = Upload::where('filename',$filename)->where('accepted',0)->firstOrFail();

File::delete('img/uploads/'.$filename.'_o.jpg');
File::delete('img/uploads/'.$filename.'.jpg');
File::delete('img/uploads/'.$filename.'_zoom.jpg');
File::delete('img/uploads/'.$filename.'_tn.jpg');
File::delete('img/uploads/'.$filename.'_250.jpg');
File::delete('img/uploads/'.$filename.'_50.jpg');

$upload->delete();

Cache::forget('waiting_uploads');

$msg = 'upload has been deleted';
Mail::to('xyz@xxx.tld')->send(new TextMail($msg));

return redirect('upload');
}

Answer Source

I would put that in a service that calls a repository. That way your controller does not interact with a data-layer object and everything is separated cleanly.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download