-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(event): add interruptable contract for events which provides eve…
…nts with a mean to interrupt the event chain and provide a reason for it
- Loading branch information
1 parent
4742d98
commit 4c4d5b3
Showing
2 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
packages/event/src/Charcoal/Event/InterruptableEventInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
43
packages/event/src/Charcoal/Event/InterruptableEventTrait.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |