Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
IcapResponse
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 withHeader
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * SPDX-License-Identifier: EUPL-1.2
5 *
6 * This file is part of icap-flow.
7 *
8 * Licensed under the EUPL, Version 1.2 only (the "Licence");
9 * you may not use this work except in compliance with the Licence.
10 * You may obtain a copy of the Licence at:
11 *
12 *     https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the Licence is distributed on an "AS IS" basis,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 */
18
19declare(strict_types=1);
20
21namespace Ndrstmr\Icap\DTO;
22
23/**
24 * Immutable DTO representing an ICAP response.
25 *
26 * A response that was read in the discarding mode of the scan helpers
27 * ({@see \Ndrstmr\Icap\DiscardingIcapClient}) was received and checked to its last
28 * byte, but its body was not kept: `body` is then empty and `discardedBodyBytes`
29 * says how many bytes the body had (0 for none). Only that field tells such a
30 * response from one whose body is empty.
31 *
32 * `encapsulatedStatus` is the HTTP status of the encapsulated response (its `res-hdr` section), if the
33 * ICAP response has one: the 403 of a block page, the 200 of a message sent back.
34 */
35final readonly class IcapResponse
36{
37    /** @var array<string, string[]> */
38    public array $headers;
39
40    /**
41     * @param int $statusCode HTTP status code, e.g. 200, 204, 500
42     * @param array<string, string[]> $headers Response headers
43     * @param string $body Response body content
44     * @param int|null $discardedBodyBytes Length of the decoded body that was received and not kept; null if the body is in `$body`
45     * @param int|null $encapsulatedStatus HTTP status of the encapsulated response; null if the response has none
46     */
47    public function __construct(
48        public int $statusCode,
49        array $headers = [],
50        public string $body = '',
51        public ?int $discardedBodyBytes = null,
52        public ?int $encapsulatedStatus = null,
53    ) {
54        $this->headers = array_map(fn ($v) => (array) $v, $headers);
55    }
56
57    /**
58     * @param string|string[] $value
59     */
60    public function withHeader(string $name, string|array $value): self
61    {
62        $headers = $this->headers;
63        $headers[$name] = (array) $value;
64
65        return new self(
66            $this->statusCode,
67            $headers,
68            $this->body,
69            $this->discardedBodyBytes,
70            $this->encapsulatedStatus,
71        );
72    }
73}