Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
InMemoryServer
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace LambdaTwelve\OneRecord\Server\InMemory;
6
7use LambdaTwelve\OneRecord\Server\GrantAccessPolicy;
8use LambdaTwelve\OneRecord\Server\OneRecordServer;
9use LambdaTwelve\OneRecord\Server\ServerBuilder;
10use LambdaTwelve\OneRecord\Server\ServerConfig;
11use LambdaTwelve\OneRecord\Server\Services;
12use LambdaTwelve\OneRecord\Server\Spi\Authenticator;
13use LambdaTwelve\OneRecord\Server\Spi\Decision;
14use LambdaTwelve\OneRecord\Server\Spi\IdGenerator;
15use LambdaTwelve\OneRecord\Server\Spi\UnitOfWork;
16use Psr\Clock\ClockInterface;
17use Psr\EventDispatcher\EventDispatcherInterface;
18use Psr\Http\Message\ResponseFactoryInterface;
19use Psr\Http\Message\StreamFactoryInterface;
20use Psr\Log\LoggerInterface;
21
22/**
23 * A complete server on in-memory stores: what tests drive and what bin/serve
24 * runs. Hosts use it as the worked example of wiring Services; the stores it
25 * exposes are also the contract their persistent implementations must meet.
26 */
27final class InMemoryServer
28{
29    public readonly InMemoryState $state;
30    public readonly InMemoryLogisticsObjectStore $objects;
31    public readonly InMemoryLogisticsEventStore $events;
32    public readonly InMemoryActionRequestStore $actionRequests;
33    public readonly InMemorySubscriptionStore $subscriptions;
34    public readonly InMemoryAccessDelegationStore $delegations;
35    public readonly InMemoryNotificationOutbox $outbox;
36    public readonly GrantAccessPolicy $policy;
37    public readonly Services $services;
38    public readonly OneRecordServer $handler;
39
40    /**
41     * @param ?InMemoryState $state stores to continue from (bin/serve reloads them between requests); fresh ones when null
42     * @param ?UnitOfWork $unitOfWork the host's transaction boundary; none by default
43     */
44    public function __construct(
45        ServerConfig $config,
46        Authenticator $authenticator,
47        ClockInterface $clock,
48        EventDispatcherInterface $dispatcher,
49        ResponseFactoryInterface $responses,
50        StreamFactoryInterface $streams,
51        ?LoggerInterface $logger = null,
52        Decision $denial = Decision::Forbid,
53        ?IdGenerator $ids = null,
54        ?InMemoryState $state = null,
55        ?UnitOfWork $unitOfWork = null,
56    ) {
57        $this->state = $state ?? new InMemoryState($clock, $denial);
58        $this->objects = $this->state->objects;
59        $this->events = $this->state->events;
60        $this->actionRequests = $this->state->actionRequests;
61        $this->subscriptions = $this->state->subscriptions;
62        $this->delegations = $this->state->delegations;
63        $this->outbox = $this->state->outbox;
64        $this->policy = $this->state->policy;
65        $this->services = new Services(
66            $config,
67            $this->objects,
68            $this->events,
69            $this->actionRequests,
70            $this->subscriptions,
71            $this->delegations,
72            $this->outbox,
73            $authenticator,
74            $this->policy,
75            $clock,
76            $dispatcher,
77            $responses,
78            $streams,
79            $logger,
80            ids: $ids,
81            unitOfWork: $unitOfWork,
82        );
83        $this->handler = ServerBuilder::build($this->services);
84    }
85}