Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
RequestStatus
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
4 / 4
16
100.00% covered (success)
100.00%
1 / 1
 tryFromString
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
5
 shortName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isFinal
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 canTransitionTo
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
8
1<?php
2
3declare(strict_types=1);
4
5namespace LambdaTwelve\OneRecord\Api;
6
7use LambdaTwelve\OneRecord\Vocabulary\Generated\Api;
8
9/**
10 * api:RequestStatus, with the two state machines of the spec's action-request
11 * page: accept/reject/revoke for change, subscription and access-delegation
12 * requests (only the latter two revocable after acceptance), acknowledge/
13 * reject/revoke/fail for verification requests.
14 */
15enum RequestStatus: string
16{
17    case Pending = Api::REQUEST_PENDING;
18    case Accepted = Api::REQUEST_ACCEPTED;
19    case Acknowledged = Api::REQUEST_ACKNOWLEDGED;
20    case Rejected = Api::REQUEST_REJECTED;
21    case Failed = Api::REQUEST_FAILED;
22    case Revoked = Api::REQUEST_REVOKED;
23
24    /**
25     * Full IRI, "api:REQUEST_ACCEPTED" or bare "REQUEST_ACCEPTED" (and the
26     * spec's audit-trail short forms PENDING/ACCEPTED/REJECTED).
27     */
28    public static function tryFromString(string $value): ?self
29    {
30        $value = trim($value);
31        if (str_starts_with($value, 'api:')) {
32            $value = substr($value, 4);
33        }
34        if (!str_contains($value, ':') && !str_starts_with($value, 'REQUEST_')) {
35            $value = 'REQUEST_' . $value;
36        }
37        if (!str_contains($value, ':')) {
38            $value = Api::NAMESPACE . $value;
39        }
40
41        return self::tryFrom($value);
42    }
43
44    public function shortName(): string
45    {
46        return substr($this->value, \strlen(Api::NAMESPACE));
47    }
48
49    public function isFinal(): bool
50    {
51        return $this !== self::Pending && $this !== self::Accepted;
52    }
53
54    public function canTransitionTo(self $next, ActionRequestType $type): bool
55    {
56        if ($type === ActionRequestType::Verification) {
57            return $this === self::Pending && \in_array($next, [self::Acknowledged, self::Rejected, self::Revoked, self::Failed], true);
58        }
59
60        return match ($this) {
61            self::Pending => \in_array($next, [self::Accepted, self::Rejected, self::Revoked], true),
62            self::Accepted => $next === self::Failed || ($next === self::Revoked && $type->revocableAfterAcceptance()),
63            default => false,
64        };
65    }
66}