Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
RejectionViewer
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
3 / 3
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 isPrivilegedFor
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 getAudience
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5/**
6 * SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
7 * SPDX-License-Identifier: AGPL-3.0-or-later
8 */
9
10namespace OCA\Libresign\Service\SignatureRejection;
11
12use OCA\Libresign\Db\SignRequest;
13use OCA\Libresign\Enum\SignatureRejectionVisibility;
14
15/**
16 * Who is reading the signers of one file, as far as rejection visibility is
17 * concerned.
18 *
19 * The requester sees every rejection, and a signer always sees their own.
20 * Anybody else sees what the `rejection_visibility` level allows for the
21 * narrowest audience they belong to: a signer taking part in the workflow is a
22 * participant; an observer, another authenticated user and an anonymous
23 * reader are the public, since they could only reach the file through the
24 * validation page.
25 */
26final class RejectionViewer {
27    /** @var list<int> */
28    private readonly array $ownSignRequestIds;
29    private readonly bool $participant;
30
31    /**
32     * @param SignRequest[] $ownSignRequests the sign requests of the file that belong to the viewer
33     */
34    public function __construct(
35        private readonly bool $requester,
36        array $ownSignRequests,
37    ) {
38        $this->ownSignRequestIds = array_values(array_map(
39            static fn (SignRequest $signRequest): int => $signRequest->getId(),
40            $ownSignRequests,
41        ));
42        $this->participant = array_filter(
43            $ownSignRequests,
44            static fn (SignRequest $signRequest): bool => $signRequest->getParticipantRoleEnum()->canSign(),
45        ) !== [];
46    }
47
48    /** Whether the viewer may see everything about this signer's rejection. */
49    public function isPrivilegedFor(SignRequest $signRequest): bool {
50        return $this->requester || in_array($signRequest->getId(), $this->ownSignRequestIds, true);
51    }
52
53    /** The narrowest audience the viewer belongs to. */
54    public function getAudience(): SignatureRejectionVisibility {
55        return match (true) {
56            $this->requester => SignatureRejectionVisibility::REQUESTER,
57            $this->participant => SignatureRejectionVisibility::PARTICIPANTS,
58            default => SignatureRejectionVisibility::PUBLIC,
59        };
60    }
61}