Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
11 / 11 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| InMemorySubscriptionStore | |
100.00% |
11 / 11 |
|
100.00% |
5 / 5 |
12 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| offer | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| withdraw | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
3 | |||
| subscribersOf | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
5 | |||
| offered | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace LambdaTwelve\OneRecord\Server\InMemory; |
| 6 | |
| 7 | use DateTimeImmutable; |
| 8 | use LambdaTwelve\OneRecord\Api\ActionRequestType; |
| 9 | use LambdaTwelve\OneRecord\Api\Subscription; |
| 10 | use LambdaTwelve\OneRecord\Api\TopicType; |
| 11 | use LambdaTwelve\OneRecord\Rdf\Iri; |
| 12 | use LambdaTwelve\OneRecord\Server\Spi\ActionRequestStore; |
| 13 | use LambdaTwelve\OneRecord\Server\Spi\SubscriptionStore; |
| 14 | use LambdaTwelve\OneRecord\Server\Spi\Volatile; |
| 15 | |
| 16 | /** |
| 17 | * Publisher side derived from the action requests (an accepted |
| 18 | * SubscriptionRequest is a subscription); subscriber side from a list the |
| 19 | * host fills with the subscriptions it wants to offer. |
| 20 | */ |
| 21 | final class InMemorySubscriptionStore implements SubscriptionStore, Volatile |
| 22 | { |
| 23 | /** @var list<Subscription> */ |
| 24 | private array $offered = []; |
| 25 | |
| 26 | public function __construct(private readonly ActionRequestStore $requests) {} |
| 27 | |
| 28 | public function offer(Subscription $subscription): void |
| 29 | { |
| 30 | $this->offered[] = $subscription; |
| 31 | } |
| 32 | |
| 33 | public function withdraw(Subscription $subscription): void |
| 34 | { |
| 35 | $this->offered = array_values(array_filter($this->offered, static fn(Subscription $s): bool => !($s->subscriber->equals($subscription->subscriber) && $s->topicType === $subscription->topicType && $s->topic === $subscription->topic))); |
| 36 | } |
| 37 | |
| 38 | public function subscribersOf(Iri $logisticsObject, array $types, DateTimeImmutable $now): array |
| 39 | { |
| 40 | $out = []; |
| 41 | foreach ($this->requests->accepted(ActionRequestType::Subscription) as $request) { |
| 42 | $subscription = $request->payload; |
| 43 | if (!$subscription instanceof Subscription || $subscription->isExpiredAt($now) || !$subscription->covers($logisticsObject, $types)) { |
| 44 | continue; |
| 45 | } |
| 46 | $out[] = ['subscription' => $subscription, 'request' => $request->iri]; |
| 47 | } |
| 48 | |
| 49 | return $out; |
| 50 | } |
| 51 | |
| 52 | public function offered(TopicType $topicType, string $topic): array |
| 53 | { |
| 54 | return array_values(array_filter($this->offered, static fn(Subscription $s): bool => $s->topicType === $topicType && $s->topic === $topic)); |
| 55 | } |
| 56 | |
| 57 | } |