Skip to content

Latest commit

 

History

History
70 lines (55 loc) · 1.16 KB

use_cases.md

File metadata and controls

70 lines (55 loc) · 1.16 KB

Use cases

Do this workflow for the very important and complicated things in your system that have many operations

Don't unit test it, it's hard for testing

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;
    }
}