Skip to content

Latest commit

 

History

History
69 lines (58 loc) · 1.01 KB

spliting_tasks_into_steps.md

File metadata and controls

69 lines (58 loc) · 1.01 KB

Spliting tasks into steps (sort of chain of responsibility)

If we have a class that has many smaller tasks that it needs to do:

class Thing
{
    public function handle()
    {
        // doThis
        // doThat
        // runSomething
        // eraseSomething
        // addSomething
    }
}

We can instead extract each step into it's own class and then filter through an array of these bite-sized chunks and trigger them

class Thing
{
    public function handle()
    {
        $tasks = [
            DoThis::class,
            DoThat::class,
            AddFooToBar::class,
            RunSomething::class
        ];

        foreach ($tasks as $task) {
            (new Task)->handle();
        }
    }
}

// we can make them adhere to some interface
class DoThis
{
    public function handle()
    {

    }
}

class DoThat
{
    public function handle()
    {

    }
}

class AddSomething
{
    public function handle()
    {

    }
}

class RunSomething
{
    public function handle()
    {

    }
}