Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions includes/Services/WorkflowEngine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace Escalated\Services;

class WorkflowEngine
{
public const OPERATORS = ['equals', 'not_equals', 'contains', 'not_contains', 'starts_with', 'ends_with', 'greater_than', 'less_than', 'greater_or_equal', 'less_or_equal', 'is_empty', 'is_not_empty'];

public const ACTION_TYPES = ['change_status', 'assign_agent', 'change_priority', 'add_tag', 'remove_tag', 'set_department', 'add_note', 'send_webhook', 'set_type', 'delay', 'add_follower', 'send_notification'];

public const TRIGGER_EVENTS = ['ticket.created', 'ticket.updated', 'ticket.status_changed', 'ticket.assigned', 'ticket.priority_changed', 'ticket.tagged', 'ticket.department_changed', 'reply.created', 'reply.agent_reply', 'sla.warning', 'sla.breached', 'ticket.reopened'];

public function evaluateConditions(array $conditions, array $ticket): bool
{
if (isset($conditions['all'])) {
foreach ($conditions['all'] as $c) {
if (! $this->evaluateSingle($c, $ticket)) {
return false;
}
}

return true;
}
if (isset($conditions['any'])) {
foreach ($conditions['any'] as $c) {
if ($this->evaluateSingle($c, $ticket)) {
return true;
}
}

return false;
}
if (array_is_list($conditions)) {
foreach ($conditions as $c) {
if (! $this->evaluateSingle($c, $ticket)) {
return false;
}
}

return true;
}

return isset($conditions['field']) && $this->evaluateSingle($conditions, $ticket);
}

public function evaluateSingle(array $condition, array $ticket): bool
{
$field = $condition['field'] ?? '';
$operator = $condition['operator'] ?? 'equals';
$expected = $condition['value'] ?? '';
$actual = $ticket[$field] ?? '';

return self::applyOperator($operator, (string) $actual, (string) $expected);
}

public static function applyOperator(string $op, string $actual, string $expected): bool
{
return match ($op) {
'equals' => $actual === $expected,
'not_equals' => $actual !== $expected,
'contains' => str_contains($actual, $expected),
'not_contains' => ! str_contains($actual, $expected),
'starts_with' => str_starts_with($actual, $expected),
'ends_with' => str_ends_with($actual, $expected),
'greater_than' => (float) $actual > (float) $expected,
'less_than' => (float) $actual < (float) $expected,
'greater_or_equal' => (float) $actual >= (float) $expected,
'less_or_equal' => (float) $actual <= (float) $expected,
'is_empty' => trim($actual) === '',
'is_not_empty' => trim($actual) !== '',
default => false,
};
}

public static function interpolateVariables(string $text, array $ticket): string
{
return preg_replace_callback('/\{\{(\w+)\}\}/', function ($m) use ($ticket) {
return $ticket[$m[1]] ?? $m[0];
}, $text);
}

public function dryRun(array $conditions, array $actions, array $ticket): array
{
$matched = $this->evaluateConditions($conditions, $ticket);

return [
'matched' => $matched,
'actions' => array_map(fn ($a) => [
'type' => $a['type'],
'value' => self::interpolateVariables($a['value'] ?? '', $ticket),
'would_execute' => $matched,
], $actions),
];
}
}
61 changes: 61 additions & 0 deletions tests/Test_Workflow_Engine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

use Escalated\Services\WorkflowEngine;
use PHPUnit\Framework\TestCase;

class Test_Workflow_Engine extends TestCase
{
private WorkflowEngine $engine;

protected function setUp(): void
{
$this->engine = new WorkflowEngine;
}

public function test_evaluate_and_conditions()
{
$ticket = ['status' => 'open', 'priority' => 'medium'];
$conditions = ['all' => [
['field' => 'status', 'operator' => 'equals', 'value' => 'open'],
['field' => 'priority', 'operator' => 'equals', 'value' => 'medium'],
]];
$this->assertTrue($this->engine->evaluateConditions($conditions, $ticket));
}

public function test_evaluate_or_conditions()
{
$ticket = ['status' => 'open'];
$conditions = ['any' => [
['field' => 'status', 'operator' => 'equals', 'value' => 'closed'],
['field' => 'status', 'operator' => 'equals', 'value' => 'open'],
]];
$this->assertTrue($this->engine->evaluateConditions($conditions, $ticket));
}

public function test_contains_operator()
{
$this->assertTrue(WorkflowEngine::applyOperator('contains', 'billing issue', 'billing'));
}

public function test_is_empty_operator()
{
$this->assertTrue(WorkflowEngine::applyOperator('is_empty', '', ''));
}

public function test_interpolate_variables()
{
$ticket = ['reference' => 'ESC-001', 'status' => 'open'];
$result = WorkflowEngine::interpolateVariables('Ticket {{reference}} is {{status}}', $ticket);
$this->assertEquals('Ticket ESC-001 is open', $result);
}

public function test_dry_run()
{
$ticket = ['status' => 'open', 'reference' => 'ESC-001'];
$conditions = ['all' => [['field' => 'status', 'operator' => 'equals', 'value' => 'open']]];
$actions = [['type' => 'add_note', 'value' => 'Note for {{reference}}']];
$result = $this->engine->dryRun($conditions, $actions, $ticket);
$this->assertTrue($result['matched']);
$this->assertStringContainsString('ESC-001', $result['actions'][0]['value']);
}
}
Loading