Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
NotificationsEndpoint
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
1 / 1
 handle
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace LambdaTwelve\OneRecord\Server\Endpoint;
6
7use LambdaTwelve\OneRecord\Api\Notification;
8use LambdaTwelve\OneRecord\Server\Event\NotificationReceived;
9use LambdaTwelve\OneRecord\Server\Http\ContentNegotiation;
10use LambdaTwelve\OneRecord\Server\Http\Negotiated;
11use LambdaTwelve\OneRecord\Server\Spi\Agent;
12use Psr\Http\Message\ResponseInterface;
13use Psr\Http\Message\ServerRequestInterface;
14
15/**
16 * POST /notifications: a partner tells us something happened on its server.
17 * Any authenticated party may post; the server validates, raises an event
18 * for the host and answers 204. What to do about it (fetch the object,
19 * update a record) is the host's business.
20 */
21final class NotificationsEndpoint extends AbstractEndpoint
22{
23    public function handle(ServerRequestInterface $request, Agent $agent, Negotiated $negotiated, array $parameters): ResponseInterface
24    {
25        (new ContentNegotiation($this->services->config))->bodyVersion($request, $negotiated);
26        $notification = Notification::fromJsonLd($this->services->body->json($request));
27        $this->services->logger->info('Notification received', ['from' => $agent->iri->value, 'eventType' => $notification->eventType->name, 'object' => $notification->logisticsObject?->value]);
28        $key = $request->getHeaderLine('Idempotency-Key');
29        $this->services->dispatcher->dispatch(new NotificationReceived($notification, $agent, $key === '' ? null : $key));
30
31        return $this->services->responder->empty(204, $negotiated);
32    }
33}