Skip to content

Commit

Permalink
Merge pull request #5 from michael-rubel/feature/events
Browse files Browse the repository at this point in the history
Events
  • Loading branch information
michael-rubel committed Aug 10, 2022
2 parents b7653b9 + 11a722b commit a2f1c7e
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 4 deletions.
11 changes: 11 additions & 0 deletions src/EnhancedPipelineServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MichaelRubel\EnhancedPipeline;

use Illuminate\Events\EventServiceProvider;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

Expand All @@ -20,4 +21,14 @@ public function configurePackage(Package $package): void
{
$package->name('laravel-enhanced-pipeline');
}

/**
* @return void
*/
public function registeringPackage(): void
{
if (! $this->app->bound('events')) {
$this->app->register(EventServiceProvider::class, true);
}
}
}
15 changes: 15 additions & 0 deletions src/Events/PipePassed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace MichaelRubel\EnhancedPipeline\Events;

class PipePassed
{
/**
* @param mixed $pipe
* @param mixed $passable
*/
public function __construct(public $pipe, public $passable)
{
//
}
}
15 changes: 15 additions & 0 deletions src/Events/PipeStarted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace MichaelRubel\EnhancedPipeline\Events;

class PipeStarted
{
/**
* @param mixed $pipe
* @param mixed $passable
*/
public function __construct(public $pipe, public $passable)
{
//
}
}
17 changes: 14 additions & 3 deletions src/Pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
use Illuminate\Container\Container as ContainerConcrete;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Pipeline\Pipeline as PipelineContract;
use MichaelRubel\EnhancedPipeline\Traits\HandlesDatabaseTransactions;
use MichaelRubel\EnhancedPipeline\Events\PipePassed;
use MichaelRubel\EnhancedPipeline\Events\PipeStarted;
use MichaelRubel\EnhancedPipeline\Traits\HasDatabaseTransactions;
use MichaelRubel\EnhancedPipeline\Traits\HasEvents;
use RuntimeException;
use Throwable;

class Pipeline implements PipelineContract
{
use HandlesDatabaseTransactions;
use HasDatabaseTransactions, HasEvents;

/**
* The container implementation.
Expand Down Expand Up @@ -191,11 +194,17 @@ protected function carry()
{
return function ($stack, $pipe) {
return function ($passable) use ($stack, $pipe) {
$this->fireEvent(PipeStarted::class, $pipe, $passable);

if (is_callable($pipe)) {
// If the pipe is a callable, then we will call it directly, but otherwise we
// will resolve the pipes out of the dependency container and call it with
// the appropriate method and arguments, returning the results back out.
return $pipe($passable, $stack);
$result = $pipe($passable, $stack);

$this->fireEvent(PipePassed::class, $pipe, $passable);

return $result;
} elseif (! is_object($pipe)) {
[$name, $parameters] = $this->parsePipeString($pipe);

Expand All @@ -216,6 +225,8 @@ protected function carry()
? $pipe->{$this->method}(...$parameters)
: $pipe(...$parameters);

$this->fireEvent(PipePassed::class, $pipe, $passable);

return $this->handleCarry($carry);
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Illuminate\Support\Facades\DB;

trait HandlesDatabaseTransactions
trait HasDatabaseTransactions
{
/**
* Determines whether class uses transaction.
Expand Down
50 changes: 50 additions & 0 deletions src/Traits/HasEvents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace MichaelRubel\EnhancedPipeline\Traits;

trait HasEvents
{
/**
* Determines whether pipeline uses events.
*
* @var bool
*/
protected bool $useEvents = false;

/**
* Enable events in pipeline.
*
* @return static
*/
public function withEvents(): static
{
$this->useEvents = true;

return $this;
}

/**
* Fire the event if enabled.
*
* @param string $event
* @param string|callable|mixed $pipe
* @param mixed $passable
*
* @return void
*/
protected function fireEvent(string $event, $pipe, $passable): void
{
if (! $this->useEvents) {
return;
}

if (is_object($pipe)) {
/** @var object $pipe */
$pipe = $pipe::class;
}

event(new $event($pipe, $passable));
}
}
78 changes: 78 additions & 0 deletions tests/PipelineEventsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace MichaelRubel\EnhancedPipeline\Tests;

use Illuminate\Support\Facades\Event;
use MichaelRubel\EnhancedPipeline\Events\PipePassed;
use MichaelRubel\EnhancedPipeline\Events\PipeStarted;
use MichaelRubel\EnhancedPipeline\Pipeline;

class PipelineEventsTest extends TestCase
{
public function setUp(): void
{
parent::setUp();

Event::fake();
}

/** @test */
public function testFiresPipeStartedEvents()
{
app(Pipeline::class)
->withEvents()
->send('data')
->through([
TestPipe::class,
TestPipe::class,
])
->thenReturn();

Event::assertDispatched(function (PipeStarted $event) {
$this->assertInstanceOf(TestPipe::class, app($event->pipe));
$this->assertSame('data', $event->passable);

return true;
}, 2);
}

/** @test */
public function testFiresPipeStartedEventsButFailsToPass()
{
app(Pipeline::class)
->withEvents()
->send('data')
->through(PipelineWithException::class)
->onFailure(fn () => true)
->thenReturn();

Event::assertDispatched(function (PipeStarted $event) {
$this->assertInstanceOf(PipelineWithException::class, app($event->pipe));
$this->assertSame('data', $event->passable);

return true;
});

Event::assertNotDispatched(PipePassed::class);
}

/** @test */
public function testFiresPipePassedEvents()
{
app(Pipeline::class)
->withEvents()
->send('data')
->through([
TestPipe::class,
TestPipe::class,
])
->thenReturn();

Event::assertDispatched(function (PipePassed $event) {
$this->assertInstanceOf(TestPipe::class, app($event->pipe));
$this->assertSame('data', $event->passable);

return true;
}, 2);
}
}

0 comments on commit a2f1c7e

Please sign in to comment.