Skip to content

Commit

Permalink
feat(event): add interruptable contract for events which provides eve…
Browse files Browse the repository at this point in the history
…nts with a mean to interrupt the event chain and provide a reason for it
  • Loading branch information
JoelAlphonso committed Nov 8, 2022
1 parent 4742d98 commit 4c4d5b3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/event/src/Charcoal/Event/InterruptableEventInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Charcoal\Event;

use Stringable;

/**
* Interface: InterruptableEventInterface
* @package Charcoal\Event
*/
interface InterruptableEventInterface
{
public function isInterrupted(): bool;

/**
* @return string|Stringable
*/
public function reason();
}
43 changes: 43 additions & 0 deletions packages/event/src/Charcoal/Event/InterruptableEventTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Charcoal\Event;

use Stringable;

/**
* Trait: InterruptableEventTrait
* @package Charcoal\Event
*/
trait InterruptableEventTrait
{
private bool $interrupted = false;

/**
* @var string|Stringable
*/
private $reason;

/**
* @param string|Stringable $reason
* @return void
*/
public function interrupt($reason)
{
$this->reason = $reason;

$this->interrupted = true;
}

public function isInterrupted(): bool
{
return $this->interrupted;
}

/**
* @return string|Stringable
*/
public function reason()
{
return $this->reason;
}
}

0 comments on commit 4c4d5b3

Please sign in to comment.