vertaalbureau-perfect/action-pattern

Action for small code bits, usable in laravel

v0.2.5 2023-11-06 08:13 UTC

This package is auto-updated.

Last update: 2025-05-06 12:00:45 UTC


README

Actions are meant to be very small, single purpose, bits of code.

Because of the small footprint actions are easy reusable.

Example use cases are:

  • implementing third paty api calls
  • Simple routine jobs

Installation

composer require vertaalbureau-perfect/action-pattern

Creating an Action

php artisan make:action [ActionName]

You can also create actions in sub namespaces:

php artisan make:action [SubNamespace]/[ActionName]

Actions are placed in your app/Actions folder.

Usage

Imagine a simple action to write something to the log:

class CreateLogAction extends AbstractAction
{
    public function handle($message)
    {
        Log::debug($message);
    }
}

And to use it we would simply call:

CreateLogAction::execute('Write me to the log please!');

Testing

For Testing purposes you can use the static fake method.

CreateLogAction::fake();

If your code depends on a return value from the action you can provide the return value in the fake method.

CreateLogAction::fake('Log Success');