Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
InMemoryNotificationOutbox
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
4 / 4
5
100.00% covered (success)
100.00%
1 / 1
 enqueue
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 all
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 drain
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 snapshot
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace LambdaTwelve\OneRecord\Server\InMemory;
6
7use LambdaTwelve\OneRecord\Api\Notification;
8use LambdaTwelve\OneRecord\Rdf\Graph;
9use LambdaTwelve\OneRecord\Server\Spi\NotificationOutbox;
10use LambdaTwelve\OneRecord\Server\Spi\OutboundNotification;
11use LambdaTwelve\OneRecord\Server\Spi\Volatile;
12
13final class InMemoryNotificationOutbox implements NotificationOutbox, Volatile
14{
15    /** @var list<OutboundNotification> */
16    private array $queue = [];
17
18    public function enqueue(OutboundNotification $notification): void
19    {
20        // The queued body is what was true when it was queued, whatever the caller does with its object afterwards (R2-005).
21        $notification = self::snapshot($notification);
22        $this->queue[] = $notification;
23    }
24
25    /**
26     * @return list<OutboundNotification>
27     */
28    public function all(): array
29    {
30        // What is read is a copy: editing it must not change what will be delivered (R3-003).
31        return array_map(self::snapshot(...), $this->queue);
32    }
33
34    /**
35     * Takes everything off the queue, as a host's delivery job would.
36     *
37     * @return list<OutboundNotification>
38     */
39    public function drain(): array
40    {
41        $queue = $this->queue;
42        $this->queue = [];
43
44        return $queue;
45    }
46
47    private static function snapshot(OutboundNotification $outbound): OutboundNotification
48    {
49        $n = $outbound->notification;
50        if ($n->body === null) {
51            return $outbound;
52        }
53        $copy = new Notification($n->eventType, $n->logisticsObject, $n->logisticsObjectType, $n->triggeredBy, $n->changedProperties, $n->logisticsEvents, $n->body->withGraph(new Graph($n->body->graph)));
54
55        return new OutboundNotification($outbound->recipient, $copy, $outbound->createdAt, $outbound->id);
56    }
57}