nasyrov / laravel-interactions by nasyrov

Laravel package for interactions.
125
6
1
Package Data
Maintainer Username: nasyrov
Maintainer Contact: inasyrov@ya.ru (Evgenii Nasyrov)
Package Create Date: 2017-06-13
Package Last Update: 2018-04-04
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-11-22 03:16:41
Package Statistics
Total Downloads: 125
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 6
Total Watchers: 1
Total Forks: 5
Total Open Issues: 0

Laravel Interactions

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.