Do this workflow for the very important and complicated things in your system that have many operations
Instead of mudding our controller with many operations:
class PurchasesController extends Controller
{
public function store()
{
// different
// operations
// for purchasing
// something
return redirect('/');
}
}
We can use "use cases":
class PurchasesController extends Controller
{
public function store()
{
PurchaseSomething::preform();
return redirect('/');
}
}
abstract class UseCase
{
public static function preform()
{
return (new static)->handle();
}
abstract public function handle();
}
// use case model
class PurchaseSomething extends UseCase
{
public function handle()
{
$this->preparePurchase()
->sendEmail();
}
protected function preparePurchase()
{
var_dump('preparing the purchase');
return $this;
}
protected function sendEmail()
{
var_dump('sending email');
return $this;
}
}