Status as a state machine, not a string
If a ticket's status is just a column that any code can set, you get impossible histories: a ticket marked closed and then started again with no reopen, or a customer closing a ticket that was never resolved. A state machine makes the allowed moves explicit and refuses the rest. Symfony's Workflow component does this in configuration:
framework:
workflows:
ticket_status:
type: state_machine
marking_store:
type: method
property: status
supports:
- App\Entity\Ticket
initial_marking: open
places: [open, in_progress, resolved, closed]
transitions:
start:
from: open
to: in_progress
resolve:
from: [open, in_progress]
to: resolved
close:
from: resolved
to: closed
reopen:
from: [resolved, closed]
to: open
The whole lifecycle is readable in twenty lines: open, in_progress, resolved, closed, and a reopen that returns a resolved or closed ticket to open. Code asks the workflow whether a transition is allowed instead of re-implementing the rules in controllers.
Who may do what: a Voter
The state machine says which moves are possible. A Voter says who may attempt them. DevDesk's TicketVoter expresses the access rules in one place:
- Customers can view their own tickets, and comment only while a ticket is still active.
- Agents can work tickets assigned to them or still unassigned.
- Admins can do everything.
return match ($attribute) {
self::VIEW => $isOwner,
self::COMMENT => $isOwner && $subject->getStatus()->isActive(),
default => false,
};
Because customers can never reach MANAGE, they cannot assign tickets or change priority, however they craft a request. Controllers call denyAccessUnlessGranted() and stay thin.
An SLA clock that notifies exactly once
Each priority has a first-response deadline. The first public agent reply stops the clock. A console command, or a protected cron endpoint on serverless hosting, looks for tickets that missed the deadline and notifies agents. The important detail is the order of operations:
public function run(?DateTimeImmutable $now = null): int
{
$breaches = $this->tickets->findNewSlaBreaches($now ?? new DateTimeImmutable());
foreach ($breaches as $ticket) {
$ticket->markSlaBreachNotified();
$this->em->flush(); // flag first so a failing dispatch never re-notifies in a loop
$this->bus->dispatch(new TicketSlaBreached((int) $ticket->getId()));
}
return \count($breaches);
}
The ticket is flagged as notified and flushed before the message is dispatched. If dispatching fails, the worst outcome is one missed notification that you can see in the logs, rather than the same alert firing on every run. Choosing which failure you can live with is the real design decision here.
Do the slow part asynchronously
The actual email goes through Symfony Messenger on a Doctrine transport, with retries and a failure transport. The request that creates a ticket does not wait for a mail server. This is also what makes the SLA command safe to run often.
Keep it honest with tests and static analysis
DevDesk has PHPUnit unit and functional tests (the database is rolled back per test) and runs PHPStan at level 8, PHP-CS-Fixer and composer audit in GitHub Actions. A state machine is easy to test: assert that each allowed transition works and each forbidden one is refused.
Checklist
- Model lifecycle as a state machine so impossible moves are rejected centrally
- Put authorization in a Voter, not scattered across controllers
- Flag before you notify, so a failure cannot cause an alert loop
- Send email through a queue with retries and a failure transport
- Run static analysis and tests in CI on every push
Read the full write-up, run the app with Docker, or try the demo in the DevDesk case study. If you need a PHP backend with this kind of structure, see my REST API development service.
Original article written by Saad Ali Khan. Code samples are illustrative and free to adapt in your own projects.