Use PHP match for explicit import states, with a deliberate failure case
An import can be queued, running, finished, or failed. An unexpected upstream state should not accidentally display as success. A match expression makes the mapping compact, but the fallback still needs a conscious decision.
Make the unknown case visible
<?php
function statusLabel(string $status): string
{
return match ($status) {
'queued' => 'Waiting',
'running' => 'Importing',
'finished' => 'Complete',
'failed' => 'Needs attention',
default => throw new UnexpectedValueException('Unknown import state'),
};
}
assert(statusLabel('finished') === 'Complete');
try {
statusLabel('almost-done');
throw new RuntimeException('Expected unknown status to fail');
} catch (UnexpectedValueException) {
echo "unknown state rejected", PHP_EOL;
}
For this internal mapping, an exception exposes a contract mismatch. For a public status page, you might instead display "Status unavailable" and record an internal alert. The correct choice depends on whether the caller can recover.
Keep transport values out of the domain
Normalize the external service's vocabulary at the integration boundary. If one supplier says "done" and another says "completed", map both into your application's documented state. Do not make every template understand every supplier's spelling.
The display label should not drive business decisions. Persist a stable state value and translate that value into human-facing text at the edge. Changing "Needs attention" to "Action required" should not alter a query.
Test all known states and one unknown state
The negative case is useful because upstream contracts grow. If a new state arrives, you want a visible decision about handling it rather than an accidental success label. Test capitalization too if the provider claims case-sensitive values.
Unlike a loose comparison, match uses strict identity. That matters when handling mixed external values, although this function already narrows its input to a string. The PHP match reference explains identity comparison and unhandled cases.