1. Go to this page and download the library: Download helvetitec/flowengine library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
helvetitec / flowengine example snippets
namespace App\Models;
use Helvetitec\FlowEngine\Traits\HasFlowRuns;
use Illuminate\Database\Eloquent\Model;
class Chat extends Model
{
use HasFlowRuns;
}
namespace App\Flows;
use Helvetitec\FlowEngine\FlowEngine;
class ChatFlow extends FlowEngine
{
protected function doRun(mixed $input): void
{
match ($this->subject()->getStateKey()) {
'start' => $this->start(),
'waiting' => $this->handleAnswer($input),
default => $this->reset(),
};
}
private function start(): void
{
// Send the available options to the user here.
$this->set('options', [1, 2])
->transitionAndStop('waiting');
}
private function handleAnswer(mixed $input): void
{
if (! in_array($input, $this->get('options', []), true)) {
$this->stop();
}
// Handle the selected option here.
$this->delete('options')
->transitionAndStop('done');
}
}
namespace App\Flows;
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Helvetitec\FlowEngine\Contracts\FlowSubject;
use Helvetitec\FlowEngine\FlowEngine;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Session;
final class SessionFlowSubject implements FlowSubject
{
public function __construct(
private string $state = 'start',
private ?array $context = null,
private bool $active = true,
private Carbon|CarbonImmutable|null $cooldown = null,
) {}
public function getOwner(): ?Model { return null; }
public function getActive(): bool { return $this->active; }
public function setActive(bool $active): void { $this->active = $active; }
public function getStateKey(): string { return $this->state; }
public function setStateKey(string $state): void { $this->state = $state; }
public function getContext(): ?array { return $this->context; }
public function setContext(?array $context): void { $this->context = $context; }
public function getCooldown(): Carbon|CarbonImmutable|null { return $this->cooldown; }
public function setCooldown(Carbon|CarbonImmutable|null $until): void { $this->cooldown = $until; }
public function persist(): void
{
//In this example we use a simple session storage to store the data.
Session::put('flow_subject_'.$this->id, [
'state' => $this->state,
'context' => $this->context,
'active' => $this->active,
'cooldown' => $this->cooldown?->toDateTimeString()
]);
}
// With this we will be able to manually load the stored subject by id or create a new one
public static function load(string $id): self
{
$data = Session::get('flow_subject_'.$id, [
'state' => 'start',
'context' => null,
'active' => true,
'cooldown' => null
]);
return new self(
id: $id,
state: $data['state'],
context: $data['context'],
active: $data['active'],
cooldown: $data['cooldown'] ? Carbon::parse($data['cooldown']) : null
);
}
}
final class SessionFlow extends FlowEngine
{
protected function doRun(mixed $input): void
{
if ($this->subject()->getStateKey() === 'start') {
$this->transitionAndStop('complete');
}
$this->stop();
}
}
$subject = SessionFlowSubject::load('subject_id');
(new SessionFlow)->run($subject, $input);
$this->cooldown(now()->addMinutes(10))->stop();
$this->pause(now()->addMinutes(5)); // Set cooldown and stop
$this->deactivate(); // Deactivate and stop
$this->reset(now()->addHour(), deleteContext: true);
use Helvetitec\FlowEngine\Models\FlowRun;
FlowRun::clear(\App\Flows\ChatFlow::class, now()->subMonth());
FlowRun::clearAll(now()->subMonth());