Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
VerificationEndpoint
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
5
100.00% covered (success)
100.00%
1 / 1
 handle
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3declare(strict_types=1);
4
5namespace LambdaTwelve\OneRecord\Server\Endpoint;
6
7use LambdaTwelve\OneRecord\Api\Verification;
8use LambdaTwelve\OneRecord\Server\ActionRequests;
9use LambdaTwelve\OneRecord\Server\Http\ContentNegotiation;
10use LambdaTwelve\OneRecord\Server\Http\HttpException;
11use LambdaTwelve\OneRecord\Server\Http\Negotiated;
12use LambdaTwelve\OneRecord\Server\Spi\Action;
13use LambdaTwelve\OneRecord\Server\Spi\Agent;
14use LambdaTwelve\OneRecord\Server\Spi\Decision;
15use LambdaTwelve\OneRecord\Vocabulary\Generated\Api;
16use Psr\Http\Message\ResponseInterface;
17use Psr\Http\Message\ServerRequestInterface;
18
19/**
20 * POST /logistics-objects/{id} with an api:Verification: a third party flags
21 * a problem. Authentication only, per spec; a policy that hides the object
22 * still hides it here so the endpoint cannot be used to probe existence.
23 */
24final class VerificationEndpoint extends AbstractEndpoint
25{
26    public function handle(ServerRequestInterface $request, Agent $agent, Negotiated $negotiated, array $parameters): ResponseInterface
27    {
28        $iri = $this->services->config->logisticsObjectIri($parameters['id']);
29        if ($this->services->policy->decide($agent, Action::ReadLogisticsObject, $iri) === Decision::Hide) {
30            throw HttpException::notFound('Logistics Object', $iri->value);
31        }
32        $stored = $this->services->objects->latest($iri) ?? throw HttpException::notFound('Logistics Object', $iri->value);
33        (new ContentNegotiation($this->services->config))->bodyVersion($request, $negotiated);
34        $verification = Verification::fromJsonLd($this->services->body->json($request));
35        if (!$verification->logisticsObject->equals($stored->object->iri)) {
36            throw HttpException::badRequest(\sprintf('api:hasLogisticsObject is %s but the request was sent to %s.', $verification->logisticsObject->value, $stored->object->iri->value), Api::hasLogisticsObject, 'Invalid resource');
37        }
38        if ($verification->revision !== null && $verification->revision > $stored->latestRevision) {
39            throw HttpException::unprocessable(\sprintf('Revision %d does not exist yet; the object is at revision %d.', $verification->revision, $stored->latestRevision), Api::hasRevision);
40        }
41        $created = (new ActionRequests($this->services))->create($verification, $agent->iri);
42
43        return $this->services->responder->empty(201, $negotiated, ['Location' => $created->iri->value, 'Type' => Api::VerificationRequest]);
44    }
45}