nasyrov/laravel-interactions

Laravel package for interactions.

v1.0.5 2018-03-26 11:14 UTC

This package is auto-updated.

Last update: 2024-04-07 15:11:17 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

Laravel package for interactions.

Requirements

Make sure all dependencies have been installed before moving on:

Install

Pull the package via Composer:

$ composer require nasyrov/laravel-interactions

Register the service provider in config/app.php:

'providers' => [
    ...
    Nasyrov\Laravel\Interactions\InteractionServiceProvider::class,
    ...
]

Usage

Let's generate a typical user registration flow with the use of interactions.

First, generate a RegisterUser interaction via the command:

$ php artisan make:interaction RegisterUser

The command above will create a new file app/Interactions/RegisterUser.php. Let's open the file and tailor it for our needs – create a new user:

use App\User;
use Illuminate\Http\Request;
use Nasyrov\Laravel\Interactions\Contracts\Interaction;

class RegisterUser implements Interaction
{
    /**
     * Handle the interaction.
     *
     * @return mixed
     */
    public function handle(Request $request)
    {
        return User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => bcrypt($request->password),
        ]);
    }
}

Next, include the CallsInteractions trait into the base controller so we will be able to run the interactions in any other controller that extends the one:

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Nasyrov\Laravel\Interactions\CallsInteractions;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, CallsInteractions, ValidatesRequests;
}

Finally, in the UsersController controller run the RegisterUser interaction and pass the request object:

use App\Http\Controllers\Controller;
use App\Interaction\RegisterUser;
use Illuminate\Http\Request;

class UsersController extends Controller
{
    public function register(RegisterUserRequest $request)
    {
        return $this->interact(RegisterUser::class, [$request]);
    }
}

Testing

$ composer lint
$ composer test

Security

If you discover any security related issues, please email inasyrov@ya.ru instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.