Result
Netson\Platform\Result · All classes
Outcome of a platform operation. Operations never throw on platform trouble; they return one of these.
Three outcomes: ok (done), unavailable (the feature is off here, nothing was
done: normal on a laptop, in CI or on a site without the feature) and failed
(tried and failed; reason says why). Your app should carry on in all three: log a
failed if you like, never show it to a visitor and never retry in a loop.
Example
/**
* Report that a user became an administrator outside a web request, e.g. in an
* artisan command. Never stops the command: a failure is logged and that is all.
*/
function reportAdminGranted(int $userId, string $via): void
{
if (!class_exists(NetsonCloud::class)) {
return; // no library on this machine
}
$result = app(NetsonCloud::class)->security()->recordUnattributed(
'user.admin_granted',
['user_id' => $userId, 'via' => $via], // an id, never a name or email address
);
if ($result->isOk() || $result->status === Result::UNAVAILABLE) {
return; // recorded, or there is no feed here: both are fine
}
error_log('security feed: ' . $result->reason); // failed: note it and carry on
}
Constants
OK='ok'—statusof an operation that was done.UNAVAILABLE='unavailable'—statuswhen the feature is off here; nothing was done.FAILED='failed'—statusof an operation that was tried and failed; seereason.
Properties
$status(string), read-only — One of the constants above:ok,unavailableorfailed.$reason(string), read-only — Why, forunavailableandfailed, in one line for your log; empty forok.
Methods
isOk()
public function isOk(): bool
Whether the operation succeeded.
Returns bool: true only for ok; false for both unavailable and failed (read status to tell them apart)
See the example at the top of this page.
Example
/**
* Clear the cached news overview and the item that changed.
*/
function purgeNewsPages(string $base, string $slug): bool
{
if (!class_exists(NetsonCloud::class)) {
return false; // no library on this machine: nothing to purge
}
$cache = app(NetsonCloud::class)->cache();
if (!$cache->available()) {
return false; // this site has no page cache
}
$changes = new Intents();
$changes->addUrls(["{$base}/news/{$slug}", "{$base}/news"], 'news:published');
return $cache->flush($changes, '')->isOk();
}